简体   繁体   中英

Problem interpolating using RBF from python's scipy

I am following the accepted answer of this thread using my ownd gridded data.

I load it as:

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)

(I do the same for Y and F(X, Y)) and then, I use what is displayed in the aforementioned 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')

Unfortunately, the last line here is an error. I get

    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

I have no idea why this happens, since in the original code x appears in the last line and is

type(x)
Out[26]: numpy.ndarray

which is the same type of variable as

type(resulttokenX2)
Out[24]: numpy.ndarray

I don't know why this happens. Can someone tell me what I have to do to reproduce the original code with my gridded data instead of random?

Thanks.

Edit:

resulttokenY2
Out[3]: 
array(['3.2000000e+01', '3.2000000e+01',

are the first lines of resulttokenY2

After some very helpful advice from Yann ziselman I have managed to do it. This is the full code:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM