ValueError: invalid shape for input data points in griddata operation
ValueError: invalid shape for input data points in griddata operation
我 运行 在使用 scipy.interpolate.griddata 时遇到了错误。我的目标是使用 matplotlib 准备等高线数据。我读过执行此操作的最佳方法是在传递给 griddata 之前使用 linspace 将 x 和 y 分离为一维数组。
我的 x 和 y 值的最小值和最大值用于输入到 linspace,以保持坐标相同以用于 GIS 制图目的(不确定是否有必要拥有数据点在与网格坐标相同的 xy 区域,但我正在这样做)
Watertable CSV 文件作为具有 x、y 和 z 值的 numpy 数组导入。 z 作为直接数组列索引提供给 griddata。
我运行进入错误"valueError: invalid shape for input data points"
我相信这很简单,希望有人能指出我的错误。
[编辑]
我已经按照建议使用 pastebin 链接了 csv 文件:
import numpy as np
from scipy.interpolate import griddata
from numpy import genfromtxt
my_data = genfromtxt('WaterTable.csv', delimiter=',')
x = my_data[1:,0:1]
y = my_data[1:,1:2]
z = my_data[1:,2:3]
xmax = max(x)
xmin = min(x)
ymax = max(y)
ymin = min(y)
xi = np.linspace(xmin, xmax, 2000)
yi = np.linspace(ymin, ymax, 2000)
zi = griddata((x, y), z, (xi, yi), method='cubic')
然后我的脚本退出并出现以下错误:
Traceback (most recent call last):
File "C:/Users/Hp/PycharmProjects/GISdev/Irregular_Grid03.py", line 60, in <module>
zi = griddata((x, y), z, (xi, yi), method='cubic')
File "C:\Python27\lib\site-packages\scipy\interpolate\ndgriddata.py", line 212, in griddata
rescale=rescale)
File "scipy/interpolate/interpnd.pyx", line 840, in scipy.interpolate.interpnd.CloughTocher2DInterpolator.__init__ (scipy\interpolate\interpnd.c:9961)
File "scipy/interpolate/interpnd.pyx", line 78, in scipy.interpolate.interpnd.NDInterpolatorBase.__init__ (scipy\interpolate\interpnd.c:2356)
File "scipy/interpolate/interpnd.pyx", line 123, in scipy.interpolate.interpnd.NDInterpolatorBase._check_init_shape (scipy\interpolate\interpnd.c:3128)
ValueError: invalid shape for input data points
您的数组 x
、y
和 z
是 二维的 ,形状为 (n, 1)
。 griddata
需要 一维 数组(即形状 (n,)
)。
要解决此问题,请在将数组从 my_data
中拉出时在第二个索引位置使用单个索引而不是切片:
x = my_data[1:, 0]
y = my_data[1:, 1]
z = my_data[1:, 2]
我 运行 在使用 scipy.interpolate.griddata 时遇到了错误。我的目标是使用 matplotlib 准备等高线数据。我读过执行此操作的最佳方法是在传递给 griddata 之前使用 linspace 将 x 和 y 分离为一维数组。
我的 x 和 y 值的最小值和最大值用于输入到 linspace,以保持坐标相同以用于 GIS 制图目的(不确定是否有必要拥有数据点在与网格坐标相同的 xy 区域,但我正在这样做)
Watertable CSV 文件作为具有 x、y 和 z 值的 numpy 数组导入。 z 作为直接数组列索引提供给 griddata。
我运行进入错误"valueError: invalid shape for input data points"
我相信这很简单,希望有人能指出我的错误。
[编辑]
我已经按照建议使用 pastebin 链接了 csv 文件:
import numpy as np
from scipy.interpolate import griddata
from numpy import genfromtxt
my_data = genfromtxt('WaterTable.csv', delimiter=',')
x = my_data[1:,0:1]
y = my_data[1:,1:2]
z = my_data[1:,2:3]
xmax = max(x)
xmin = min(x)
ymax = max(y)
ymin = min(y)
xi = np.linspace(xmin, xmax, 2000)
yi = np.linspace(ymin, ymax, 2000)
zi = griddata((x, y), z, (xi, yi), method='cubic')
然后我的脚本退出并出现以下错误:
Traceback (most recent call last):
File "C:/Users/Hp/PycharmProjects/GISdev/Irregular_Grid03.py", line 60, in <module>
zi = griddata((x, y), z, (xi, yi), method='cubic')
File "C:\Python27\lib\site-packages\scipy\interpolate\ndgriddata.py", line 212, in griddata
rescale=rescale)
File "scipy/interpolate/interpnd.pyx", line 840, in scipy.interpolate.interpnd.CloughTocher2DInterpolator.__init__ (scipy\interpolate\interpnd.c:9961)
File "scipy/interpolate/interpnd.pyx", line 78, in scipy.interpolate.interpnd.NDInterpolatorBase.__init__ (scipy\interpolate\interpnd.c:2356)
File "scipy/interpolate/interpnd.pyx", line 123, in scipy.interpolate.interpnd.NDInterpolatorBase._check_init_shape (scipy\interpolate\interpnd.c:3128)
ValueError: invalid shape for input data points
您的数组 x
、y
和 z
是 二维的 ,形状为 (n, 1)
。 griddata
需要 一维 数组(即形状 (n,)
)。
要解决此问题,请在将数组从 my_data
中拉出时在第二个索引位置使用单个索引而不是切片:
x = my_data[1:, 0]
y = my_data[1:, 1]
z = my_data[1:, 2]