svmroc.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:aq_weather 作者: eliucidate 项目源码 文件源码
def main():
  plt.figure()
  for j in range(1,6):
    random_state = np.random.RandomState(0)
    X,y = load_file(file_name,j)
    k = 2
    # y = label_binarize(y, classes=[0, 1, 2])
    # n_classes = y.shape[1]
    # print n_classes
    n_classes = 2
    ylabel, ave= transformtolabel(y,k)
    ylabel = np.array(ylabel)
    # ylabel = np.transpose(ylabel)
  # shuffle and split training and test sets
    X_train, X_test, y_train, y_test = train_test_split(X, ylabel, test_size=.5,
                                                        random_state=0)

    # Learn to predict each class against the other
    classifier = OneVsRestClassifier(svm.SVC(kernel='rbf', probability=True,
                                     random_state=random_state))
    y_score = classifier.fit(X_train, y_train).decision_function(X_test)

    # Compute ROC curve and ROC area for each class
    fpr = dict()
    tpr = dict()
    roc_auc = dict()
    for i in range(n_classes):
      # print y_test[i]
      fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
      roc_auc[i] = auc(fpr[i], tpr[i])

    # Compute micro-average ROC curve and ROC area
    fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel())
    roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
    # print fpr[1]

    ##############################################################################

    # Plot of a ROC curve for a specific class

    # plt.figure()
    # plt.plot(fpr[0], tpr[0], label='CO below %0.2f' % ave +' (area = %0.2f)' %roc_auc[0])
    plt.plot(fpr[1], tpr[1], label='O3 prediction (area = %0.2f)' %roc_auc[1]+'(%0.0f'% j+' features)')
  plt.plot([0, 1], [0, 1], 'k--')
  plt.xlim([0.0, 1.0])
  plt.ylim([0.0, 1.0])
  plt.xlabel('False Positive Rate')
  plt.ylabel('True Positive Rate')
  plt.title('Receiver operating characteristic for SVM')
  plt.legend(loc="lower right")
  plt.show()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号