• Register
1 vote


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))


The error I am getting is following below

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


12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote


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


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

how_many_to_change = np.sum(Y)/2


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



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 38 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, 2020 LizzyM 6.1k points
0 votes
1 answer 5 views
Problem: TypeError: ‘float’ object cannot be interpreted as an integer Floating-point numbers are values that can contain a decimal point. Integers are whole numbers. It is common in programming for these two data types to be distinct.How to solve this error
asked Jan 6 Mashhoodch 5.6k points
0 votes
1 answer 10 views
Problem: Hi there! I am a noob and I have got an assignment in which I have to take two numbers as input from users and print the even numbers that lie between two numbers. I wrote the following program: starting = input("Enter the starting number: ") ending = ... : TypeError: 'str' object cannot be interpreted as an integer I have no idea what is wrong with my program. Can anybody here help me?
asked Dec 21, 2020 Code Learner 5.3k points
2 votes
1 answer 953 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, 2020 Gavin 15.3k points
0 votes
2 answers 1.1K views
Problem : I am facing following type error while trying to execute the Python program deprecationwarning: object of type <class 'float'> cannot be safely interpreted as an integer.
asked Nov 14, 2019 peterlaw 6.9k points
1 vote
1 answer 81 views
Problem: Here is my sample code below from turtle import * speed(0) hideturtle() c = 450 def grid(x,y,a): seth(0) pu() goto(x,y) pd() for i in range(4): forward(a) rt(90) for i in range(c/10): seth(0) forward(10) rt(90) forward(c) backward(c ... \Desktop\gridv2.py", line 15, in grid for i in range(c/10): TypeError: 'float' object cannot be interpreted as an integer What&rsquo;s wrong with my code?
asked Mar 7, 2020 Gavin 15.3k points
0 votes
1 answer 17 views
Task: Read an integer N . For all non-negative integers i<N , print i2. if __name__ == '__main__': n = int(input()) if (n<=0) and (n>=20): print("Exit") else: for(i=0,i<=n,i++): print(i*i) I am getting below error File "solution.py", line 6 for(i=0,i<=n,i++): ^ SyntaxError: invalid syntax Exit Status 255
asked Oct 25, 2020 psandprop 2.4k points