python - Scikit-learn: How to obtain True Positive, True Negative, False Positive and False Negative -


i new in machine learning , in scikit-learn.

my problem:

(please, correct type of missconception)

i have dataset big json, retrieve , store in trainlist variable.

i pre-process in order able work it.

once have done that, start classification:

  1. i use kfold cross validation method in order obtain mean accuracy , train classifier.
  2. i make predicctions , obtain accuracy , confusion matrix of fold.
  3. after this, obtain true positive(tp), true negative(tn), false positive(fp) , false negative(fn) values. use these paramters obtain sensitivity , specificity , them , total of tps html in order show chart tps of each label.

code:

the variables have moment:

trainlist #it list data of dataset in json form labellist #it list labels of data  

most part of method:

#i transform data json form numerical 1 x=vec.fit_transform(trainlist)  #i scale matrix (don't know why without it, makes error) x=preprocessing.scale(x.toarray())  #i generate kfold in order make cross validation kf = kfold(len(x), n_folds=10, indices=true, shuffle=true, random_state=1)  #i start cross validation train_indices, test_indices in kf:     x_train=[x[ii] ii in train_indices]     x_test=[x[ii] ii in test_indices]     y_train=[listalabels[ii] ii in train_indices]     y_test=[listalabels[ii] ii in test_indices]      #i train classifier     trained=qda.fit(x_train,y_train)      #i make predictions     predicted=qda.predict(x_test)      #i obtain accuracy of fold     ac=accuracy_score(predicted,y_test)      #i obtain confusion matrix     cm=confusion_matrix(y_test, predicted)      #i should calculate tp,tn, fp , fn      #i don't know how continue 

if have 2 lists have predicted , actual values; appears can pass them function calculate tp, fp, tn, fn this:

def perf_measure(y_actual, y_hat):     tp = 0     fp = 0     tn = 0     fn = 0      in range(len(y_hat)):          if y_actual[i]==y_hat[i]==1:            tp += 1     in range(len(y_hat)):          if y_hat[i]==1 , y_actual!=y_hat[i]:            fp += 1     in range(len(y_hat)):          if y_actual[i]==y_hat[i]==0:            tn += 1     in range(len(y_hat)):          if y_hat[i]==0 , y_actual!=y_hat[i]:            fn += 1  return(tp, fp, tn, fn) 

from here think able calculate rates of interest you, , other performance measure specificity , sensitivity.


Comments