插值经纬度坐标数据系列 - 输入数据错误
Interpolating a data series of longitude and latitude coordinates - Error in input data
我想根据一组经度和纬度值执行插值。
我用库 SciPy.interpolate 试过这个。下面显示的代码在简单数据系列上也可以正常工作。
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
import csv
csv_file = csv.DictReader(open(file=file_path))
longitude = []
latitude = []
for row in csv_file:
longitude.append(float(row['longitude']))
latitude.append(float(row['latitude']))
plt.scatter(longitude, latitude, color='blue', label='given')
tck, u = interpolate.splprep([longitude, latitude], s=0.0)
x_i, y_i= interpolate.splev(np.linspace(0, 1, 100), tck)
plt.plot(x_i, y_i, color='green', label='calculated')
plt.legend()
plt.show()
如果我尝试使用真实的经度和纬度值执行代码,我会得到一个我无法解释其含义的异常。
在第15行抛出异常:
(tck, u = interpolate.splprep([longitude, latitude], s=0.0))
它说:ValueError: Invalid inputs.
但我只是不知道哪些输入应该无效。读入的所有值都是浮点值,None 值也不存在。应该在大约 900 个坐标上执行插值。但即使有 50 个坐标也会抛出此异常。我的代码中是否有任何限制可以解释此类异常?
非常感谢!
csv 导入一定有问题,我只是 运行 你的代码与 1000 运行dom 浮点对一样,它执行时没有任何错误。
尝试用
初始化你的数组
for i in range(0, 1000):
longitude.append(random.randrange(0.0, 120.0))
latitude.append(random.randrange(0.0, 89.0))
而不是导入 csv,看看您的问题是否仍然存在。
问题是我的数据集中有一些重复数据。显然scipy处理起来有些问题。
作为删除所有重复数据点,上面显示的代码完美运行。
我想根据一组经度和纬度值执行插值。
我用库 SciPy.interpolate 试过这个。下面显示的代码在简单数据系列上也可以正常工作。
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
import csv
csv_file = csv.DictReader(open(file=file_path))
longitude = []
latitude = []
for row in csv_file:
longitude.append(float(row['longitude']))
latitude.append(float(row['latitude']))
plt.scatter(longitude, latitude, color='blue', label='given')
tck, u = interpolate.splprep([longitude, latitude], s=0.0)
x_i, y_i= interpolate.splev(np.linspace(0, 1, 100), tck)
plt.plot(x_i, y_i, color='green', label='calculated')
plt.legend()
plt.show()
如果我尝试使用真实的经度和纬度值执行代码,我会得到一个我无法解释其含义的异常。
在第15行抛出异常:
(tck, u = interpolate.splprep([longitude, latitude], s=0.0))
它说:ValueError: Invalid inputs.
但我只是不知道哪些输入应该无效。读入的所有值都是浮点值,None 值也不存在。应该在大约 900 个坐标上执行插值。但即使有 50 个坐标也会抛出此异常。我的代码中是否有任何限制可以解释此类异常?
非常感谢!
csv 导入一定有问题,我只是 运行 你的代码与 1000 运行dom 浮点对一样,它执行时没有任何错误。 尝试用
初始化你的数组for i in range(0, 1000):
longitude.append(random.randrange(0.0, 120.0))
latitude.append(random.randrange(0.0, 89.0))
而不是导入 csv,看看您的问题是否仍然存在。
问题是我的数据集中有一些重复数据。显然scipy处理起来有些问题。 作为删除所有重复数据点,上面显示的代码完美运行。