'DataFrame' 对象没有属性 'as_matrix

Lax*_*ava 23 python dataframe pandas

import pandas as pd

from sklearn.model_selection import train_test_split

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn import ensemble

from sklearn.metrics import mean_absolute_error

from joblib import *

df = pd.read_csv('~/Downloads/Melbourne_housing_FULL.csv')

df.head(n=5)
del df['Address']
del df['Method']
del df['SellerG']
del df['Date']
del df['Postcode']
del df['Lattitude']
del df['Longtitude']
del df['Regionname']
del df['Propertycount']
df.dropna(axis=0, how='any', thresh=None, subset=None, inplace=True)
features_df = pd.get_dummies(df, columns=['Suburb', 'CouncilArea', 'Type'])
X = features_df.as_matrix()
y = df['Price'].as_matrix()
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我我在输入 X = features_df.as_matrix() y = df['Price'].as_matrix() 时遇到错误,我正在用 Oliver 的名为 Machine Learning with python 的书学习机器学习...任何帮助都非常感谢谢谢

小智 33

df.as_matrix()在 0.23.0 版本后被弃用。使用df.values来代替。

请点击此链接以获取更多信息。

  • df.values --> 没有括号 (19认同)

Dr_*_*ope 12

Dataframe 贬低了很多属性,例如 .ix

在这里你需要这个命令:

y = df['Price'].values
Run Code Online (Sandbox Code Playgroud)


小智 5

替换.as_matrix().values()也导致错误,但替换为.to_numpy()完美工作

将 DataFrame 转换为 NumPy 数组。

0.24.0 版中的新功能。

  • `.values()` 不会起作用,因为它不是一种方法。请改用“.values”。 (3认同)

jua*_*aza 5

根据文档,从 Pandas 1.0 开始,.to_numpy()不推荐使用其他方式