我正在尝试从cross_validation.cross_val_score生成R平方值,该值约为0.35,然后将模型应用于同一火车数据集,并使用“ r2_score”函数生成R平方值,该值约为0.87。我不知道给我两个结果有如此大的差异。任何帮助将不胜感激。代码附在下面。
num_folds = 2
num_instances = len(X_train)
scoring ='r2'
models = []
models.append(('RF', RandomForestRegressor()))
results = []
names = []
for name, model in models:
kfold = cross_validation.KFold(n=num_instances, n_folds=num_folds, random_state=seed)
cv_results = cross_validation.cross_val_score(model, X_train, Y_train, cv=kfold,
scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
model.fit(X_train, Y_train)
train_pred=model.predict(X_train)
r2_score(Y_train, train_pred)