Programmer
Programmer

Reputation: 8717

Numpy : ValueError: object of too small depth for desired array

I am trying to convert a MATLAB code to Python where I am stuck of how to import this line to Python:

YDFA_xa_p = interp1(data(:,1),data(:,2),YDFA_lam_p*1e9,'linear')*1e-24;

Now for Python I have changed it as:

YDFA_xa_p = numpy.interp(data[:, 1], data[:, 2], YDFA_lam_p * 1e9) * 1e-24

data[:,1] and data[:,2] and YDFA_lam_p values are:

[ 2.  2.  2.  2.  2.  2.  2.  2.  2.  2.] [ 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.] 915.0

The issue I see is that the variable YDFA_lam_p is a float variable while it is expecting an array of float of 10 elements?

If I am correct in my understanding how can I correct it ? I tried ways I find in google but it just isn't working.

Upvotes: 2

Views: 11860

Answers (1)

hpaulj
hpaulj

Reputation: 231510

When I use the same sort of numbers in Octave I get a similar error:

octave:32> interp1([2,2,2,2],[3,3,3,3],900)
warning: interp1: multiple discontinuities at the same X value
error: mkpp: at least one interval is needed

You've given it one point (repeatedly) and are asking it to interpolate some value way off in left field.

A correct sample use is:

octave:32> interp1([1,2,3,4,5],[3,3.5,2,2.5,1],2.33,'linear')
ans =  3.0050

the equivalent Python (note different order of variables):

In [364]: np.interp(2.33,[1,2,3,4,5],[3,3.5,2,2.5,1])
Out[364]: 3.005

Read help(np.interp) to see more about its inputs.

Upvotes: 3

Related Questions