不同深度值创建决策树回归器模型,learning curve没有用r2,为何提示R^2 score is not well-defined with less than two samples.

困扰ing.qaq
代码如下: 求帮助


predictions = data.values[:, 5]
features = data.values[:, 0:5] 

from sklearn.model_selection import train_test_split  #清洗分割数据
x_train, x_test, y_train, y_test = train_test_split(features, predictions, test_size=0.2, random_state=50)

import matplotlib.pyplot as plt
from sklearn.model_selection import ShuffleSplit, learning_curve
from  sklearn.tree import DecisionTreeRegressor

def ModelLearningGraphMetrics(x, y):
    cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=0)

    train_sizes = np.rint(np.linspace(1, x.shape[0]*0.8-1, 18)).astype(int)   # 返回数据

    fig = plt.figure(figsize=(10, 7))

    for k, depth in enumerate([1, 2, 6, 10]):
        regressor = DecisionTreeRegressor(max_depth=depth)

        sizes, train_scores, test_scores = learning_curve(regressor, x, y,
                                                          train_sizes =train_sizes, cv=cv, scoring=None)

        train_std = np.std(train_scores, axis=1)
        test_std = np.std(test_scores, axis=1)
        train_mean = np.mean(train_scores, axis=1)
        test_mean = np.mean(test_scores, axis=1)

        ax = fig.add_subplot(2, 2, k+1)
        ax.plot(sizes, train_mean, 'o-', color='r', label='Training Score')
        ax.plot(sizes, test_mean, 'o-', color='g', label='Testing Score')
        ax.fill_between(sizes, train_mean-train_std, train_mean+train_std, alpha=0.15, color='r')
        ax.fill_between(sizes, test_mean - test_std, test_mean + test_std, alpha=0.3, color='g')
        ax.set_title('max_depth={}'.format(depth))
        ax.set_xlabel('Number of training points')
        ax.set_ylabel('score')
        ax.set_xlim([0, x.shape[0]*0.5])
        ax.set_ylim([-0.05, 1.05])
    ax.legend(bbox_to_anchor=(1.05, 2.05), loc='upper center', borderaxespad=0.)
    fig.suptitle('performance', fontsize=16, y=1.03)
    fig.tight_layout()
    fig.show()


ModelLearningGraphMetrics(features, predictions)

```