python - 学习曲线

标签 python machine-learning scikit-learn random-forest

我在 Boston 上尝试随机森林算法借助 sklearn 的 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)

训练/测试拆分的维度 X.形状:(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 的问题

  1. 我只是不明白传递整个数据集而不是只传递train subset是否正确
  2. 测试数据集的大小是根据列表 train_sizes 中提到的训练数据集的大小而变化还是始终固定(根据训练,在我的情况下为 25%/test split 是 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 函数中训练/测试的大小感到有点困惑

最佳答案

你肯定只想使用你的训练测试,所以这样调用这个函数,原因是你想看看你实际使用的数据是如何进行学习的:

learning_curves(estimator=RFReg, X=X_train, y=y_size, train_sizes= train_sizes)

关于python - 学习曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53596459/

相关文章:

python-3.x - Python3 : ValueError: too many values to unpack (expected 2)

python - 按复杂标准合并/加入 2 个 DataFrame

Python string.strip 剥离太多字符

python - 如何使用SIFT从数据集中提取相同大小的特征向量?

python - 使用 Mysqldb 在 Python 中对大型 SQL 语句进行 Econding/char 转义

machine-learning - keras 中使用类权重的 U-net 的自定义损失函数 : `class_weight` not supported for 3+ dimensional targets

opencv - 图像未根据使用 imagedatagenerator 和 flow 定义的范围创建

python - 用于 scikit 学习中 10 折交叉验证的混淆矩阵

python - 将 scikit 缩放数据映射回 ID

python - 从python文件中的行中删除整数值