在sklearn的Boston的帮助下,我尝试对RandomForestRegressor数据集使用随机森林算法来预测房价medv
。
下面是我的训练/测试数据拆分:
'''Train Test Split of Data'''
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 1)
Dimensions of Train/Test split
X.shape: (489, 11)
X_train.shape: (366, 11)
X_test.shape: (123, 11)
下面是我的调整随机森林模型:
#1. import the class/model
from sklearn.ensemble import RandomForestRegressor
#2. Instantiate the estimator
RFReg = RandomForestRegressor(max_features = 'auto', random_state = 1, n_jobs = -1, max_depth = 14, min_samples_split = 2, n_estimators = 550)
#3. Fit the model with data aka model training
RFReg.fit(X_train, y_train)
#4. Predict the response for a new observation
y_pred = RFReg.predict(X_test)
y_pred_train = RFReg.predict(X_train)
只需评估我使用以下代码尝试sklearn的learning curve的模型的效果如何
train_sizes = [1, 25, 50, 100, 200, 390] # 390 is 80% of shape(X)
from sklearn.model_selection import learning_curve
def learning_curves(estimator, X, y, train_sizes, cv):
train_sizes, train_scores, validation_scores = learning_curve(
estimator, X, y, train_sizes = train_sizes,
cv = cv, scoring = 'neg_mean_squared_error')
#print('Training scores:\n\n', train_scores)
#print('\n', '-' * 70) # separator to make the output easy to read
#print('\nValidation scores:\n\n', validation_scores)
train_scores_mean = -train_scores.mean(axis = 1)
print(train_scores_mean)
validation_scores_mean = -validation_scores.mean(axis = 1)
print(validation_scores_mean)
plt.plot(train_sizes, train_scores_mean, label = 'Training error')
plt.plot(train_sizes, validation_scores_mean, label = 'Validation error')
plt.ylabel('MSE', fontsize = 14)
plt.xlabel('Training set size', fontsize = 14)
title = 'Learning curves for a ' + str(estimator).split('(')[0] + ' model'
plt.title(title, fontsize = 18, y = 1.03)
plt.legend()
plt.ylim(0,40)
如果您注意到我已经通过X, y
而不是X_train, y_train
到learning_curve
。
我对learning_curve
train subset
是正确的还是不正确的测试数据集的大小是否根据列表train_sizes
中所述的火车数据集的大小而变化,或者始终是固定的(根据火车/测试划分,在我的情况下为25%例如123个样本)
train dataset size = 1
时,测试数据大小将为488还是123(X_test的大小)train dataset size = 25
时,测试数据大小将为464还是123(X_test的大小)train dataset size = 50
时,测试数据大小将为439还是123(X_test的大小)我对learning_curve
函数中的训练/测试的大小感到困惑
答案 0 :(得分:0)
您绝对只想使用训练测验,因此以这种方式调用该函数,原因是您希望了解实际使用的数据是如何进行学习的:
public Task<TurnBasedMatch> acceptInvitation (String invitationId)