gpt4 book ai didi

python - 未计算 DecisionTreeRegressor 分数

转载 作者:行者123 更新时间:2023-12-01 00:41:02 43 4
gpt4 key购买 nike

我正在尝试使用以下代码计算 DecisionTreeRegressor 的分数:

from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.metrics import accuracy_score
from sklearn import tree

# some features are better using LabelEncoder like HouseStyle but the chance that they will affect
# the target LotFrontage are small so we just use HotEncoder and drop unwanted columns later
encoded_df = pd.get_dummies(train_df, prefix_sep="_", columns=['MSZoning', 'Street', 'Alley',
'LotShape', 'LandContour', 'Utilities',
'LotConfig', 'LandSlope', 'Neighborhood',
'Condition1', 'Condition2', 'BldgType', 'HouseStyle'])
encoded_df = encoded_df[['LotFrontage', 'LotArea', 'LotShape_IR1', 'LotShape_IR2', 'LotShape_IR3',
'LotConfig_Corner', 'LotConfig_CulDSac', 'LotConfig_FR2', 'LotConfig_FR3', 'LotConfig_Inside']]

# imputate LotFrontage with the mean value (we saw low outliers ratio so we gonna use the mean value)
encoded_df['LotFrontage'].fillna(encoded_df['LotFrontage'].mean(), inplace=True)
X = encoded_df.drop('LotFrontage', axis=1)
y = encoded_df['LotFrontage'].astype('int32')
X_train, X_test, y_train, y_test = train_test_split(X, y)
classifier = DecisionTreeRegressor()
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
y_test = y_test.values.reshape(-1, 1)
classifier.score(y_test, y_pred)
print("Accuracy is: ", accuracy_score(y_test, y_pred) * 100)

当开始计算模型的分数时,我收到以下错误:

ValueError: Number of features of the model must match the input. Model n_features is 9 and input n_features is 1 

不确定为什么会发生,因为根据 sklearn docs测试样本的形状为(n_samples, n_features)并且 y_test 确实是这样的形状:

y_test.shape # (365, 1)

真实标签的形状应为(n_samples)或(n_samples, n_outputs),并且y_pred确实是这种形状:

y_pred.shape # (365,)

数据集:https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data

最佳答案

分数函数的第一个参数不应该是测试集的目标值,它应该是测试集的输入值,所以你应该这样做

classifier.score(X_test, y_test)

关于python - 未计算 DecisionTreeRegressor 分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57350239/

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