繁体   English   中英

“标量变量的无效索引”-使用Scikit时学习“ accuracy_score”

[英]“Invalid Index to Scalar Variable” - When Using Scikit Learn “accuracy_score”

不确定到底出了什么问题。 但是,我的目标是建立一个交叉验证python代码。 我知道有各种指标,但是我认为我使用的是正确的指标。 我没有得到我想要的CV10结果,而是收到一个错误:

“标量变量的无效索引”

我在StackOverflow上发现了此问题: IndexError:当您尝试索引numpy标量(例如numpy.int64或numpy.float64)时,对标量变量的索引无效。 它与TypeError非常相似:当您尝试为一个int索引时,“ int”对象没有属性“ _ getitem _”。

任何帮助,将不胜感激...

我正在尝试遵循:: http://scikit-learn.org/stable/modules/model_evaluation.html

from sklearn.ensemble import RandomForestClassifier
from sklearn import cross_validation
from numpy import genfromtxt
import numpy as np
from sklearn.metrics import accuracy_score

def main():
    #read in  data, parse into training and target sets
    dataset = genfromtxt(open('D:\\CA_DataPrediction_TrainData\\CA_DataPrediction_TrainDataGenetic.csv','r'), delimiter=',', dtype='f8')[1:]   
    target = np.array( [x[0] for x in dataset] )
    train = np.array( [x[1:] for x in dataset] )

    #In this case we'll use a random forest, but this could be any classifier
    cfr = RandomForestClassifier(n_estimators=10)

    #Simple K-Fold cross validation. 10 folds.
    cv = cross_validation.KFold(len(train), k=10, indices=False)

    #iterate through the training and test cross validation segments and
    #run the classifier on each one, aggregating the results into a list
    results = []
    for traincv, testcv in cv:
        pred = cfr.fit(train[traincv], target[traincv]).predict(train[testcv])
        results.append(accuracy_score(target[testcv], [x[1] for x in pred]) )

    #print out the mean of the cross-validated results
    print "Results: " + str( np.array(results).mean() )

if __name__=="__main__":
    main()

您的pred变量只是预测的列表,因此您无法为其元素编制索引(这就是错误的原因)

results.append(accuracy_score(target[testcv], [x[1] for x in pred]) )

应该

results.append(accuracy_score(target[testcv], pred) )

或者如果您真的想要副本

results.append(accuracy_score(target[testcv], [x for x in pred]) )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM