Getting Attribute Errors: 'as_matrix' errors

Issue

I’m trying to split the dataset into x and y for training and for some reason I kept getting an attribute errors.

Attempt

import pandas as pd

link = 'http://archive.ics.uci.edu/ml/machine-learning-databases/haberman/haberman.data'
df = pd.read_csv(link, names=['Age', 'YearOfExpereince', 'NodeNums', 'Class'], header = None)

from sklearn.model_selection import train_test_split

X = df.as_matrix(['Age', 'NodeNums'])
y = df['Class'].values.tolist()

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 99)

When I run the code, this is the error i get:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-432c2506c3cb> in <module>
      1 from sklearn.model_selection import train_test_split
      2 
----> 3 X = df.as_matrix(['Age', 'NodeNums'])
      4 y = df['Class'].values.tolist()
      5 

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5476         ):
   5477             return self[name]
-> 5478         return object.__getattribute__(self, name)
   5479 
   5480     def __setattr__(self, name: str, value) -> None:

AttributeError: 'DataFrame' object has no attribute 'as_matrix'

Is there any other way that I can fix the problem?

Solution

If you look here. You will find as_matrixis deprecated. You can use .values instead.

X = df[['Age', 'NodeNums']].values

Answered By – Phoenix

Answer Checked By – Cary Denson (AngularFixing Admin)

Leave a Reply

Your email address will not be published.