Advertisement
Guest User

ROC

a guest
Dec 6th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | None | 0 0
  1. from scipy.io import arff
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. import sklearn
  6. from   sklearn import metrics as m
  7. from   sklearn.cross_validation import ShuffleSplit
  8. #klasyfikatory
  9. from   sklearn.neighbors        import KNeighborsClassifier
  10. from   sklearn.naive_bayes      import MultinomialNB, GaussianNB
  11. from   sklearn.ensemble         import BaggingClassifier, RandomForestClassifier, AdaBoostClassifier
  12.  
  13. #funkcja do wczytania danych
  14. def load_data(path):
  15.     [data, meta] = arff.loadarff(path)
  16.     n1 = len(data)
  17.     n2 = len(data[0])
  18.     x = np.zeros((n1, n2-1))
  19.     if meta.types()[-1] == "nominal":
  20.         d = np.chararray((n1,1),itemsize=20)
  21.     else:
  22.         d = np.zeros((n1,1))
  23.     d = d.flatten()
  24.     for row in range(n1):
  25.         for col in range(n2):
  26.             if col in range(n2-1):
  27.                 x[row][col] = data[row][col]
  28.         d[row] = data[row][-1]
  29.     return x,d,meta
  30.  
  31. #funkcja wyswietla w konsoli informacje o klasyfikatorze
  32. def print_classifier_stats(name, conf_matrix):
  33.    print('----------------------------')
  34.    print(name)
  35.    print(conf_matrix)
  36.    TN = float(conf_matrix[0][0])
  37.    FP = float(conf_matrix[0][1])
  38.    FN = float(conf_matrix[1][0])
  39.    TP = float(conf_matrix[1][1])
  40.  
  41.    dokl = (TN+TP) / (TN+FP+FN+TP)
  42.    blad = 1 - dokl
  43.    czul = TP / (FN+TP)
  44.    spec = TN / (TN + FP)
  45.    prec = TP / (TP + FN)
  46.    F1   = (2 * czul * prec) / (czul + prec)
  47.    zbal = (czul + spec) / 2
  48.  
  49.    print("dokladnosc : " + str(dokl))
  50.    print("blad : " + str(blad))
  51.    print("czulosc : " + str(czul))
  52.    print("specyficznosc : " + str(spec))
  53.    print("precyzja : " + str(prec))
  54.    print("F1 : " + str(F1))
  55.    print("zbalansowana dokladnosc : " + str(zbal))
  56.    print('----------------------------')
  57.  
  58. #wczytuje dane
  59. x,d,meta = load_data('Dane/diabetes.arff')
  60. #tablica na klasyfikatory
  61. classifiers = [
  62.    KNeighborsClassifier(3),
  63.    MultinomialNB(),
  64.    GaussianNB(),
  65.    BaggingClassifier(),
  66.    RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
  67.    AdaBoostClassifier()
  68. ]
  69. #tablica pomocnicza na nazwy
  70. classifiers_n = [
  71.    "KNeighbors Classifier",
  72.    "MultinomialNC Classifier",
  73.    "GaussianNB Classifier",
  74.    "Baggibg Classifier",
  75.    "Random Forest Classifier",
  76.    "AdaBoost Classifier"
  77. ]
  78. #losowanie tablic do nauki i testow
  79. ss = ShuffleSplit(len(x), 1, 0.5, 0.5)
  80.  
  81. #petla glowna
  82. plt.figure()
  83. for id, classifier in enumerate(classifiers):
  84.    for train_id, test_id in ss:
  85.       classifier.fit(x[train_id][:], d[train_id])
  86.       y = classifier.predict(x[test_id][:])
  87.       conf_matrix = m.confusion_matrix(d[test_id], y)
  88.       #wypisz wyniki klasyfikatora
  89.       print_classifier_stats(classifiers_n[id], conf_matrix)
  90.       #krzywa ROC
  91.       preds = classifier.predict_proba(x[test_id])[:,1]
  92.       r_fpr, r_tpr, _ = m.roc_curve(d[test_id], preds)
  93.    plt.subplot()
  94.    plt.plot(r_fpr, r_tpr, label=classifiers_n[id])
  95.  
  96. plt.title("POROWNANIE KLASYFIKATOROW - KRZYWA ROC")
  97. plt.xlabel('FPR')
  98. plt.ylabel('TPR')
  99. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement