获取属性错误:'as_matrix' 个错误

Getting Attribute Errors: 'as_matrix' errors

我正在尝试将数据集拆分为 x 和 y 进行训练,但出于某种原因,我不断收到属性错误。

尝试

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)

当我 运行 代码时,这是我得到的错误:

---------------------------------------------------------------------------
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'

还有其他方法可以解决这个问题吗?

如果你看 here。您会发现 as_matrix 已弃用。您可以改用 .values

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