lucy
lucy

Reputation: 11

TypeError: can't multiply sequence by non-int of type 'numpy.float64' when trying to solve a 2nd degree polynomial

The question requires creating a second degree polynomial fit to some data and then printing out the coefficients. I have to plot the raw data and overlay the polynomial fit in the same plot.

I've done an almost identical question to this before and there was no problem so I'm a bit stuck as to why it isn't working. The error message states:

y1=p[0]*x+p[1] TypeError: can't multiply sequence by non-int of type 'numpy.float64''

I think I understand that this error message is coming up because it doesn't like the [ ] but this is how I did it previously so I don't know? I've messed around with the equation a bit to try and make it work but nothing is making it right.

x=(0.0,0.45,0.89,1.34,1.79,2.24,2.69,3.14,3.59,4.04,4.49,4.94,5.39,5.83,6.28) 
y=(0.36,0.30,0.311,0.09,0.51,0.55,1.10,1.11,1.45,1.74,2.30,2.52,3.26,3.69,4.12)
r,s=st.pearsonr(x,y)
print'The Pearson Correlation Coefficient of this polynomial is',r
p=np.polyfit(x,y,2)
y1=p[0]*x+p[1]
plt.plot(x,y1,'-r')
plt.plot(x,y,'o')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Polynomial')
plt.show()

I expect that a graph will be produced plotting the actual data as well as plotting a straight line over the top, representing the polynomial fit.

Upvotes: 1

Views: 1928

Answers (2)

Sheldore
Sheldore

Reputation: 39072

There are two problems with your approach:

  • You are using (and also want) a second order fit but computing a linear fit from the coefficients.
  • Your x and y values are tuples which cannot be multiplied by floats. You can multiply them by integers but that will repeat the tuple by the integer number of times. This is not what you want.

One solution is to convert your x and y values to a NumPy array which allows vectorized operation and then use a second order equation to compute y1. Below is a complete answer.


import numpy as np
import matplotlib.pyplot as plt

x = np.array([0.0,0.45,0.89,1.34,1.79,2.24,2.69,3.14,3.59,4.04,4.49,4.94,5.39,5.83,6.28]) 
y = np.array([0.36,0.30,0.311,0.09,0.51,0.55,1.10,1.11,1.45,1.74,2.30,2.52,3.26,3.69,4.12])

p = np.polyfit(x,y,2)
y1 = p[0]*x**2+p[1]*x+p[2]
plt.plot(x,y1,'-r')
plt.plot(x,y,'o')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Polynomial')

enter image description here

Better alternative is to use poly1d to create a polynomial from the coefficients. This avoids manually writing the whole equation. You can then use it to evaluate the polynomial for given x values as

p = np.poly1d(np.polyfit(x,y,2))
y1 = p(x)

Upvotes: 3

Lesiak
Lesiak

Reputation: 26064

The problem is in the expression p[0]*x The types of arguments of this exprtession are float and tuple (sequence) respectively. There is no multiplication operation that matches these arguments. The closest match is multiplying a sequence by an integer (which creates a new, longer sequence). This is the reason of the error you see.

You probably want to use a numpy array. Multiplying numpy array by a float multiplies each element. Example:

x = np.array([1, 2, 3])

Upvotes: 0

Related Questions