我正在使用以下数据集进行 Kaggle 比赛:https : //www.kaggle.com/c/home-data-for-ml-course/download/train.csv
根据该理论,通过增加随机森林模型中估计量的数量,平均绝对误差只会下降到某个数字(最佳点),进一步增加会导致过度拟合。通过绘制估计量的数量和平均绝对误差,我们应该得到这个红色图表,最低点标志着估计量的最佳数量。
我尝试使用以下代码找到最佳估计器数量,但数据图显示 MAE 不断下降。我做错了什么?
train_data = pd.read_csv('train.csv')
y = train_data['SalePrice']
#for simplicity dropping all columns with missing values and non-numerical values
X = train_data.drop('SalePrice', axis=1).dropna(axis=1).select_dtypes(['number'])
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
mae_list = []
for n_estimators in range(10, 800, 10):
rf_model = RandomForestRegressor(n_estimators=n_estimators, random_state=0, n_jobs=8)
rf_model.fit(X_train, y_train)
preds = rf_model.predict(X_test)
mae = mean_absolute_error(y_test, preds)
mae_list.append({'n_est': n_estimators, 'mae': mae})
#plotting the results
plt.plot([item['n_est'] for item in mae_list], [item['mae'] for item in mae_list])
ITMISS
相关分类