Unable to Create Polynomial Features for regression using numpy.plyfit -- AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy'

39 views Asked by At

Excuse my ignorance I have never used polynomial regression with numpy before.

I am attempting to use the variable "CPU_frequency" to create Polynomial features. Using three different values of polynomial degrees.

import numpy

X = X.to_numpy().flatten()
f1 = np.polyfit(X, Y, 1)
p1 = np.poly1d(f1)

f3 = np.polyfit(X, Y, 3)
p3 = np.poly1d(f3)

f5 = np.polyfit(X, Y, 5)
p5 = np.poly1d(f5)

yields

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[74], line 3
      1 import numpy
----> 3 X = X.to_numpy().flatten()
      4 f1 = np.polyfit(X, Y, 1)
      5 p1 = np.poly1d(f1)

AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy'

attempted change from X. to X._ no joy. reminded jupyter it was using numpy by calling import.

any help appreciated.

2

There are 2 answers

0
Talha Tayyab On

if you need to flatten you can directly use flatten() on numpy.ndarray object

X = X.flatten()

Link to doc: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flatten.html

0
Englishman Bob On

DO NOT VOTE UP RESPONSE MAIN WORK DONE BY @wayne above:

Did you run print(type(X)) on X prior to that line that gave an error? Given you got an error of AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy', X appears to already be numpy object. Also, I don't think the to_numpy comes from numpy. Pandas has one. Polars has one. This drops us in the end of the problem and so we cannot provide much help without more code upstream.

solution

reload data set.

df = pd.read_csv(file_name, header=0)

[str(i) for i in ([["CPU_frequency"]])]

code above resolves.