用于创建字典的循环

时间:2016-06-20 14:28:55

标签: python dictionary pandas scikit-learn

我正在尝试使用字典中相应的test_size存储训练和测试错误,因为我想创建一个测试/训练错误数字。不幸的是,for循环不起作用。有谁知道我做错了什么? (而不是将它们存储在熊猫df中的字典也很好)。

array = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
dicto = {}

for i in array: 
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = i)
    clf.fit(X_train,y_train)   
    test = clf.score(X_test, y_test) 
    train = clf.score(X_train, y_train)
    dicto[i, test, train]

print(dicto)

我收到以下错误:

  

KeyError:(0.1,0.89473684210526316,0.91176470588235292)

2 个答案:

答案 0 :(得分:2)

您永远不会在字典中分配值:

dicto[i, test, train] # This is trying to lookup a key of (i, test, train), which doesn't exist yet.

请改为尝试:

dicto[i] = (test, train) # map test and train variables to test_size, which is i

答案 1 :(得分:0)

那就是你想要的吗?

UNNotificationServiceExtensions