attributeerror: dataframe object has no attribute as_matrix : Solved

attributeerror_ dataframe object has no attribute as_matrix

Attributeerror: dataframe object has no attribute as_matrix error occurs because as_matrix() function is deprecated in pandas latest version. Mostly when we try to convert pandas dataframe to NumPy array with as_matrix() function in the recent version we get this error. In this article, we will understand this error in more detail. So let’s start.

 

attributeerror: dataframe object has no attribute as_matrix ( Steps to Fix ) –

In order to understand the root cause, Let’s create a dummy dataframe with some rows, and then we will replicate the same error (dataframe object has no attribute as_matrix ). Once we understand the cause behind this error, we will see the fix in the same section.

Step 1: Create a dummy pandas dataframe-

We will use a python dictionary to create pandas dataframe. Here is the code for this-

import pandas as pd
sample = {'Id':['Abhishek', 'Ankita', 'Sukesh', 'Anuj'],
        'Marks':[21, 20, 17, 11]}
df = pd.DataFrame(sample)
df.head()
pandas dummy dataframe
pandas dummy dataframe

 

Step 2: Replicating root cause –

we will try to convert dataframe to a NumPy array. We will use the as_matrix() function. As per our narrative, It should throw the same error.

arr=df.as_matrix()
attributeerror dataframe object has no attribute as_matrix
attributeerror dataframe object has no attribute as_matrix

 

Step 3 : using .value as solution –

.values is an attribute of dataframe which we can use to convert dataframe to a NumPy array. As an alternative to as_matrix() function.  Let’s see the implementation here.

 

dataframe object has no attribute as_matrix ( Fix)
dataframe object has no attribute as_matrix ( Fix )

Note –

If you want to use as_matrix() function to convert pandas dataframe to NumPy array, Then we need to downgrade the version of pandas to  0.23.0 or lower. Then the same syntax will work for you.

Mostly this dataframe to NumPy conversion is very important in Machine Learning Model training with scikit learn. See most of the function in scikit-learn accepts only NumPy array as a parameter there it is mandatory to perform this conversion. I Hope now this fix or solution is clear to you all ! ! Let us know if you have any doubt related to this topic.

Thanks 

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner