Jump to content

'Numpy.float64' object cannot be interpreted as an interger

NoLoMo
Go to solution Solved by elpiop,

im guessing you want xdata = x, not xdata = x1? not sure if that will make a difference.

Hello I am not extremely versed with 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.

 

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

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):
  #This is line 44
    return (T - Ts) * np.exp(-r * t) + Ts
xdata = x1
y1data = y1
y2data = y2

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

#Up to this point I think it's working fine
#print(popt)

plt.plot(xdata, y1data, 'b-', label='black coffee')
#plt.plot(xdata, y2data, 'g-', label='cream coffee')

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

#plt.plot(xdata, func(xdata, *popt), 'r-', label= 'fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
#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 the following
#Traceback (most recent call last):
#  File "cooling-data.py", line 57, in <module>
#	plt.plot(xdata, func(xdata, *popt), 'r-',
#  File  "cooling-data.py", line 44, in func
#	return (T - Ts) * np.exp(-r * t) + Ts
#TypeError: 'numpy.float64' object cannot be interpreted as an integer

All the commented out sections is me trying out different things.

Link to comment
Share on other sites

Link to post
Share on other sites

im guessing you want xdata = x, not xdata = x1? not sure if that will make a difference.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, elpiop said:

im guessing you want xdata = x, not xdata = x1.

Why would that be an issue?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, NoLoMo said:

Why would that be an issue?

Coming from someone who barely uses python and develops the platform on which people run numpy code, not actually being someone who writes it, my guess is that 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, since it is probably having troubles interpreting your standard list.

Main Rig: R9 5950X @ PBO, RTX 3090, 64 GB DDR4 3666, InWin 101, Full Hardline Watercooling

Server: R7 1700X @ 4.0 GHz, GTX 1080 Ti, 32GB DDR4 3000, Cooler Master NR200P, Full Soft Watercooling

LAN Rig: R5 3600X @ PBO, RTX 2070, 32 GB DDR4 3200, Dan Case A4-SFV V4, 120mm AIO for the CPU

HTPC: i7-7700K @ 4.6 GHz, GTX 1050 Ti, 16 GB DDR4 3200, AliExpress K39, IS-47K Cooler

Router: R3 2200G @ stock, 4GB DDR4 2400, what are cases, stock cooler
 

I don't have a problem...

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, NoLoMo said:

Why would that be an issue?

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

You should be using x, since it is the numpy array. Seems to work once that is changed.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, elpiop said:

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

You should be using x, since it is the numpy array. Seems to work once that is changed.

Yup it did, wow... I was stuck on this for a while.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, tarfeef101 said:

Coming from someone who barely uses python and develops the platform on which people run numpy code, not actually being someone who writes it, my guess is that 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, since it is probably having troubles interpreting your standard list.

Yea... missed that small detail.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, elpiop said:

im guessing you want xdata = x, not xdata = x1.

You still around? I may need help on one more thing.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, NoLoMo said:

You still around? I may need help on one more thing.

What is it?

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, elpiop said:

What is it?

Was having some issues but I think I am in the right track. So never mind for now, still havent gotten it right but I got a different error code.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×