使用 python 的 scipy 中的 RBF 进行插值时出现问题
Problem interpolating using RBF from python's scipy
我正在使用我自己的网格化数据遵循 this thread 的公认答案。
我加载为:
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
import pylab as py
token = open('Ydata_48_of_50.txt','r')
linestoken=token.readlines()
tokens_column_numberX = 0
resulttokenX=[]
for x in linestoken:
resulttokenX.append(x.split()[tokens_column_numberX])
token.close()
resulttokenX = np.array(resulttokenX)
(我对 Y 和 F(X, Y) 做同样的事情)然后,我使用前面提到的 link:
中显示的内容
xi, yi = np.linspace(resulttokenX.min(), resulttokenX.max(), 200), np.linspace(resulttokenY.min(), resulttokenY.max(), 200)
xi, yi = np.meshgrid(xi, yi)
# Interpolate
rbf = scipy.interpolate.Rbf(resulttokenX, resulttokenY, resulttokenF, function='linear')
不幸的是,这里的最后一行是错误的。我得到
xi, yi = np.linspace(resulttokenX2.min(), resulttokenX2.max(), 200), np.linspace(resulttokenY2.min(), resulttokenY2.max(), 200)
File "D:\Users\me\anaconda3\lib\site-packages\numpy\core\_methods.py", line 43, in _amin
return umr_minimum(a, axis, None, out, keepdims, initial, where)
TypeError: cannot perform reduce with flexible type
我不知道为什么会这样,因为在原始代码中 x
出现在最后一行并且是
type(x)
Out[26]: numpy.ndarray
与
是同一类型的变量
type(resulttokenX2)
Out[24]: numpy.ndarray
我不知道为什么会这样。有人能告诉我如何使用我的网格化数据而不是随机数据来重现原始代码吗?
谢谢。
编辑:
resulttokenY2
Out[3]:
array(['3.2000000e+01', '3.2000000e+01',
是resulttokenY2的第一行
在 Yann ziselman 的一些非常有用的建议之后,我成功地做到了。这是完整的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
import pylab as py
import scipy
token = open('Ydata_48_of_50.txt','r')
linestoken=token.readlines()
tokens_column_numberX = 0
tokens_column_numberY = 1
tokens_column_numberF = 2
resulttokenX=[]
resulttokenY=[]
resulttokenF=[]
for x in linestoken:
resulttokenX.append(x.split()[tokens_column_numberX])
resulttokenY.append(x.split()[tokens_column_numberY])
resulttokenF.append(x.split()[tokens_column_numberF])
token.close()
resulttokenX2 = np.array(resulttokenX)
resulttokenY2 = np.array(resulttokenY)
resulttokenF2 = np.array(resulttokenF)
# Set up a regular grid of interpolation points
xi, yi = np.linspace(resulttokenX2.astype('float').min(), resulttokenX2.astype('float').max(), 100), np.linspace(resulttokenY2.astype('float').min(), resulttokenY2.astype('float').max(), 100)
xi, yi = np.meshgrid(xi, yi)
# Interpolate
rbf = scipy.interpolate.Rbf(resulttokenX2, resulttokenY2, resulttokenF2, function='linear')
zi = rbf(xi, yi)
plt.imshow(zi, vmin=resulttokenF2.astype('float').min(), vmax=resulttokenF2.astype('float').max(), origin='lower', extent=[resulttokenX2.astype('float').min(), resulttokenX2.astype('float').max(), resulttokenY2.astype('float').min(), resulttokenY2.astype('float').max()])
plt.scatter(resulttokenX2.astype('float'), resulttokenY2.astype('float'), c=resulttokenF2.astype('float'))
plt.colorbar()
plt.show()
我正在使用我自己的网格化数据遵循 this thread 的公认答案。
我加载为:
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
import pylab as py
token = open('Ydata_48_of_50.txt','r')
linestoken=token.readlines()
tokens_column_numberX = 0
resulttokenX=[]
for x in linestoken:
resulttokenX.append(x.split()[tokens_column_numberX])
token.close()
resulttokenX = np.array(resulttokenX)
(我对 Y 和 F(X, Y) 做同样的事情)然后,我使用前面提到的 link:
中显示的内容xi, yi = np.linspace(resulttokenX.min(), resulttokenX.max(), 200), np.linspace(resulttokenY.min(), resulttokenY.max(), 200)
xi, yi = np.meshgrid(xi, yi)
# Interpolate
rbf = scipy.interpolate.Rbf(resulttokenX, resulttokenY, resulttokenF, function='linear')
不幸的是,这里的最后一行是错误的。我得到
xi, yi = np.linspace(resulttokenX2.min(), resulttokenX2.max(), 200), np.linspace(resulttokenY2.min(), resulttokenY2.max(), 200)
File "D:\Users\me\anaconda3\lib\site-packages\numpy\core\_methods.py", line 43, in _amin
return umr_minimum(a, axis, None, out, keepdims, initial, where)
TypeError: cannot perform reduce with flexible type
我不知道为什么会这样,因为在原始代码中 x
出现在最后一行并且是
type(x)
Out[26]: numpy.ndarray
与
是同一类型的变量type(resulttokenX2)
Out[24]: numpy.ndarray
我不知道为什么会这样。有人能告诉我如何使用我的网格化数据而不是随机数据来重现原始代码吗?
谢谢。
编辑:
resulttokenY2
Out[3]:
array(['3.2000000e+01', '3.2000000e+01',
是resulttokenY2的第一行
在 Yann ziselman 的一些非常有用的建议之后,我成功地做到了。这是完整的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
import pylab as py
import scipy
token = open('Ydata_48_of_50.txt','r')
linestoken=token.readlines()
tokens_column_numberX = 0
tokens_column_numberY = 1
tokens_column_numberF = 2
resulttokenX=[]
resulttokenY=[]
resulttokenF=[]
for x in linestoken:
resulttokenX.append(x.split()[tokens_column_numberX])
resulttokenY.append(x.split()[tokens_column_numberY])
resulttokenF.append(x.split()[tokens_column_numberF])
token.close()
resulttokenX2 = np.array(resulttokenX)
resulttokenY2 = np.array(resulttokenY)
resulttokenF2 = np.array(resulttokenF)
# Set up a regular grid of interpolation points
xi, yi = np.linspace(resulttokenX2.astype('float').min(), resulttokenX2.astype('float').max(), 100), np.linspace(resulttokenY2.astype('float').min(), resulttokenY2.astype('float').max(), 100)
xi, yi = np.meshgrid(xi, yi)
# Interpolate
rbf = scipy.interpolate.Rbf(resulttokenX2, resulttokenY2, resulttokenF2, function='linear')
zi = rbf(xi, yi)
plt.imshow(zi, vmin=resulttokenF2.astype('float').min(), vmax=resulttokenF2.astype('float').max(), origin='lower', extent=[resulttokenX2.astype('float').min(), resulttokenX2.astype('float').max(), resulttokenY2.astype('float').min(), resulttokenY2.astype('float').max()])
plt.scatter(resulttokenX2.astype('float'), resulttokenY2.astype('float'), c=resulttokenF2.astype('float'))
plt.colorbar()
plt.show()