gpt4 book ai didi

python - 学习曲线图保持测试数据恒定

转载 作者:行者123 更新时间:2023-11-30 09:54:07 24 4
gpt4 key购买 nike

首先我定义了X和y,下面部分描述。

    from sklearn import svm
from sklearn.cross_validation import train_test_split

X = array([[11.8, 0., 3.4, 5.7, 0., 5.7],
[33.4, 6.8, 0., 5.7, 0., 5.7],
[33.4, 6.8, 0., 5.7, 0., 5.7])

y = array([ 1., 1., 0.])

我正在使用下面代码中创建的字典绘制学习曲线:

#First separation of test data
X_train_prev, X_test_prev, y_train_prev, y_test_prev = train_test_split(X, y, test_size = 0.2)

#storing test and training error in dictionary as a function of decreasing test size
array = np.arange(0.01,0.9,0.025)
dicto = {}


for i in array:
X_train, _, y_train, _ = train_test_split(X_train_prev, y_train_prev, test_size = i)
clf.fit(X_train,y_train)

#use the previous test data...
test = clf.score(X_test_prev, y_test_prev)
train = clf.score(X_train, y_train)
dicto[i] = test, train

print(dicto)

我的学习曲线如下: learning curve

问题在于测试误差与模型无关。这怎么可能?我应该如何更改代码,使测试错误取决于训练的模型?

最佳答案

from sklearn.svm import SVC
from sklearn.datasets import load_iris
data = load_iris()
X = data.data
y = data.target
clf = SVC()
#====
#Your code
#====
test_training_error = dicto.values()
test_training_error_sorted = sorted(test_training_error, key = lambda e:e[0]) #I think this is important.

from matplotlib import pyplot as plt
plt.plot(test_training_error_sorted[0], test_training_error_sorted[1])

我用的是sklearn的数据,结果还可以。图形是正常的。也许您应该检查代码数据和排序数据以绘制图形。

关于python - 学习曲线图保持测试数据恒定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37992551/

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