【问题标题】:Learning curve plot keeping test data constant学习曲线图保持测试数据不变
【发布时间】:2016-10-25 20:13:38
【问题描述】:

首先,我定义了 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)

我的学习曲线如下:

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

【问题讨论】:

  • 您能否提供完整的工作 sn-p,最好使用 sklearn 中提供的样本数据集
  • 我编辑了我的帖子。感谢您的评论 michael_j_ward
  • 通过工作 sn-p,我的意思是我应该能够将 sn-p 复制并粘贴到 python 中并获得您所遇到的结果。我不会立即出现错误,因此它可能在您的分类器设置或绘图功能中 - 所以我需要一个完整的工作 sn-p 才能诊断问题
  • 你的数据不是很不平衡吗,88%的单类? (因此这个结果只是简单的模型说“总是正确的”?)

标签: python for-loop machine-learning


【解决方案1】:
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的数据,结果还可以。图很正常。也许你应该检查你的代码数据和排序数据以绘制图形。

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 2020-11-06
    • 2019-05-04
    • 2014-04-01
    • 2013-06-16
    • 2011-01-21
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多