1

I was trying to run a script which is written below. The focus is to print 'rab'.

from numpy import *
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate


x = [64, 67, 70.459, 73, 76]
y = [0.697, 0.693, 0.694, 0.698, 0.702]
z = [0.748, 0.745, 0.747, 0.757, 0.763]


delT = 1

f = open('volume-temperature.dat', 'r')
V = []
for line in f:
        parts = line.split()
        if len(parts) > 1:
                #print parts[1]
                V.append(parts[1])
f.close()

for M in range(0,5):
        T = 0+M*delT
        if M == 0:
            rab = np.interp(V[M], x, y)
            print rab
        else:
            print M

The problem is while printing 'rab' I am geting this error:

return compiled_interp(x, xp, fp, left, right)
ValueError: object of too small depth for desired array

It looks some fundamental error, But as I am new in Python, so a little help would be appreciated. N.B. V[M] for M = 0 is 70.31

7
  • 6
    Aside: Since you're new I'll give you an important warning: don't use star imports like from numpy import *. While it can be convenient sometimes it replaces certain built-in functions like sum with numpy's versions which behave very differently (i.e. can give the opposite results) in some circumstances. Commented Sep 18, 2014 at 17:05
  • I would add to DSM not to import it twice use either from numpy import * or import numpya as np Commented Sep 18, 2014 at 17:06
  • I tried with your suggestions, but problem remains same. Commented Sep 18, 2014 at 17:10
  • If I just put the 70.31 instead of V[M], then it is working. But since I have a series of values so, in any case I have to use V[M]. Commented Sep 18, 2014 at 17:12
  • @TanmoyChakraborty what happens when you print V[M] after you loop through the file? Also, I would advise learning how to use the "with as" file iterator, because it's much easier and cleaner :-) (the info in the link is towards the bottom of that section) Commented Sep 18, 2014 at 17:37

1 Answer 1

3

Your list V is a list of strings. You must convert these values to floating point numbers before passing them to np.interp.

You could change this:

        V.append(parts[1])

to this:

        V.append(float(parts[1]))
Sign up to request clarification or add additional context in comments.

1 Comment

Many many thanks Warren for the correct solution. It's working now. Thanks!!

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.