python - "Invalid Index to Scalar Variable" - When Using Scikit Learn "accuracy_score" -


not sure wrong exactly. however, goal establish cross-validtion python code. know there various metrics, think using correct one. instead of getting desired cv10 result receiving error:

"invalid index scalar variable"

i found on stackoverflow: indexerror: invalid index scalar variable happens when try index numpy scalar such numpy.int64 or numpy.float64. similar typeerror: 'int' object has no attribute '_getitem_' when try index int.

any appreciated...

i trying follow :: http://scikit-learn.org/stable/modules/model_evaluation.html

from sklearn.ensemble import randomforestclassifier sklearn import cross_validation numpy import genfromtxt import numpy np sklearn.metrics import accuracy_score  def main():     #read in  data, parse training , target sets     dataset = genfromtxt(open('d:\\ca_dataprediction_traindata\\ca_dataprediction_traindatagenetic.csv','r'), delimiter=',', dtype='f8')[1:]        target = np.array( [x[0] x in dataset] )     train = np.array( [x[1:] x in dataset] )      #in case we'll use random forest, 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 training , test cross validation segments ,     #run classifier on each one, aggregating results list     results = []     traincv, testcv in cv:         pred = cfr.fit(train[traincv], target[traincv]).predict(train[testcv])         results.append(accuracy_score(target[testcv], [x[1] x in pred]) )      #print out mean of cross-validated results     print "results: " + str( np.array(results).mean() )  if __name__=="__main__":     main() 

your pred variable list of predictions, cannot index elements (this reason error)

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

should be

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

or if want copy

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

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -