pandas - 如何解决此归因错误 AttributeError: 'DataFrame' object has no attribute 'as_matrix' (使用 Python 3.8)
问题描述
大家好,当我在 jupyter 记事本上运行以下代码时,我得到了(AttributeError: 'DataFrame' object has no attribute 'as_matrix'),引用了这两行 #create x & y variables
X = features_df.as_matrix()
y = df['Price'].as_matrix()
我的整个代码如下
#developing model to predict houses prices in Australia
#importing needed libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import ensemble
from sklearn.metrics import mean_absolute_error
import sklearn.externals
# importing the file path
df = pd.read_csv('~/mypython/machine_learning/machine_learning/housing/Melbourne_housing_FULL.csv')
#removing less related dimentions
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']
#delete raws with any empty value
df.dropna(axis = 0 ,how = 'any' ,thresh = None, subset = None, inplace = True)
#converting non-numerical values to numerical values using pandas
features_df = pd.get_dummies(df, columns=['Suburb', 'CouncilArea', 'Type'])
# delete price because it's the dependant varilable
del features_df['Price']
#create x & y variables
X = features_df.as_matrix()
y = df['Price'].as_matrix()
X_train, X_test, y_train, y_test=train_test_split(X, y, test_size=0.3,random_state=0)
model = ensembel.GradientBoostingRegressor(
n_estimators=150,
learning_rate=0.1,
max_depth=30,
min_sample_split= 4,
min_samples_leaf=6,
max_features=0.6,
loss="huber")
model.fit(X_train,y_train)
joblib.dumb(model,"house_train_model.pkl")
mse=mean_absolute_error(y_train_model, model.predict(X_train))
print("Training set mean absolute error:%.2f"%mse)
解决方案
你应该使用这个
X = features_df.values
y = df['Price'].values
推荐阅读
- java - 当应用程序被杀死或不在后台Android 8.0,9.0中时如何检测来电号码和去电号码
- javascript - TypeError:response.json 不是函数。(在 'response.json()' 中,'response.json' 未定义)
- mongodb - MongoDB 客户端字段级加密 (CSFLE) 限制对未加密集合的操作
- android - Android 问题:: 发现多个文件与操作系统无关路径 'androidsupportmultidexversion.txt' “错误”
- python-3.x - 从谷歌下载多张图片
- swift - Swift Combine:在订阅级别或接收级别处理异步?
- 3d - 向 3D 模型添加自定义属性?
- mysql - 为什么从 Python 的 unittest 运行时写入 MySQL 无法持久?
- linux - 使用 Shell 脚本在日志文件中提取具有自己时间戳的不可预测数据
- keycloak - Keycloak :: 您的登录尝试超时。登录将从头开始