machine-learning - 回归中的 scikit-learn 交叉验证分数

标签 machine-learning scikit-learn regression cross-validation

我正在尝试构建一个回归模型,对其进行验证和测试,并确保它不会过度拟合数据。到目前为止,这是我的代码:

from pandas import read_csv
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split, cross_val_score, validation_curve
import numpy as np
import matplotlib.pyplot as plt

data = np.array(read_csv('timeseries_8_2.csv', index_col=0))

inputs = data[:, :8]
targets = data[:, 8:]

x_train, x_test, y_train, y_test = train_test_split(
    inputs, targets, test_size=0.1, random_state=2)

rate1 = 0.005
rate2 = 0.1

mlpr = MLPRegressor(hidden_layer_sizes=(12,10), max_iter=700, learning_rate_init=rate1)

# trained = mlpr.fit(x_train, y_train)  # should I fit before cross val?
# predicted = mlpr.predict(x_test)      

scores = cross_val_score(mlpr, inputs, targets, cv=5)
print(scores)

Scores 打印一个由 5 个数字组成的数组,其中第一个数字通常约为 0.91,并且始终是数组中最大的数字。 我在弄清楚如何处理这些数字时遇到了一些困难。那么,如果第一个数字是最大的数字,那么这是否意味着在第一次交叉验证尝试中,模型得分最高,然后随着它不断尝试交叉验证,得分下降?

此外,我应该在调用交叉验证函数之前对数据进行拟合吗?我尝试将其注释掉,它给了我或多或少相同的结果。

最佳答案

交叉验证功能将模型拟合作为操作的一部分执行,因此您手动执行此操作不会获得任何好处:

The following example demonstrates how to estimate the accuracy of a linear kernel support vector machine on the iris dataset by splitting the data, fitting a model and computing the score 5 consecutive times (with different splits each time):

http://scikit-learn.org/stable/modules/cross_validation.html#computing-cross-validated-metrics

是的,返回的数字反射(reflect)了多次运行:

Returns: Array of scores of the estimator for each run of the cross validation.

http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.cross_val_score.html#sklearn.model_selection.cross_val_score

最后,没有理由期望第一个结果是最大的:

from sklearn.model_selection import cross_val_score
from sklearn import datasets
from sklearn.neural_network import MLPRegressor
boston = datasets.load_boston()
est = MLPRegressor(hidden_layer_sizes=(120,100), max_iter=700, learning_rate_init=0.0001)
cross_val_score(est, boston.data, boston.target, cv=5)

# Output
array([-0.5611023 , -0.48681641, -0.23720267, -0.19525727, -4.23935449])

关于machine-learning - 回归中的 scikit-learn 交叉验证分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46866180/

相关文章:

python - 从 Keras 的 imdb 数据集中恢复原始文本

python - sklearn 如何选择精度召回曲线中的阈值步骤?

r - 无法绘制 rlm 对象。非 NA 剩余长度与拟合中使用的情况不匹配

python - 在 python 中使用 SVM 进行回归置信度

machine-learning - 如何解释SoftMax回归中的 "soft"和 "max"?

machine-learning - 如何使用神经网络进行人脸检测?

c# - 为什么在使用 Accord.NET 运行 BackPropagation 时出现 OutofRangeException?

python - 使用 Sci-Kit 的 Count Vectorizer 转换输入以仅匹配词汇表中的精确单词

python - tensorflow 导入导致numpy计算错误

r - 对于这个混合效果模型,什么是最好的 "formula"