• Register
1 vote
1.6k views

Problem:

Hello, I am not extremely versed in numpy, so I am not exactly sure what I am doing wrong. I got it to work with made-up data but I am not sure as to what it is not working with the data I put in. All I am trying to do is fit the data I have, with the function. I am getting a result but I am not sure as to why I cannot plot the result.

x1 = [0 ,2 ,4 ,6 ,8 ,10 ,12 ,14 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 ,32 ,34 ,36 ,38 ,40 ,42 ,44 ,46]
x = np.array(x1)

y1data = [82.3 ,78.5 ,74.3 ,70.7 ,67.6 ,65 ,62.5 ,60.1 ,58.1 ,56.1 ,54.3 ,52.8 ,51.2 ,49.9 ,48.6 ,47.2 ,46.1 ,45 ,43.9 ,43 ,41.9 ,41 ,40.1 ,39.5]
y1 = np.array(y1data)

y2data = [68.8 ,64.8 ,62.1 ,59.9 ,57.7 ,55.9 ,53.9 ,52.3 ,50.8 ,49.5 ,48.1 ,46.8 ,45.9 ,44.8 ,43.7 ,42.6 ,41.7 ,40.8 ,39.9 ,39.3 ,38.6 ,37.7 ,37 ,36.4]
y2 = np.array(y2data)

def func(t, T, r, Ts):
    return (T - Ts) * np.exp(-r * t) + Ts
xdata = x1
y1data = y1
y2data = y2

popt, pcov = curve_fit(func, xdata, y1data)

plt.plot(xdata, y1data, 'b-', label='black coffee')

plt.plot(xdata, func(xdata, *popt), 'r-',
         label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))

plt.xlabel('t')
plt.ylabel('T')
plt.legend()
plt.show()

The error I am getting is following below

TypeError: 'numpy.float64' object cannot be interpreted as an integer

 

12 7 7
15,250 points

2 Answers

1 vote

Solution:

You created x as the numpy version of an array, so when passing that data to a numpy function (using xdata to represent it), you probably want to use the numpy data type.

As I can understand by looking into your program, you messed up on the line

xdata = x1

You probably be wanting xdata = x, not xdata = x1. I am not so sure. As far as I can understand

x = np.array(x1)
#later on: 
xdata = x1

this block producing the error. That’s why you should be using x since it is the numpy array. Seems to work once that is changed.

13 9 6
94,240 points
0 votes

Solution:

The first thing you can do Print the value of how_many_to_change. It is a float value and hence you are getting the error. Make the following change in your code and it should work
Change 

how_many_to_change = np.sum(Y)/2

to

how_many_to_change = int(np.sum(Y)/2)

 

or

N=np.floor(np.divide(l,delta))
...
for j in range(N[i]/2):

N[i]/2 will be a float64 but range() expects an integer. Just cast the call to

for j in range(int(N[i]/2)):

It is caused by unsupported float index in 1.12.0 and newer numpy versions even if the code should be considered as valid.

An int type is expected, not a np.float64

For soving this install 

numpy 1.11.0
sudo pip install -U numpy==1.11.0.

 

10 6 4
31,120 points

Related questions

2 votes
1 answer 34 views
34 views
Problem: I am a python newbee. I was trying some code : x = int(input("Input an Integer:")) res = [x] while x != 1: ---> for i in range(2, a): if x % i == 0: res.append(i) x = x / i break print(res) but this code shows some error ... did not compile. The error message is : TypeError: 'float' object cannot be interpreted as an integer Can any one tell me why am I getting this kind of error message?
asked Mar 23 LizzyM 6.1k points
2 votes
1 answer 632 views
632 views
Problem: I am trying a run a program with a simple list object in my code. But it throwing the same error again and again. The error is: list object cannot be interpreted as an integer python. I am attaching my code snippet below: def userNum( ... winsound.PlaySound("SystemExit", winsound.SND_ALIAS) What could be the possible solution for this program? If possible figure the error, please. Thanks
asked Mar 26 Gavin 15.3k points