gpt4 book ai didi

pandas - 如何解决此归因错误AttributeError : 'DataFrame' object has no attribute 'as_matrix' (using Python 3.8)

转载 作者:行者123 更新时间:2023-12-03 08:39:49 31 4
gpt4 key购买 nike

大家好,当我在jupyter记事本上运行以下代码时,我指的是这两行(AttributeError:“DataFrame”对象没有属性“as_matrix”)
#创建x和y变量

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.valuesy = df['Price'].values

关于pandas - 如何解决此归因错误AttributeError : 'DataFrame' object has no attribute 'as_matrix' (using Python 3.8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62712910/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com