Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Method .as_matrix will be removed in a future version. Use .values instead [duplicate]

I have the following code

train_X, test_X, train_y, test_y = train_test_split(X.as_matrix(), y.as_matrix(), test_size=0.25)

where X is a DataFrame and y is a series. When calling the function above, I get the following warning:

/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.

"""Entry point for launching an IPython kernel.

Then I tried to change using .values as mentioned in the warning:

train_X, test_X, train_y, test_y = train_test_split(X.values(), y.values(), test_size=0.25)

But I get the following error:

TypeError Traceback (most recent call last) in () ----> 1 train_X, test_X, train_y, test_y = train_test_split(X.values(), y.values(), test_size=0.25)

TypeError: 'numpy.ndarray' object is not callable

How do I solve this?

like image 838
rcs Avatar asked Sep 18 '18 08:09

rcs


2 Answers

It should be:

train_X, test_X, train_y, test_y = train_test_split(X.values, y.values, test_size=0.25)

See this.

like image 171
Deepak Saini Avatar answered Oct 14 '22 06:10

Deepak Saini


According to Panda 0.25.1 documentation, they recommend more using DataFrame.to_numpy() than DataFrame.values()

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.values.html#pandas.DataFrame.values

So I'd like to suggest to update it like below:

train_X, test_X, train_y, test_y = train_test_split(X.to_numpy(), y.to_numpy(), test_size=0.25)
like image 29
Bongsang Avatar answered Oct 14 '22 05:10

Bongsang