3

I have a data set that has one set of x values and many sets of y values that have the same set of x values. For instance,

X     Y2     Y3
0     2     1     
2     4     15
4     7     20
6     4     30
8     5     10
10     0.2     1 
12     0.7     2
14     1     1
16     1.2     10
18     2.4     11
20     2.5     5
22     3     6
24     1     7
26     8     7
28     9     5
30     1     1.2
32     1.2     1.5
34     1.5     2

I want to get Y2 and Y3 values for "new_x" by using interpolation. new_x=np.arange(0,34,0.424).

array([ 0.   ,  0.424,  0.848,  1.272,  1.696,  2.12 ,  2.544,  2.968,
        3.392,  3.816,  4.24 ,  4.664,  5.088,  5.512,  5.936,  6.36 ,
        6.784,  7.208,  7.632,  8.056,  8.48 ,  8.904,  9.328,  9.752,
       10.176, 10.6  , 11.024, 11.448, 11.872, 12.296, 12.72 , 13.144,
       13.568, 13.992, 14.416, 14.84 , 15.264, 15.688, 16.112, 16.536,
       16.96 , 17.384, 17.808, 18.232, 18.656, 19.08 , 19.504, 19.928,
       20.352, 20.776, 21.2  , 21.624, 22.048, 22.472, 22.896, 23.32 ,
       23.744, 24.168, 24.592, 25.016, 25.44 , 25.864, 26.288, 26.712,
       27.136, 27.56 , 27.984, 28.408, 28.832, 29.256, 29.68 , 30.104,
       30.528, 30.952, 31.376, 31.8  , 32.224, 32.648, 33.072, 33.496,
       33.92 ])

When I run intrp_CC=np.interp(new_x,old_x,current_Y), I get ValueError: object too deep for desired array.

Any idea as to why this is happening and how I can fix it?

0

2 Answers 2

2

Did you read the docs?

Returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.

fp : 1-D sequence of float or complex

With a 1d fp:

In [397]: np.interp(np.arange(10), np.arange(10), np.arange(20).reshape(2,10)[0,:])
Out[397]: array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])

With a 2d, multirow, fp - your error:

In [398]: np.interp(np.arange(10), np.arange(10), np.arange(20).reshape(2,10))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-398-e1383ba2f0f0> in <module>()
----> 1 np.interp(np.arange(10), np.arange(10), np.arange(20).reshape(2,10))

/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in interp(x, xp, fp, left, right, period)
   1306         fp = np.concatenate((fp[-1:], fp, fp[0:1]))
   1307 
-> 1308     return interp_func(x, xp, fp, left, right)
   1309 
   1310 

ValueError: object too deep for desired array
Sign up to request clarification or add additional context in comments.

1 Comment

My bad. What I end up having to do is to create a for loop to do interpolate one y for one x for all sets of y values.
2

I think that it is still a fair question to ask if the explicit for loop is in any way avoidable..... The answer to that question is yes, provided one uses scipy interpolate.interp1d module instead of numpy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.