Interpolate each row in the x value matrix

I want to interpolate between the values ​​in each row of the matrix (x-value) given by a fixed vector of y values. I am using python and essentially I need something like scipy.interpolate.interp1d , but with x values ​​are the input to the matrix. I implemented this by cyclizing, but I want to do the operation as quickly as possible.

Edit

Below is a sample code of what I'm doing right now, note that my matrix has more rows in the order of millions:

 import numpy as np x = np.linspace(0,1,100).reshape(10,10) results = np.zeros(10) for i in range(10): results[i] = np.interp(0.1,x[i],range(10)) 
+6
source share
1 answer

As @Joe Kington suggested using map_coordinates :

 import scipy.ndimage as nd # your data - make sure is float/double X = np.arange(100).reshape(10,10).astype(float) # the points where you want to interpolate each row y = np.random.rand(10) * (X.shape[1]-1) # the rows at which you want the data interpolated -- all rows r = np.arange(X.shape[0]) result = nd.map_coordinates(X, [r, y], order=1, mode='nearest') 

Above for the following y :

 array([ 8.00091648, 0.46124587, 7.03994936, 1.26307275, 1.51068952, 5.2981205 , 7.43509764, 7.15198457, 5.43442468, 0.79034372]) 

Note. Each value indicates the position at which the value will be interpolated for each row.

Gives the following result :

 array([ 8.00091648, 10.46124587, 27.03994936, 31.26307275, 41.51068952, 55.2981205 , 67.43509764, 77.15198457, 85.43442468, 90.79034372]) 

which makes sense given the nature of the arange d data and the columns ( y ) at which it is interpolated.

+1
source

Source: https://habr.com/ru/post/989311/


All Articles