Reputation: 5512
I have 2 arrays that I plot this way:
plt.plot(x1, x2)
And I want to find out what are the values of x2
at x1=2,5,75,10,100, and 1000
. Since all values may not exist, I interpolate using this:
from scipy.interpolate import interp1d
f1=interp1d(x1, x2)
f2=interp1d(x1, x2, kind='cubic')
But f2
throws this error:
ValueError: Expect x to be a 1-D sorted array_like.
But the values are monotonically increasingly as expected by: https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html
What am I doing wrong?
x1
array([1.00020004e+00, 1.00020004e+00, 1.00080064e+00, 1.00200401e+00,
1.00341160e+00, 1.00603622e+00, 1.01173614e+00, 1.02165917e+00,
1.02965404e+00, 1.04362346e+00, 1.05820106e+00, 1.07688994e+00,
1.10107906e+00, 1.12688754e+00, 1.16063138e+00, 1.19161106e+00,
1.23578843e+00, 1.29198966e+00, 1.35062129e+00, 1.42775557e+00,
1.52207002e+00, 1.63345312e+00, 1.77746178e+00, 1.93124759e+00,
2.11954218e+00, 2.37191651e+00, 2.68528464e+00, 2.97973778e+00,
3.38983051e+00, 3.89105058e+00, 4.48430493e+00, 5.31349628e+00,
6.28930818e+00, 7.59878419e+00, 9.38086304e+00, 1.20192308e+01,
1.57232704e+01, 2.06611570e+01, 2.68817204e+01, 3.49650350e+01,
4.58715596e+01, 6.57894737e+01, 8.92857143e+01, 1.38888889e+02,
2.27272727e+02, 4.16666667e+02, 1.00000000e+03, 2.50000000e+03,
2.50000000e+03, 5.00000000e+03])
x2
array([ 11.18083778, 12.00065196, 12.88057749, 13.82502193,
14.838716 , 15.92673731, 17.09453577, 18.34796088,
19.69329106, 21.13726508, 22.68711581, 24.35060647,
26.1360695 , 28.05244828, 30.10934199, 32.31705361,
34.68664159, 37.22997521, 39.95979405, 42.88977178,
46.0345847 , 49.40998519, 53.03288065, 56.9214182 ,
61.09507555, 65.57475857, 70.38290604, 75.54360201,
81.08269642, 87.02793465, 93.40909644, 100.25814508,
107.6093875 , 115.49964612, 123.96844331, 133.05819935,
142.81444487, 153.28604899, 164.52546404, 176.58898835,
189.53704818, 203.43450047, 218.35095766, 234.36113639,
251.54523176, 269.98931903, 289.78578477, 311.03378962,
333.83976499, 358.31794618])
Upvotes: 0
Views: 5783
Reputation: 92440
The test it's doing it is:
np.any(x1[1:] <= x1[:-1])
notice the <=
, which means equal values (like: 2.50000000e+03,, 2.50000000e+03) in your array will cause errors.
Changing these and (there's two more at the start of the array) will make the error go away.
I not sure this is the right way to go about dealing with your data, but you could delete the dupes (and corresponding x2 values) with something like:
x3, ind = np.unique(x1, return_index = True)
x4 = x2[ind]
f2=interp1d(x3, x4, kind='cubic')
Upvotes: 1
Reputation:
Just add this two lines before doing the interpolation:
x1[0] -= 0.0000001
x1[-2] += 0.0000001
Looks like with epsilon does not work
Upvotes: 1