gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:43:22 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com