[ad_1]

**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()
```

### 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()`

### 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.

### 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.

[ad_2]

Source link