While working on VIF in multiple regression I came across this problem

0 votes
def get_vif_factors(X):
    X_matrix = X.as_matrix()
    vif = [variance_inflation_factor(X_matrix, i ) for i in range(X_matrix.shape[1])]
    vif_factors = pd.DataFrame()
    vif_factors['column'] = X.columns
    vif_factors['VIF'] = vif

    return vif_factors

And I got a error of-

'DataFrame' object has no attribute 'as_matrix'
Aug 10, 2020 in Python by Pawan
• 120 points
181 views

1 answer to this question.

0 votes

Hello, @Pawan,

The as_matrix method is deprecated since 0.23.0, so you should use to_numpy instead.

I am giving you an example regarding your doubt:

For example let's assume we only need to convert the subset ['col1', 'col2', 'col4'] of our original DataFrame to a Numpy array. In that case, you might have some legacy code relying on as_matrix to convert, which looks more or less like:

df.as_matrix(['col1', 'col2', 'col4'])

While converting the above code to to_numpy you cannot simply replace the function name like in:

df.to_numpy(['col1', 'col2', 'col4'])  # WRONG

because to_numpy does not accept a subset of columns as parameter. The solution, in that case, would be to do the selection first and apply to_numpy to the result, as in:

df[['col1', 'col2', 'col4']].to_numpy()  # CORRECT
answered Aug 10, 2020 by Dhiman

Related Questions In Python

+2 votes
0 answers
0 votes
1 answer

I'm working on a Summer dataset for practice in Python. However I get the below error:

Hi@Pratap, I think to find duplicate values using ...READ MORE

answered Jun 15, 2020 in Python by MD
• 95,140 points
219 views
0 votes
1 answer

How can I print variable and string on same line in Python?

Use , to separate strings and variables while printing: print ...READ MORE

answered Sep 17, 2018 in Python by Priyaj
• 58,100 points
1,997 views
0 votes
2 answers

How can I rename multiple files in a certain directory using Python?

import os from optparse import OptionParser, Option class MyOption ...READ MORE

answered Jul 28, 2020 in Python by The real slim shady
3,265 views
0 votes
1 answer

How do I determine if my python shell is executing in 32bit or 64bit mode on OS X?

UPDATED: One way is to look at sys.maxsize as ...READ MORE

answered Dec 11, 2018 in Python by aryya
• 7,440 points
988 views
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by MD 632 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is "size" includes NaN values, ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by Gitika 1,442 views
0 votes
1 answer

Changing Pandas data-frame to JSON type

abc.to_json(orient='records') This command will give you the desired ...READ MORE

answered May 22, 2018 in Data Analytics by anonymous
1,232 views
0 votes
2 answers

Replacing a row in pandas data.frame

key error. I love python READ MORE

answered Feb 18, 2019 in Data Analytics by anonymous
8,587 views