Analiza danych i uczenie maszynowe w Python

Autor notebooka: Jakub Nowacki.

Klasyfikacja

Zadaniem klasyfikacji w uczeniu maszynowym jest przewidzenie dyskretnych klas na podstawie podanych cech. Do zilustrowania działania klasyfikacji wykorzystamy klasyczny zbiór parametrów irysów. Poniżej wczytujemy dane z dostępnych bibliotek.


In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model, neighbors, svm, tree, datasets
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import roc_curve, roc_auc_score, classification_report
%matplotlib inline

plt.rcParams['figure.figsize'] = (10, 8)

iris_ds = datasets.load_iris()
print(iris_ds.DESCR)


Iris Plants Database
====================

Notes
-----
Data Set Characteristics:
    :Number of Instances: 150 (50 in each of three classes)
    :Number of Attributes: 4 numeric, predictive attributes and the class
    :Attribute Information:
        - sepal length in cm
        - sepal width in cm
        - petal length in cm
        - petal width in cm
        - class:
                - Iris-Setosa
                - Iris-Versicolour
                - Iris-Virginica
    :Summary Statistics:

    ============== ==== ==== ======= ===== ====================
                    Min  Max   Mean    SD   Class Correlation
    ============== ==== ==== ======= ===== ====================
    sepal length:   4.3  7.9   5.84   0.83    0.7826
    sepal width:    2.0  4.4   3.05   0.43   -0.4194
    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)
    petal width:    0.1  2.5   1.20  0.76     0.9565  (high!)
    ============== ==== ==== ======= ===== ====================

    :Missing Attribute Values: None
    :Class Distribution: 33.3% for each of 3 classes.
    :Creator: R.A. Fisher
    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
    :Date: July, 1988

This is a copy of UCI ML iris datasets.
http://archive.ics.uci.edu/ml/datasets/Iris

The famous Iris database, first used by Sir R.A Fisher

This is perhaps the best known database to be found in the
pattern recognition literature.  Fisher's paper is a classic in the field and
is referenced frequently to this day.  (See Duda & Hart, for example.)  The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant.  One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.

References
----------
   - Fisher,R.A. "The use of multiple measurements in taxonomic problems"
     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
     Mathematical Statistics" (John Wiley, NY, 1950).
   - Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis.
     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.
   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
     Structure and Classification Rule for Recognition in Partially Exposed
     Environments".  IEEE Transactions on Pattern Analysis and Machine
     Intelligence, Vol. PAMI-2, No. 1, 67-71.
   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions
     on Information Theory, May 1972, 431-433.
   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II
     conceptual clustering system finds 3 classes in the data.
   - Many, many more ...

Zadanie

  1. Zamień zbiór na DataFrame iris potrzebny do późniejszej klasyfikacji:
    • nazwę kolumn cech pobierz ze listy cech;
    • usuń z nazw kolumn jednostki w nawiasie;
    • zamień spacje w nazwie na podkreślnik _
    • zmienna opisywaną nazwij iris_class.

Użyjemy funkcji train_test_split do podziału zbioru na treningowy i testowy.


In [2]:
import pandas as pd

iris = pd.DataFrame(iris_ds.data, columns=iris_ds.feature_names).assign(target=iris_ds.target)
iris.columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'target']

In [3]:
iris_train, iris_test = train_test_split(iris, test_size=0.2)

In [4]:
iris_train.head()


Out[4]:
sepal_length sepal_width petal_length petal_width target
122 7.7 2.8 6.7 2.0 2
93 5.0 2.3 3.3 1.0 1
60 5.0 2.0 3.5 1.0 1
23 5.1 3.3 1.7 0.5 0
85 6.0 3.4 4.5 1.6 1

In [5]:
iris_test.head()


Out[5]:
sepal_length sepal_width petal_length petal_width target
100 6.3 3.3 6.0 2.5 2
10 5.4 3.7 1.5 0.2 0
91 6.1 3.0 4.6 1.4 1
46 5.1 3.8 1.6 0.2 0
18 5.7 3.8 1.7 0.3 0

In [6]:
iris.size, iris_train.size, iris_test.size


Out[6]:
(750, 600, 150)

In [7]:
iris_ds.target_names


Out[7]:
array(['setosa', 'versicolor', 'virginica'], dtype='<U10')

Regresja logistyczna

Jednym z najprostszych modeli klasyfikacji jest regresja logistyczna.

Model ten wykonuje klasyfikację z użyciem funkcji sigmoidalnej do przewidzenia jednej z dwóch klas. W przypadku wielu klas wykorzystuje się wiele funkcji sigmoidalnych.


In [8]:
features = ['petal_width', 'petal_length']
#  ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'target']

logreg = linear_model.LogisticRegression(C=1e5, multi_class='multinomial', solver='lbfgs')

logreg.fit(iris_train[features], iris_train['target'])

print(classification_report(iris_test['target'], logreg.predict(iris_test[features])))


             precision    recall  f1-score   support

          0       1.00      1.00      1.00        11
          1       0.83      0.91      0.87        11
          2       0.86      0.75      0.80         8

avg / total       0.90      0.90      0.90        30


In [9]:
linear_model.LogisticRegression?

In [10]:
def plot_decision_area(df, features, model, target='target'):

    if len(features) > 2:
        raise ValueError('Too many features, works only with 2')
    
    h = .02  # step size in the mesh

    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, x_max]x[y_min, y_max].
    x_min, y_min = tuple(df[features].min() - .5)
    x_max, y_max = tuple(df[features].max() + .5)

    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    Z = model.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)

    plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)

    # Plot also the training points
    plt.scatter(df[features[0]], df[features[1]], c=df[target], edgecolors='k', cmap=plt.cm.Paired)
    plt.xlabel(features[0])
    plt.ylabel(features[1])

    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.xticks(())
    plt.yticks(())

plot_decision_area(iris, features, logreg)


Zadanie

  1. Spróbuj zmienić parametr multi_class i solver; zobacz dokumentacje, żeby poznać dostępne opcję; co się zmieniło?
  2. Dodaj inne cechy i zobacz jak to wpływa na jakość klasyfikacji?

Support Vector Machines Classifier

Jak wspomniano w poprzednim notebooku odnośnie SVM, są też klasyfikatory używające funkcji jądrowych.

Zacznijmy od klasyfikatora liniowego.


In [11]:
features = ['sepal_length', 'sepal_width']

svc = svm.LinearSVC()

svc.fit(iris_train[features], iris_train['target'])

print(classification_report(iris_test['target'], svc.predict(iris_test[features])))


             precision    recall  f1-score   support

          0       0.92      1.00      0.96        11
          1       0.83      0.45      0.59        11
          2       0.58      0.88      0.70         8

avg / total       0.80      0.77      0.75        30


In [12]:
plot_decision_area(iris, features, svc)


Zadanie

  1. Spróbuj zmienić parametr multi_class; zobacz dokumentacje żeby poznać dostępne opcję; co się zmieniło?
  2. Spróbuj zmienić parametr loss; co się zmieniło?
  3. Dodaj inne cechy i zobacz jak to wpływa na jakość klasyfikacji?
  4. Zastosuj SVC z inną funkcją jądrową; zobacz dokumentację. Uwaga! Wykres działa tylko dla klasyfikacji dwuwymiarowej,

In [13]:
features = ['sepal_length', 'sepal_width']

# svc = svm.LinearSVC(multi_class='ovr')
svc = svm.SVC(kernel='rbf', C=1)

svc.fit(iris_train[features], iris_train['target'])

#print(classification_report(iris_test['target'], svc.predict(iris_test[features])))
plot_decision_area(iris, features, svc)


K-Nearest Neighbors

K-Nearest Neighbors to bardzo prosty algorytm klasyfikujący. Nie ma on funkcji klasyfikacyjnej jako takie, ale zapisuje zbiór uczący i podejmuje decyzję o klasie nowego elementu na podstawie k wybranych najbliższych elementów; zobacz poniższą ilustrację.

Dla k=3 nowy element (trójkąt) będzie sklasyfikowany jako czerwony, ale już dla k=5, będzie on sklasyfikowany jako niebieski.

Poniżej przedstawiamy działanie algorytmu na przykładowym zbiorze danych.


In [17]:
features = ['sepal_length', 'sepal_width']


def my_function(*args):
    print(args)

knn = neighbors.KNeighborsClassifier(n_neighbors=3, weights=my_function)

knn.fit(iris_train[features], iris_train['target'])

print(classification_report(iris_test['target'], knn.predict(iris_test[features])))
plot_decision_area(iris, features, knn)


(array([[0.1       , 0.14142136, 0.14142136],
       [0.1       , 0.2       , 0.2236068 ],
       [0.        , 0.1       , 0.1       ],
       [0.        , 0.        , 0.1       ],
       [0.2236068 , 0.31622777, 0.36055513],
       [0.1       , 0.1       , 0.1       ],
       [0.2       , 0.36055513, 0.4       ],
       [0.28284271, 0.31622777, 0.31622777],
       [0.1       , 0.1       , 0.14142136],
       [0.2       , 0.72801099, 0.82462113],
       [0.1       , 0.14142136, 0.14142136],
       [0.        , 0.        , 0.1       ],
       [0.        , 0.        , 0.1       ],
       [0.1       , 0.1       , 0.1       ],
       [0.2       , 0.2       , 0.2236068 ],
       [0.        , 0.        , 0.1       ],
       [0.        , 0.14142136, 0.2236068 ],
       [0.1       , 0.1       , 0.1       ],
       [0.        , 0.1       , 0.1       ],
       [0.14142136, 0.2       , 0.31622777],
       [0.2       , 0.2       , 0.2236068 ],
       [0.14142136, 0.14142136, 0.2236068 ],
       [0.1       , 0.1       , 0.1       ],
       [0.        , 0.1       , 0.1       ],
       [0.1       , 0.14142136, 0.14142136],
       [0.        , 0.1       , 0.1       ],
       [0.        , 0.31622777, 0.41231056],
       [0.        , 0.1       , 0.1       ],
       [0.        , 0.1       , 0.1       ],
       [0.        , 0.2236068 , 0.31622777]]),)
             precision    recall  f1-score   support

          0       1.00      1.00      1.00        11
          1       0.75      0.27      0.40        11
          2       0.47      0.88      0.61         8

avg / total       0.77      0.70      0.68        30

(array([[1.06301458, 1.3       , 1.42126704],
       [1.04995238, 1.28156155, 1.40584494],
       [1.03711137, 1.26317061, 1.39053946],
       ...,
       [1.28280942, 1.74229733, 2.01136769],
       [1.29321305, 1.75567651, 2.01801883],
       [1.30384048, 1.7691806 , 2.02484567]]),)

In [ ]:


In [ ]:


In [15]:


In [18]:
plot_decision_area(iris, features, knn)


(array([[1.06301458, 1.3       , 1.42126704],
       [1.04995238, 1.28156155, 1.40584494],
       [1.03711137, 1.26317061, 1.39053946],
       ...,
       [1.28280942, 1.74229733, 2.01136769],
       [1.29321305, 1.75567651, 2.01801883],
       [1.30384048, 1.7691806 , 2.02484567]]),)

Zadanie

  1. Zmień parametr weights; zobacz dokumentację; co się zmieniło?
  2. Zmień parametr n_neighbors; co się zmieniło?
  3. Dodaj inne cechy i zobacz jak to wpływa na jakość klasyfikacji?

Drzewa decyzyjne

Kolejnym typem klasifikatorów są drzewa decyzyjne. Takie klasyfikatory składają się z drzewa, który nauczony jest podejmować decyzje w zależności od wartości parametrów.

Zastosujmy drzewo decyzyjne do naszego zbioru danych.


In [19]:
features = ['sepal_length', 'sepal_width']

dtc = tree.DecisionTreeClassifier(criterion='entropy', splitter='best') # random

dtc.fit(iris_train[features], iris_train['target'])

print(classification_report(iris_test['target'], dtc.predict(iris_test[features])))


             precision    recall  f1-score   support

          0       0.91      0.91      0.91        11
          1       0.50      0.18      0.27        11
          2       0.47      0.88      0.61         8

avg / total       0.64      0.63      0.59        30


In [20]:
dtc = tree.DecisionTreeClassifier

In [21]:
plot_decision_area(iris, features, dtc)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-3a089066adca> in <module>()
----> 1 plot_decision_area(iris, features, dtc)

<ipython-input-10-a2c00ce28f26> in plot_decision_area(df, features, model, target)
     12 
     13     xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
---> 14     Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
     15 
     16     # Put the result into a color plot

TypeError: predict() missing 1 required positional argument: 'X'

In [ ]:
import graphviz

dot_data = tree.export_graphviz(dtc, out_file=None, 
                         feature_names=features,  
                         class_names=iris_ds.target_names,  
                         filled=True, rounded=True,  
                         special_characters=True)  
graph = graphviz.Source(dot_data, )  
graph

Zadanie

  1. Zmień parametr criterion; zobacz dokumentację; co się zmieniło?
  2. Zmień parametr splitter; co się zmieniło?
  3. Dodaj inne cechy i zobacz jak to wpływa na jakość klasyfikacji?

Kalibracja parametrów modeli

Wiele powyższych modeli ma wiele parametrów, które mogą wpłynąć na jakość klasyfikacji. Dotychczas skupialiśmy się na zmianach algorytmów lub cech. Spróbujmy teraz znaleźć najlepszy model. Wykorzystamy do tego celu funkcję GridSearchCV.

Najpierw definiujemy przestrzeń parametrów do przeszukania.


In [39]:
param_grid = [
  {'C': range(1, 1000, 1), 'kernel': ['linear']},
  {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
]

Następnie uczymy model podobnie jak poprzednio ale z użyciem GridSearchCV.


In [40]:
features = ['sepal_length', 'sepal_width']

svc = GridSearchCV(svm.SVC(probability=True), param_grid, return_train_score=True)    

svc.fit(iris_train[features], iris_train['target'])

print(classification_report(iris_test['target'], svc.predict(iris_test[features])))


             precision    recall  f1-score   support

          0       1.00      1.00      1.00        11
          1       0.88      0.64      0.74        11
          2       0.64      0.88      0.74         8

avg / total       0.86      0.83      0.83        30


In [41]:
plot_decision_area(iris, features, svc)



In [43]:
svc.best_estimator_


Out[43]:
SVC(C=100, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma=0.001, kernel='rbf',
  max_iter=-1, probability=True, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

In [44]:
svc.best_params_


Out[44]:
{'C': 100, 'gamma': 0.001, 'kernel': 'rbf'}

In [45]:
svc.cv_results_


Out[45]:
{'mean_fit_time': array([0.00200001, 0.00233618, 0.00200065, ..., 0.00199993, 0.0016667 ,
        0.00200001]),
 'mean_score_time': array([0.0006671 , 0.00066439, 0.00066582, ..., 0.00066662, 0.00033331,
        0.00033323]),
 'mean_test_score': array([0.775     , 0.75833333, 0.75833333, ..., 0.64166667, 0.75833333,
        0.80833333]),
 'mean_train_score': array([0.8375    , 0.82083333, 0.81666667, ..., 0.65416667, 0.82083333,
        0.82916667]),
 'param_C': masked_array(data=[1, 2, 3, ..., 100, 1000, 1000],
              mask=[False, False, False, ..., False, False, False],
        fill_value='?',
             dtype=object),
 'param_gamma': masked_array(data=[--, --, --, ..., 0.0001, 0.001, 0.0001],
              mask=[ True,  True,  True, ..., False, False, False],
        fill_value='?',
             dtype=object),
 'param_kernel': masked_array(data=['linear', 'linear', 'linear', ..., 'rbf', 'rbf', 'rbf'],
              mask=[False, False, False, ..., False, False, False],
        fill_value='?',
             dtype=object),
 'params': [{'C': 1, 'kernel': 'linear'},
  {'C': 2, 'kernel': 'linear'},
  {'C': 3, 'kernel': 'linear'},
  {'C': 4, 'kernel': 'linear'},
  {'C': 5, 'kernel': 'linear'},
  {'C': 6, 'kernel': 'linear'},
  {'C': 7, 'kernel': 'linear'},
  {'C': 8, 'kernel': 'linear'},
  {'C': 9, 'kernel': 'linear'},
  {'C': 10, 'kernel': 'linear'},
  {'C': 11, 'kernel': 'linear'},
  {'C': 12, 'kernel': 'linear'},
  {'C': 13, 'kernel': 'linear'},
  {'C': 14, 'kernel': 'linear'},
  {'C': 15, 'kernel': 'linear'},
  {'C': 16, 'kernel': 'linear'},
  {'C': 17, 'kernel': 'linear'},
  {'C': 18, 'kernel': 'linear'},
  {'C': 19, 'kernel': 'linear'},
  {'C': 20, 'kernel': 'linear'},
  {'C': 21, 'kernel': 'linear'},
  {'C': 22, 'kernel': 'linear'},
  {'C': 23, 'kernel': 'linear'},
  {'C': 24, 'kernel': 'linear'},
  {'C': 25, 'kernel': 'linear'},
  {'C': 26, 'kernel': 'linear'},
  {'C': 27, 'kernel': 'linear'},
  {'C': 28, 'kernel': 'linear'},
  {'C': 29, 'kernel': 'linear'},
  {'C': 30, 'kernel': 'linear'},
  {'C': 31, 'kernel': 'linear'},
  {'C': 32, 'kernel': 'linear'},
  {'C': 33, 'kernel': 'linear'},
  {'C': 34, 'kernel': 'linear'},
  {'C': 35, 'kernel': 'linear'},
  {'C': 36, 'kernel': 'linear'},
  {'C': 37, 'kernel': 'linear'},
  {'C': 38, 'kernel': 'linear'},
  {'C': 39, 'kernel': 'linear'},
  {'C': 40, 'kernel': 'linear'},
  {'C': 41, 'kernel': 'linear'},
  {'C': 42, 'kernel': 'linear'},
  {'C': 43, 'kernel': 'linear'},
  {'C': 44, 'kernel': 'linear'},
  {'C': 45, 'kernel': 'linear'},
  {'C': 46, 'kernel': 'linear'},
  {'C': 47, 'kernel': 'linear'},
  {'C': 48, 'kernel': 'linear'},
  {'C': 49, 'kernel': 'linear'},
  {'C': 50, 'kernel': 'linear'},
  {'C': 51, 'kernel': 'linear'},
  {'C': 52, 'kernel': 'linear'},
  {'C': 53, 'kernel': 'linear'},
  {'C': 54, 'kernel': 'linear'},
  {'C': 55, 'kernel': 'linear'},
  {'C': 56, 'kernel': 'linear'},
  {'C': 57, 'kernel': 'linear'},
  {'C': 58, 'kernel': 'linear'},
  {'C': 59, 'kernel': 'linear'},
  {'C': 60, 'kernel': 'linear'},
  {'C': 61, 'kernel': 'linear'},
  {'C': 62, 'kernel': 'linear'},
  {'C': 63, 'kernel': 'linear'},
  {'C': 64, 'kernel': 'linear'},
  {'C': 65, 'kernel': 'linear'},
  {'C': 66, 'kernel': 'linear'},
  {'C': 67, 'kernel': 'linear'},
  {'C': 68, 'kernel': 'linear'},
  {'C': 69, 'kernel': 'linear'},
  {'C': 70, 'kernel': 'linear'},
  {'C': 71, 'kernel': 'linear'},
  {'C': 72, 'kernel': 'linear'},
  {'C': 73, 'kernel': 'linear'},
  {'C': 74, 'kernel': 'linear'},
  {'C': 75, 'kernel': 'linear'},
  {'C': 76, 'kernel': 'linear'},
  {'C': 77, 'kernel': 'linear'},
  {'C': 78, 'kernel': 'linear'},
  {'C': 79, 'kernel': 'linear'},
  {'C': 80, 'kernel': 'linear'},
  {'C': 81, 'kernel': 'linear'},
  {'C': 82, 'kernel': 'linear'},
  {'C': 83, 'kernel': 'linear'},
  {'C': 84, 'kernel': 'linear'},
  {'C': 85, 'kernel': 'linear'},
  {'C': 86, 'kernel': 'linear'},
  {'C': 87, 'kernel': 'linear'},
  {'C': 88, 'kernel': 'linear'},
  {'C': 89, 'kernel': 'linear'},
  {'C': 90, 'kernel': 'linear'},
  {'C': 91, 'kernel': 'linear'},
  {'C': 92, 'kernel': 'linear'},
  {'C': 93, 'kernel': 'linear'},
  {'C': 94, 'kernel': 'linear'},
  {'C': 95, 'kernel': 'linear'},
  {'C': 96, 'kernel': 'linear'},
  {'C': 97, 'kernel': 'linear'},
  {'C': 98, 'kernel': 'linear'},
  {'C': 99, 'kernel': 'linear'},
  {'C': 100, 'kernel': 'linear'},
  {'C': 101, 'kernel': 'linear'},
  {'C': 102, 'kernel': 'linear'},
  {'C': 103, 'kernel': 'linear'},
  {'C': 104, 'kernel': 'linear'},
  {'C': 105, 'kernel': 'linear'},
  {'C': 106, 'kernel': 'linear'},
  {'C': 107, 'kernel': 'linear'},
  {'C': 108, 'kernel': 'linear'},
  {'C': 109, 'kernel': 'linear'},
  {'C': 110, 'kernel': 'linear'},
  {'C': 111, 'kernel': 'linear'},
  {'C': 112, 'kernel': 'linear'},
  {'C': 113, 'kernel': 'linear'},
  {'C': 114, 'kernel': 'linear'},
  {'C': 115, 'kernel': 'linear'},
  {'C': 116, 'kernel': 'linear'},
  {'C': 117, 'kernel': 'linear'},
  {'C': 118, 'kernel': 'linear'},
  {'C': 119, 'kernel': 'linear'},
  {'C': 120, 'kernel': 'linear'},
  {'C': 121, 'kernel': 'linear'},
  {'C': 122, 'kernel': 'linear'},
  {'C': 123, 'kernel': 'linear'},
  {'C': 124, 'kernel': 'linear'},
  {'C': 125, 'kernel': 'linear'},
  {'C': 126, 'kernel': 'linear'},
  {'C': 127, 'kernel': 'linear'},
  {'C': 128, 'kernel': 'linear'},
  {'C': 129, 'kernel': 'linear'},
  {'C': 130, 'kernel': 'linear'},
  {'C': 131, 'kernel': 'linear'},
  {'C': 132, 'kernel': 'linear'},
  {'C': 133, 'kernel': 'linear'},
  {'C': 134, 'kernel': 'linear'},
  {'C': 135, 'kernel': 'linear'},
  {'C': 136, 'kernel': 'linear'},
  {'C': 137, 'kernel': 'linear'},
  {'C': 138, 'kernel': 'linear'},
  {'C': 139, 'kernel': 'linear'},
  {'C': 140, 'kernel': 'linear'},
  {'C': 141, 'kernel': 'linear'},
  {'C': 142, 'kernel': 'linear'},
  {'C': 143, 'kernel': 'linear'},
  {'C': 144, 'kernel': 'linear'},
  {'C': 145, 'kernel': 'linear'},
  {'C': 146, 'kernel': 'linear'},
  {'C': 147, 'kernel': 'linear'},
  {'C': 148, 'kernel': 'linear'},
  {'C': 149, 'kernel': 'linear'},
  {'C': 150, 'kernel': 'linear'},
  {'C': 151, 'kernel': 'linear'},
  {'C': 152, 'kernel': 'linear'},
  {'C': 153, 'kernel': 'linear'},
  {'C': 154, 'kernel': 'linear'},
  {'C': 155, 'kernel': 'linear'},
  {'C': 156, 'kernel': 'linear'},
  {'C': 157, 'kernel': 'linear'},
  {'C': 158, 'kernel': 'linear'},
  {'C': 159, 'kernel': 'linear'},
  {'C': 160, 'kernel': 'linear'},
  {'C': 161, 'kernel': 'linear'},
  {'C': 162, 'kernel': 'linear'},
  {'C': 163, 'kernel': 'linear'},
  {'C': 164, 'kernel': 'linear'},
  {'C': 165, 'kernel': 'linear'},
  {'C': 166, 'kernel': 'linear'},
  {'C': 167, 'kernel': 'linear'},
  {'C': 168, 'kernel': 'linear'},
  {'C': 169, 'kernel': 'linear'},
  {'C': 170, 'kernel': 'linear'},
  {'C': 171, 'kernel': 'linear'},
  {'C': 172, 'kernel': 'linear'},
  {'C': 173, 'kernel': 'linear'},
  {'C': 174, 'kernel': 'linear'},
  {'C': 175, 'kernel': 'linear'},
  {'C': 176, 'kernel': 'linear'},
  {'C': 177, 'kernel': 'linear'},
  {'C': 178, 'kernel': 'linear'},
  {'C': 179, 'kernel': 'linear'},
  {'C': 180, 'kernel': 'linear'},
  {'C': 181, 'kernel': 'linear'},
  {'C': 182, 'kernel': 'linear'},
  {'C': 183, 'kernel': 'linear'},
  {'C': 184, 'kernel': 'linear'},
  {'C': 185, 'kernel': 'linear'},
  {'C': 186, 'kernel': 'linear'},
  {'C': 187, 'kernel': 'linear'},
  {'C': 188, 'kernel': 'linear'},
  {'C': 189, 'kernel': 'linear'},
  {'C': 190, 'kernel': 'linear'},
  {'C': 191, 'kernel': 'linear'},
  {'C': 192, 'kernel': 'linear'},
  {'C': 193, 'kernel': 'linear'},
  {'C': 194, 'kernel': 'linear'},
  {'C': 195, 'kernel': 'linear'},
  {'C': 196, 'kernel': 'linear'},
  {'C': 197, 'kernel': 'linear'},
  {'C': 198, 'kernel': 'linear'},
  {'C': 199, 'kernel': 'linear'},
  {'C': 200, 'kernel': 'linear'},
  {'C': 201, 'kernel': 'linear'},
  {'C': 202, 'kernel': 'linear'},
  {'C': 203, 'kernel': 'linear'},
  {'C': 204, 'kernel': 'linear'},
  {'C': 205, 'kernel': 'linear'},
  {'C': 206, 'kernel': 'linear'},
  {'C': 207, 'kernel': 'linear'},
  {'C': 208, 'kernel': 'linear'},
  {'C': 209, 'kernel': 'linear'},
  {'C': 210, 'kernel': 'linear'},
  {'C': 211, 'kernel': 'linear'},
  {'C': 212, 'kernel': 'linear'},
  {'C': 213, 'kernel': 'linear'},
  {'C': 214, 'kernel': 'linear'},
  {'C': 215, 'kernel': 'linear'},
  {'C': 216, 'kernel': 'linear'},
  {'C': 217, 'kernel': 'linear'},
  {'C': 218, 'kernel': 'linear'},
  {'C': 219, 'kernel': 'linear'},
  {'C': 220, 'kernel': 'linear'},
  {'C': 221, 'kernel': 'linear'},
  {'C': 222, 'kernel': 'linear'},
  {'C': 223, 'kernel': 'linear'},
  {'C': 224, 'kernel': 'linear'},
  {'C': 225, 'kernel': 'linear'},
  {'C': 226, 'kernel': 'linear'},
  {'C': 227, 'kernel': 'linear'},
  {'C': 228, 'kernel': 'linear'},
  {'C': 229, 'kernel': 'linear'},
  {'C': 230, 'kernel': 'linear'},
  {'C': 231, 'kernel': 'linear'},
  {'C': 232, 'kernel': 'linear'},
  {'C': 233, 'kernel': 'linear'},
  {'C': 234, 'kernel': 'linear'},
  {'C': 235, 'kernel': 'linear'},
  {'C': 236, 'kernel': 'linear'},
  {'C': 237, 'kernel': 'linear'},
  {'C': 238, 'kernel': 'linear'},
  {'C': 239, 'kernel': 'linear'},
  {'C': 240, 'kernel': 'linear'},
  {'C': 241, 'kernel': 'linear'},
  {'C': 242, 'kernel': 'linear'},
  {'C': 243, 'kernel': 'linear'},
  {'C': 244, 'kernel': 'linear'},
  {'C': 245, 'kernel': 'linear'},
  {'C': 246, 'kernel': 'linear'},
  {'C': 247, 'kernel': 'linear'},
  {'C': 248, 'kernel': 'linear'},
  {'C': 249, 'kernel': 'linear'},
  {'C': 250, 'kernel': 'linear'},
  {'C': 251, 'kernel': 'linear'},
  {'C': 252, 'kernel': 'linear'},
  {'C': 253, 'kernel': 'linear'},
  {'C': 254, 'kernel': 'linear'},
  {'C': 255, 'kernel': 'linear'},
  {'C': 256, 'kernel': 'linear'},
  {'C': 257, 'kernel': 'linear'},
  {'C': 258, 'kernel': 'linear'},
  {'C': 259, 'kernel': 'linear'},
  {'C': 260, 'kernel': 'linear'},
  {'C': 261, 'kernel': 'linear'},
  {'C': 262, 'kernel': 'linear'},
  {'C': 263, 'kernel': 'linear'},
  {'C': 264, 'kernel': 'linear'},
  {'C': 265, 'kernel': 'linear'},
  {'C': 266, 'kernel': 'linear'},
  {'C': 267, 'kernel': 'linear'},
  {'C': 268, 'kernel': 'linear'},
  {'C': 269, 'kernel': 'linear'},
  {'C': 270, 'kernel': 'linear'},
  {'C': 271, 'kernel': 'linear'},
  {'C': 272, 'kernel': 'linear'},
  {'C': 273, 'kernel': 'linear'},
  {'C': 274, 'kernel': 'linear'},
  {'C': 275, 'kernel': 'linear'},
  {'C': 276, 'kernel': 'linear'},
  {'C': 277, 'kernel': 'linear'},
  {'C': 278, 'kernel': 'linear'},
  {'C': 279, 'kernel': 'linear'},
  {'C': 280, 'kernel': 'linear'},
  {'C': 281, 'kernel': 'linear'},
  {'C': 282, 'kernel': 'linear'},
  {'C': 283, 'kernel': 'linear'},
  {'C': 284, 'kernel': 'linear'},
  {'C': 285, 'kernel': 'linear'},
  {'C': 286, 'kernel': 'linear'},
  {'C': 287, 'kernel': 'linear'},
  {'C': 288, 'kernel': 'linear'},
  {'C': 289, 'kernel': 'linear'},
  {'C': 290, 'kernel': 'linear'},
  {'C': 291, 'kernel': 'linear'},
  {'C': 292, 'kernel': 'linear'},
  {'C': 293, 'kernel': 'linear'},
  {'C': 294, 'kernel': 'linear'},
  {'C': 295, 'kernel': 'linear'},
  {'C': 296, 'kernel': 'linear'},
  {'C': 297, 'kernel': 'linear'},
  {'C': 298, 'kernel': 'linear'},
  {'C': 299, 'kernel': 'linear'},
  {'C': 300, 'kernel': 'linear'},
  {'C': 301, 'kernel': 'linear'},
  {'C': 302, 'kernel': 'linear'},
  {'C': 303, 'kernel': 'linear'},
  {'C': 304, 'kernel': 'linear'},
  {'C': 305, 'kernel': 'linear'},
  {'C': 306, 'kernel': 'linear'},
  {'C': 307, 'kernel': 'linear'},
  {'C': 308, 'kernel': 'linear'},
  {'C': 309, 'kernel': 'linear'},
  {'C': 310, 'kernel': 'linear'},
  {'C': 311, 'kernel': 'linear'},
  {'C': 312, 'kernel': 'linear'},
  {'C': 313, 'kernel': 'linear'},
  {'C': 314, 'kernel': 'linear'},
  {'C': 315, 'kernel': 'linear'},
  {'C': 316, 'kernel': 'linear'},
  {'C': 317, 'kernel': 'linear'},
  {'C': 318, 'kernel': 'linear'},
  {'C': 319, 'kernel': 'linear'},
  {'C': 320, 'kernel': 'linear'},
  {'C': 321, 'kernel': 'linear'},
  {'C': 322, 'kernel': 'linear'},
  {'C': 323, 'kernel': 'linear'},
  {'C': 324, 'kernel': 'linear'},
  {'C': 325, 'kernel': 'linear'},
  {'C': 326, 'kernel': 'linear'},
  {'C': 327, 'kernel': 'linear'},
  {'C': 328, 'kernel': 'linear'},
  {'C': 329, 'kernel': 'linear'},
  {'C': 330, 'kernel': 'linear'},
  {'C': 331, 'kernel': 'linear'},
  {'C': 332, 'kernel': 'linear'},
  {'C': 333, 'kernel': 'linear'},
  {'C': 334, 'kernel': 'linear'},
  {'C': 335, 'kernel': 'linear'},
  {'C': 336, 'kernel': 'linear'},
  {'C': 337, 'kernel': 'linear'},
  {'C': 338, 'kernel': 'linear'},
  {'C': 339, 'kernel': 'linear'},
  {'C': 340, 'kernel': 'linear'},
  {'C': 341, 'kernel': 'linear'},
  {'C': 342, 'kernel': 'linear'},
  {'C': 343, 'kernel': 'linear'},
  {'C': 344, 'kernel': 'linear'},
  {'C': 345, 'kernel': 'linear'},
  {'C': 346, 'kernel': 'linear'},
  {'C': 347, 'kernel': 'linear'},
  {'C': 348, 'kernel': 'linear'},
  {'C': 349, 'kernel': 'linear'},
  {'C': 350, 'kernel': 'linear'},
  {'C': 351, 'kernel': 'linear'},
  {'C': 352, 'kernel': 'linear'},
  {'C': 353, 'kernel': 'linear'},
  {'C': 354, 'kernel': 'linear'},
  {'C': 355, 'kernel': 'linear'},
  {'C': 356, 'kernel': 'linear'},
  {'C': 357, 'kernel': 'linear'},
  {'C': 358, 'kernel': 'linear'},
  {'C': 359, 'kernel': 'linear'},
  {'C': 360, 'kernel': 'linear'},
  {'C': 361, 'kernel': 'linear'},
  {'C': 362, 'kernel': 'linear'},
  {'C': 363, 'kernel': 'linear'},
  {'C': 364, 'kernel': 'linear'},
  {'C': 365, 'kernel': 'linear'},
  {'C': 366, 'kernel': 'linear'},
  {'C': 367, 'kernel': 'linear'},
  {'C': 368, 'kernel': 'linear'},
  {'C': 369, 'kernel': 'linear'},
  {'C': 370, 'kernel': 'linear'},
  {'C': 371, 'kernel': 'linear'},
  {'C': 372, 'kernel': 'linear'},
  {'C': 373, 'kernel': 'linear'},
  {'C': 374, 'kernel': 'linear'},
  {'C': 375, 'kernel': 'linear'},
  {'C': 376, 'kernel': 'linear'},
  {'C': 377, 'kernel': 'linear'},
  {'C': 378, 'kernel': 'linear'},
  {'C': 379, 'kernel': 'linear'},
  {'C': 380, 'kernel': 'linear'},
  {'C': 381, 'kernel': 'linear'},
  {'C': 382, 'kernel': 'linear'},
  {'C': 383, 'kernel': 'linear'},
  {'C': 384, 'kernel': 'linear'},
  {'C': 385, 'kernel': 'linear'},
  {'C': 386, 'kernel': 'linear'},
  {'C': 387, 'kernel': 'linear'},
  {'C': 388, 'kernel': 'linear'},
  {'C': 389, 'kernel': 'linear'},
  {'C': 390, 'kernel': 'linear'},
  {'C': 391, 'kernel': 'linear'},
  {'C': 392, 'kernel': 'linear'},
  {'C': 393, 'kernel': 'linear'},
  {'C': 394, 'kernel': 'linear'},
  {'C': 395, 'kernel': 'linear'},
  {'C': 396, 'kernel': 'linear'},
  {'C': 397, 'kernel': 'linear'},
  {'C': 398, 'kernel': 'linear'},
  {'C': 399, 'kernel': 'linear'},
  {'C': 400, 'kernel': 'linear'},
  {'C': 401, 'kernel': 'linear'},
  {'C': 402, 'kernel': 'linear'},
  {'C': 403, 'kernel': 'linear'},
  {'C': 404, 'kernel': 'linear'},
  {'C': 405, 'kernel': 'linear'},
  {'C': 406, 'kernel': 'linear'},
  {'C': 407, 'kernel': 'linear'},
  {'C': 408, 'kernel': 'linear'},
  {'C': 409, 'kernel': 'linear'},
  {'C': 410, 'kernel': 'linear'},
  {'C': 411, 'kernel': 'linear'},
  {'C': 412, 'kernel': 'linear'},
  {'C': 413, 'kernel': 'linear'},
  {'C': 414, 'kernel': 'linear'},
  {'C': 415, 'kernel': 'linear'},
  {'C': 416, 'kernel': 'linear'},
  {'C': 417, 'kernel': 'linear'},
  {'C': 418, 'kernel': 'linear'},
  {'C': 419, 'kernel': 'linear'},
  {'C': 420, 'kernel': 'linear'},
  {'C': 421, 'kernel': 'linear'},
  {'C': 422, 'kernel': 'linear'},
  {'C': 423, 'kernel': 'linear'},
  {'C': 424, 'kernel': 'linear'},
  {'C': 425, 'kernel': 'linear'},
  {'C': 426, 'kernel': 'linear'},
  {'C': 427, 'kernel': 'linear'},
  {'C': 428, 'kernel': 'linear'},
  {'C': 429, 'kernel': 'linear'},
  {'C': 430, 'kernel': 'linear'},
  {'C': 431, 'kernel': 'linear'},
  {'C': 432, 'kernel': 'linear'},
  {'C': 433, 'kernel': 'linear'},
  {'C': 434, 'kernel': 'linear'},
  {'C': 435, 'kernel': 'linear'},
  {'C': 436, 'kernel': 'linear'},
  {'C': 437, 'kernel': 'linear'},
  {'C': 438, 'kernel': 'linear'},
  {'C': 439, 'kernel': 'linear'},
  {'C': 440, 'kernel': 'linear'},
  {'C': 441, 'kernel': 'linear'},
  {'C': 442, 'kernel': 'linear'},
  {'C': 443, 'kernel': 'linear'},
  {'C': 444, 'kernel': 'linear'},
  {'C': 445, 'kernel': 'linear'},
  {'C': 446, 'kernel': 'linear'},
  {'C': 447, 'kernel': 'linear'},
  {'C': 448, 'kernel': 'linear'},
  {'C': 449, 'kernel': 'linear'},
  {'C': 450, 'kernel': 'linear'},
  {'C': 451, 'kernel': 'linear'},
  {'C': 452, 'kernel': 'linear'},
  {'C': 453, 'kernel': 'linear'},
  {'C': 454, 'kernel': 'linear'},
  {'C': 455, 'kernel': 'linear'},
  {'C': 456, 'kernel': 'linear'},
  {'C': 457, 'kernel': 'linear'},
  {'C': 458, 'kernel': 'linear'},
  {'C': 459, 'kernel': 'linear'},
  {'C': 460, 'kernel': 'linear'},
  {'C': 461, 'kernel': 'linear'},
  {'C': 462, 'kernel': 'linear'},
  {'C': 463, 'kernel': 'linear'},
  {'C': 464, 'kernel': 'linear'},
  {'C': 465, 'kernel': 'linear'},
  {'C': 466, 'kernel': 'linear'},
  {'C': 467, 'kernel': 'linear'},
  {'C': 468, 'kernel': 'linear'},
  {'C': 469, 'kernel': 'linear'},
  {'C': 470, 'kernel': 'linear'},
  {'C': 471, 'kernel': 'linear'},
  {'C': 472, 'kernel': 'linear'},
  {'C': 473, 'kernel': 'linear'},
  {'C': 474, 'kernel': 'linear'},
  {'C': 475, 'kernel': 'linear'},
  {'C': 476, 'kernel': 'linear'},
  {'C': 477, 'kernel': 'linear'},
  {'C': 478, 'kernel': 'linear'},
  {'C': 479, 'kernel': 'linear'},
  {'C': 480, 'kernel': 'linear'},
  {'C': 481, 'kernel': 'linear'},
  {'C': 482, 'kernel': 'linear'},
  {'C': 483, 'kernel': 'linear'},
  {'C': 484, 'kernel': 'linear'},
  {'C': 485, 'kernel': 'linear'},
  {'C': 486, 'kernel': 'linear'},
  {'C': 487, 'kernel': 'linear'},
  {'C': 488, 'kernel': 'linear'},
  {'C': 489, 'kernel': 'linear'},
  {'C': 490, 'kernel': 'linear'},
  {'C': 491, 'kernel': 'linear'},
  {'C': 492, 'kernel': 'linear'},
  {'C': 493, 'kernel': 'linear'},
  {'C': 494, 'kernel': 'linear'},
  {'C': 495, 'kernel': 'linear'},
  {'C': 496, 'kernel': 'linear'},
  {'C': 497, 'kernel': 'linear'},
  {'C': 498, 'kernel': 'linear'},
  {'C': 499, 'kernel': 'linear'},
  {'C': 500, 'kernel': 'linear'},
  {'C': 501, 'kernel': 'linear'},
  {'C': 502, 'kernel': 'linear'},
  {'C': 503, 'kernel': 'linear'},
  {'C': 504, 'kernel': 'linear'},
  {'C': 505, 'kernel': 'linear'},
  {'C': 506, 'kernel': 'linear'},
  {'C': 507, 'kernel': 'linear'},
  {'C': 508, 'kernel': 'linear'},
  {'C': 509, 'kernel': 'linear'},
  {'C': 510, 'kernel': 'linear'},
  {'C': 511, 'kernel': 'linear'},
  {'C': 512, 'kernel': 'linear'},
  {'C': 513, 'kernel': 'linear'},
  {'C': 514, 'kernel': 'linear'},
  {'C': 515, 'kernel': 'linear'},
  {'C': 516, 'kernel': 'linear'},
  {'C': 517, 'kernel': 'linear'},
  {'C': 518, 'kernel': 'linear'},
  {'C': 519, 'kernel': 'linear'},
  {'C': 520, 'kernel': 'linear'},
  {'C': 521, 'kernel': 'linear'},
  {'C': 522, 'kernel': 'linear'},
  {'C': 523, 'kernel': 'linear'},
  {'C': 524, 'kernel': 'linear'},
  {'C': 525, 'kernel': 'linear'},
  {'C': 526, 'kernel': 'linear'},
  {'C': 527, 'kernel': 'linear'},
  {'C': 528, 'kernel': 'linear'},
  {'C': 529, 'kernel': 'linear'},
  {'C': 530, 'kernel': 'linear'},
  {'C': 531, 'kernel': 'linear'},
  {'C': 532, 'kernel': 'linear'},
  {'C': 533, 'kernel': 'linear'},
  {'C': 534, 'kernel': 'linear'},
  {'C': 535, 'kernel': 'linear'},
  {'C': 536, 'kernel': 'linear'},
  {'C': 537, 'kernel': 'linear'},
  {'C': 538, 'kernel': 'linear'},
  {'C': 539, 'kernel': 'linear'},
  {'C': 540, 'kernel': 'linear'},
  {'C': 541, 'kernel': 'linear'},
  {'C': 542, 'kernel': 'linear'},
  {'C': 543, 'kernel': 'linear'},
  {'C': 544, 'kernel': 'linear'},
  {'C': 545, 'kernel': 'linear'},
  {'C': 546, 'kernel': 'linear'},
  {'C': 547, 'kernel': 'linear'},
  {'C': 548, 'kernel': 'linear'},
  {'C': 549, 'kernel': 'linear'},
  {'C': 550, 'kernel': 'linear'},
  {'C': 551, 'kernel': 'linear'},
  {'C': 552, 'kernel': 'linear'},
  {'C': 553, 'kernel': 'linear'},
  {'C': 554, 'kernel': 'linear'},
  {'C': 555, 'kernel': 'linear'},
  {'C': 556, 'kernel': 'linear'},
  {'C': 557, 'kernel': 'linear'},
  {'C': 558, 'kernel': 'linear'},
  {'C': 559, 'kernel': 'linear'},
  {'C': 560, 'kernel': 'linear'},
  {'C': 561, 'kernel': 'linear'},
  {'C': 562, 'kernel': 'linear'},
  {'C': 563, 'kernel': 'linear'},
  {'C': 564, 'kernel': 'linear'},
  {'C': 565, 'kernel': 'linear'},
  {'C': 566, 'kernel': 'linear'},
  {'C': 567, 'kernel': 'linear'},
  {'C': 568, 'kernel': 'linear'},
  {'C': 569, 'kernel': 'linear'},
  {'C': 570, 'kernel': 'linear'},
  {'C': 571, 'kernel': 'linear'},
  {'C': 572, 'kernel': 'linear'},
  {'C': 573, 'kernel': 'linear'},
  {'C': 574, 'kernel': 'linear'},
  {'C': 575, 'kernel': 'linear'},
  {'C': 576, 'kernel': 'linear'},
  {'C': 577, 'kernel': 'linear'},
  {'C': 578, 'kernel': 'linear'},
  {'C': 579, 'kernel': 'linear'},
  {'C': 580, 'kernel': 'linear'},
  {'C': 581, 'kernel': 'linear'},
  {'C': 582, 'kernel': 'linear'},
  {'C': 583, 'kernel': 'linear'},
  {'C': 584, 'kernel': 'linear'},
  {'C': 585, 'kernel': 'linear'},
  {'C': 586, 'kernel': 'linear'},
  {'C': 587, 'kernel': 'linear'},
  {'C': 588, 'kernel': 'linear'},
  {'C': 589, 'kernel': 'linear'},
  {'C': 590, 'kernel': 'linear'},
  {'C': 591, 'kernel': 'linear'},
  {'C': 592, 'kernel': 'linear'},
  {'C': 593, 'kernel': 'linear'},
  {'C': 594, 'kernel': 'linear'},
  {'C': 595, 'kernel': 'linear'},
  {'C': 596, 'kernel': 'linear'},
  {'C': 597, 'kernel': 'linear'},
  {'C': 598, 'kernel': 'linear'},
  {'C': 599, 'kernel': 'linear'},
  {'C': 600, 'kernel': 'linear'},
  {'C': 601, 'kernel': 'linear'},
  {'C': 602, 'kernel': 'linear'},
  {'C': 603, 'kernel': 'linear'},
  {'C': 604, 'kernel': 'linear'},
  {'C': 605, 'kernel': 'linear'},
  {'C': 606, 'kernel': 'linear'},
  {'C': 607, 'kernel': 'linear'},
  {'C': 608, 'kernel': 'linear'},
  {'C': 609, 'kernel': 'linear'},
  {'C': 610, 'kernel': 'linear'},
  {'C': 611, 'kernel': 'linear'},
  {'C': 612, 'kernel': 'linear'},
  {'C': 613, 'kernel': 'linear'},
  {'C': 614, 'kernel': 'linear'},
  {'C': 615, 'kernel': 'linear'},
  {'C': 616, 'kernel': 'linear'},
  {'C': 617, 'kernel': 'linear'},
  {'C': 618, 'kernel': 'linear'},
  {'C': 619, 'kernel': 'linear'},
  {'C': 620, 'kernel': 'linear'},
  {'C': 621, 'kernel': 'linear'},
  {'C': 622, 'kernel': 'linear'},
  {'C': 623, 'kernel': 'linear'},
  {'C': 624, 'kernel': 'linear'},
  {'C': 625, 'kernel': 'linear'},
  {'C': 626, 'kernel': 'linear'},
  {'C': 627, 'kernel': 'linear'},
  {'C': 628, 'kernel': 'linear'},
  {'C': 629, 'kernel': 'linear'},
  {'C': 630, 'kernel': 'linear'},
  {'C': 631, 'kernel': 'linear'},
  {'C': 632, 'kernel': 'linear'},
  {'C': 633, 'kernel': 'linear'},
  {'C': 634, 'kernel': 'linear'},
  {'C': 635, 'kernel': 'linear'},
  {'C': 636, 'kernel': 'linear'},
  {'C': 637, 'kernel': 'linear'},
  {'C': 638, 'kernel': 'linear'},
  {'C': 639, 'kernel': 'linear'},
  {'C': 640, 'kernel': 'linear'},
  {'C': 641, 'kernel': 'linear'},
  {'C': 642, 'kernel': 'linear'},
  {'C': 643, 'kernel': 'linear'},
  {'C': 644, 'kernel': 'linear'},
  {'C': 645, 'kernel': 'linear'},
  {'C': 646, 'kernel': 'linear'},
  {'C': 647, 'kernel': 'linear'},
  {'C': 648, 'kernel': 'linear'},
  {'C': 649, 'kernel': 'linear'},
  {'C': 650, 'kernel': 'linear'},
  {'C': 651, 'kernel': 'linear'},
  {'C': 652, 'kernel': 'linear'},
  {'C': 653, 'kernel': 'linear'},
  {'C': 654, 'kernel': 'linear'},
  {'C': 655, 'kernel': 'linear'},
  {'C': 656, 'kernel': 'linear'},
  {'C': 657, 'kernel': 'linear'},
  {'C': 658, 'kernel': 'linear'},
  {'C': 659, 'kernel': 'linear'},
  {'C': 660, 'kernel': 'linear'},
  {'C': 661, 'kernel': 'linear'},
  {'C': 662, 'kernel': 'linear'},
  {'C': 663, 'kernel': 'linear'},
  {'C': 664, 'kernel': 'linear'},
  {'C': 665, 'kernel': 'linear'},
  {'C': 666, 'kernel': 'linear'},
  {'C': 667, 'kernel': 'linear'},
  {'C': 668, 'kernel': 'linear'},
  {'C': 669, 'kernel': 'linear'},
  {'C': 670, 'kernel': 'linear'},
  {'C': 671, 'kernel': 'linear'},
  {'C': 672, 'kernel': 'linear'},
  {'C': 673, 'kernel': 'linear'},
  {'C': 674, 'kernel': 'linear'},
  {'C': 675, 'kernel': 'linear'},
  {'C': 676, 'kernel': 'linear'},
  {'C': 677, 'kernel': 'linear'},
  {'C': 678, 'kernel': 'linear'},
  {'C': 679, 'kernel': 'linear'},
  {'C': 680, 'kernel': 'linear'},
  {'C': 681, 'kernel': 'linear'},
  {'C': 682, 'kernel': 'linear'},
  {'C': 683, 'kernel': 'linear'},
  {'C': 684, 'kernel': 'linear'},
  {'C': 685, 'kernel': 'linear'},
  {'C': 686, 'kernel': 'linear'},
  {'C': 687, 'kernel': 'linear'},
  {'C': 688, 'kernel': 'linear'},
  {'C': 689, 'kernel': 'linear'},
  {'C': 690, 'kernel': 'linear'},
  {'C': 691, 'kernel': 'linear'},
  {'C': 692, 'kernel': 'linear'},
  {'C': 693, 'kernel': 'linear'},
  {'C': 694, 'kernel': 'linear'},
  {'C': 695, 'kernel': 'linear'},
  {'C': 696, 'kernel': 'linear'},
  {'C': 697, 'kernel': 'linear'},
  {'C': 698, 'kernel': 'linear'},
  {'C': 699, 'kernel': 'linear'},
  {'C': 700, 'kernel': 'linear'},
  {'C': 701, 'kernel': 'linear'},
  {'C': 702, 'kernel': 'linear'},
  {'C': 703, 'kernel': 'linear'},
  {'C': 704, 'kernel': 'linear'},
  {'C': 705, 'kernel': 'linear'},
  {'C': 706, 'kernel': 'linear'},
  {'C': 707, 'kernel': 'linear'},
  {'C': 708, 'kernel': 'linear'},
  {'C': 709, 'kernel': 'linear'},
  {'C': 710, 'kernel': 'linear'},
  {'C': 711, 'kernel': 'linear'},
  {'C': 712, 'kernel': 'linear'},
  {'C': 713, 'kernel': 'linear'},
  {'C': 714, 'kernel': 'linear'},
  {'C': 715, 'kernel': 'linear'},
  {'C': 716, 'kernel': 'linear'},
  {'C': 717, 'kernel': 'linear'},
  {'C': 718, 'kernel': 'linear'},
  {'C': 719, 'kernel': 'linear'},
  {'C': 720, 'kernel': 'linear'},
  {'C': 721, 'kernel': 'linear'},
  {'C': 722, 'kernel': 'linear'},
  {'C': 723, 'kernel': 'linear'},
  {'C': 724, 'kernel': 'linear'},
  {'C': 725, 'kernel': 'linear'},
  {'C': 726, 'kernel': 'linear'},
  {'C': 727, 'kernel': 'linear'},
  {'C': 728, 'kernel': 'linear'},
  {'C': 729, 'kernel': 'linear'},
  {'C': 730, 'kernel': 'linear'},
  {'C': 731, 'kernel': 'linear'},
  {'C': 732, 'kernel': 'linear'},
  {'C': 733, 'kernel': 'linear'},
  {'C': 734, 'kernel': 'linear'},
  {'C': 735, 'kernel': 'linear'},
  {'C': 736, 'kernel': 'linear'},
  {'C': 737, 'kernel': 'linear'},
  {'C': 738, 'kernel': 'linear'},
  {'C': 739, 'kernel': 'linear'},
  {'C': 740, 'kernel': 'linear'},
  {'C': 741, 'kernel': 'linear'},
  {'C': 742, 'kernel': 'linear'},
  {'C': 743, 'kernel': 'linear'},
  {'C': 744, 'kernel': 'linear'},
  {'C': 745, 'kernel': 'linear'},
  {'C': 746, 'kernel': 'linear'},
  {'C': 747, 'kernel': 'linear'},
  {'C': 748, 'kernel': 'linear'},
  {'C': 749, 'kernel': 'linear'},
  {'C': 750, 'kernel': 'linear'},
  {'C': 751, 'kernel': 'linear'},
  {'C': 752, 'kernel': 'linear'},
  {'C': 753, 'kernel': 'linear'},
  {'C': 754, 'kernel': 'linear'},
  {'C': 755, 'kernel': 'linear'},
  {'C': 756, 'kernel': 'linear'},
  {'C': 757, 'kernel': 'linear'},
  {'C': 758, 'kernel': 'linear'},
  {'C': 759, 'kernel': 'linear'},
  {'C': 760, 'kernel': 'linear'},
  {'C': 761, 'kernel': 'linear'},
  {'C': 762, 'kernel': 'linear'},
  {'C': 763, 'kernel': 'linear'},
  {'C': 764, 'kernel': 'linear'},
  {'C': 765, 'kernel': 'linear'},
  {'C': 766, 'kernel': 'linear'},
  {'C': 767, 'kernel': 'linear'},
  {'C': 768, 'kernel': 'linear'},
  {'C': 769, 'kernel': 'linear'},
  {'C': 770, 'kernel': 'linear'},
  {'C': 771, 'kernel': 'linear'},
  {'C': 772, 'kernel': 'linear'},
  {'C': 773, 'kernel': 'linear'},
  {'C': 774, 'kernel': 'linear'},
  {'C': 775, 'kernel': 'linear'},
  {'C': 776, 'kernel': 'linear'},
  {'C': 777, 'kernel': 'linear'},
  {'C': 778, 'kernel': 'linear'},
  {'C': 779, 'kernel': 'linear'},
  {'C': 780, 'kernel': 'linear'},
  {'C': 781, 'kernel': 'linear'},
  {'C': 782, 'kernel': 'linear'},
  {'C': 783, 'kernel': 'linear'},
  {'C': 784, 'kernel': 'linear'},
  {'C': 785, 'kernel': 'linear'},
  {'C': 786, 'kernel': 'linear'},
  {'C': 787, 'kernel': 'linear'},
  {'C': 788, 'kernel': 'linear'},
  {'C': 789, 'kernel': 'linear'},
  {'C': 790, 'kernel': 'linear'},
  {'C': 791, 'kernel': 'linear'},
  {'C': 792, 'kernel': 'linear'},
  {'C': 793, 'kernel': 'linear'},
  {'C': 794, 'kernel': 'linear'},
  {'C': 795, 'kernel': 'linear'},
  {'C': 796, 'kernel': 'linear'},
  {'C': 797, 'kernel': 'linear'},
  {'C': 798, 'kernel': 'linear'},
  {'C': 799, 'kernel': 'linear'},
  {'C': 800, 'kernel': 'linear'},
  {'C': 801, 'kernel': 'linear'},
  {'C': 802, 'kernel': 'linear'},
  {'C': 803, 'kernel': 'linear'},
  {'C': 804, 'kernel': 'linear'},
  {'C': 805, 'kernel': 'linear'},
  {'C': 806, 'kernel': 'linear'},
  {'C': 807, 'kernel': 'linear'},
  {'C': 808, 'kernel': 'linear'},
  {'C': 809, 'kernel': 'linear'},
  {'C': 810, 'kernel': 'linear'},
  {'C': 811, 'kernel': 'linear'},
  {'C': 812, 'kernel': 'linear'},
  {'C': 813, 'kernel': 'linear'},
  {'C': 814, 'kernel': 'linear'},
  {'C': 815, 'kernel': 'linear'},
  {'C': 816, 'kernel': 'linear'},
  {'C': 817, 'kernel': 'linear'},
  {'C': 818, 'kernel': 'linear'},
  {'C': 819, 'kernel': 'linear'},
  {'C': 820, 'kernel': 'linear'},
  {'C': 821, 'kernel': 'linear'},
  {'C': 822, 'kernel': 'linear'},
  {'C': 823, 'kernel': 'linear'},
  {'C': 824, 'kernel': 'linear'},
  {'C': 825, 'kernel': 'linear'},
  {'C': 826, 'kernel': 'linear'},
  {'C': 827, 'kernel': 'linear'},
  {'C': 828, 'kernel': 'linear'},
  {'C': 829, 'kernel': 'linear'},
  {'C': 830, 'kernel': 'linear'},
  {'C': 831, 'kernel': 'linear'},
  {'C': 832, 'kernel': 'linear'},
  {'C': 833, 'kernel': 'linear'},
  {'C': 834, 'kernel': 'linear'},
  {'C': 835, 'kernel': 'linear'},
  {'C': 836, 'kernel': 'linear'},
  {'C': 837, 'kernel': 'linear'},
  {'C': 838, 'kernel': 'linear'},
  {'C': 839, 'kernel': 'linear'},
  {'C': 840, 'kernel': 'linear'},
  {'C': 841, 'kernel': 'linear'},
  {'C': 842, 'kernel': 'linear'},
  {'C': 843, 'kernel': 'linear'},
  {'C': 844, 'kernel': 'linear'},
  {'C': 845, 'kernel': 'linear'},
  {'C': 846, 'kernel': 'linear'},
  {'C': 847, 'kernel': 'linear'},
  {'C': 848, 'kernel': 'linear'},
  {'C': 849, 'kernel': 'linear'},
  {'C': 850, 'kernel': 'linear'},
  {'C': 851, 'kernel': 'linear'},
  {'C': 852, 'kernel': 'linear'},
  {'C': 853, 'kernel': 'linear'},
  {'C': 854, 'kernel': 'linear'},
  {'C': 855, 'kernel': 'linear'},
  {'C': 856, 'kernel': 'linear'},
  {'C': 857, 'kernel': 'linear'},
  {'C': 858, 'kernel': 'linear'},
  {'C': 859, 'kernel': 'linear'},
  {'C': 860, 'kernel': 'linear'},
  {'C': 861, 'kernel': 'linear'},
  {'C': 862, 'kernel': 'linear'},
  {'C': 863, 'kernel': 'linear'},
  {'C': 864, 'kernel': 'linear'},
  {'C': 865, 'kernel': 'linear'},
  {'C': 866, 'kernel': 'linear'},
  {'C': 867, 'kernel': 'linear'},
  {'C': 868, 'kernel': 'linear'},
  {'C': 869, 'kernel': 'linear'},
  {'C': 870, 'kernel': 'linear'},
  {'C': 871, 'kernel': 'linear'},
  {'C': 872, 'kernel': 'linear'},
  {'C': 873, 'kernel': 'linear'},
  {'C': 874, 'kernel': 'linear'},
  {'C': 875, 'kernel': 'linear'},
  {'C': 876, 'kernel': 'linear'},
  {'C': 877, 'kernel': 'linear'},
  {'C': 878, 'kernel': 'linear'},
  {'C': 879, 'kernel': 'linear'},
  {'C': 880, 'kernel': 'linear'},
  {'C': 881, 'kernel': 'linear'},
  {'C': 882, 'kernel': 'linear'},
  {'C': 883, 'kernel': 'linear'},
  {'C': 884, 'kernel': 'linear'},
  {'C': 885, 'kernel': 'linear'},
  {'C': 886, 'kernel': 'linear'},
  {'C': 887, 'kernel': 'linear'},
  {'C': 888, 'kernel': 'linear'},
  {'C': 889, 'kernel': 'linear'},
  {'C': 890, 'kernel': 'linear'},
  {'C': 891, 'kernel': 'linear'},
  {'C': 892, 'kernel': 'linear'},
  {'C': 893, 'kernel': 'linear'},
  {'C': 894, 'kernel': 'linear'},
  {'C': 895, 'kernel': 'linear'},
  {'C': 896, 'kernel': 'linear'},
  {'C': 897, 'kernel': 'linear'},
  {'C': 898, 'kernel': 'linear'},
  {'C': 899, 'kernel': 'linear'},
  {'C': 900, 'kernel': 'linear'},
  {'C': 901, 'kernel': 'linear'},
  {'C': 902, 'kernel': 'linear'},
  {'C': 903, 'kernel': 'linear'},
  {'C': 904, 'kernel': 'linear'},
  {'C': 905, 'kernel': 'linear'},
  {'C': 906, 'kernel': 'linear'},
  {'C': 907, 'kernel': 'linear'},
  {'C': 908, 'kernel': 'linear'},
  {'C': 909, 'kernel': 'linear'},
  {'C': 910, 'kernel': 'linear'},
  {'C': 911, 'kernel': 'linear'},
  {'C': 912, 'kernel': 'linear'},
  {'C': 913, 'kernel': 'linear'},
  {'C': 914, 'kernel': 'linear'},
  {'C': 915, 'kernel': 'linear'},
  {'C': 916, 'kernel': 'linear'},
  {'C': 917, 'kernel': 'linear'},
  {'C': 918, 'kernel': 'linear'},
  {'C': 919, 'kernel': 'linear'},
  {'C': 920, 'kernel': 'linear'},
  {'C': 921, 'kernel': 'linear'},
  {'C': 922, 'kernel': 'linear'},
  {'C': 923, 'kernel': 'linear'},
  {'C': 924, 'kernel': 'linear'},
  {'C': 925, 'kernel': 'linear'},
  {'C': 926, 'kernel': 'linear'},
  {'C': 927, 'kernel': 'linear'},
  {'C': 928, 'kernel': 'linear'},
  {'C': 929, 'kernel': 'linear'},
  {'C': 930, 'kernel': 'linear'},
  {'C': 931, 'kernel': 'linear'},
  {'C': 932, 'kernel': 'linear'},
  {'C': 933, 'kernel': 'linear'},
  {'C': 934, 'kernel': 'linear'},
  {'C': 935, 'kernel': 'linear'},
  {'C': 936, 'kernel': 'linear'},
  {'C': 937, 'kernel': 'linear'},
  {'C': 938, 'kernel': 'linear'},
  {'C': 939, 'kernel': 'linear'},
  {'C': 940, 'kernel': 'linear'},
  {'C': 941, 'kernel': 'linear'},
  {'C': 942, 'kernel': 'linear'},
  {'C': 943, 'kernel': 'linear'},
  {'C': 944, 'kernel': 'linear'},
  {'C': 945, 'kernel': 'linear'},
  {'C': 946, 'kernel': 'linear'},
  {'C': 947, 'kernel': 'linear'},
  {'C': 948, 'kernel': 'linear'},
  {'C': 949, 'kernel': 'linear'},
  {'C': 950, 'kernel': 'linear'},
  {'C': 951, 'kernel': 'linear'},
  {'C': 952, 'kernel': 'linear'},
  {'C': 953, 'kernel': 'linear'},
  {'C': 954, 'kernel': 'linear'},
  {'C': 955, 'kernel': 'linear'},
  {'C': 956, 'kernel': 'linear'},
  {'C': 957, 'kernel': 'linear'},
  {'C': 958, 'kernel': 'linear'},
  {'C': 959, 'kernel': 'linear'},
  {'C': 960, 'kernel': 'linear'},
  {'C': 961, 'kernel': 'linear'},
  {'C': 962, 'kernel': 'linear'},
  {'C': 963, 'kernel': 'linear'},
  {'C': 964, 'kernel': 'linear'},
  {'C': 965, 'kernel': 'linear'},
  {'C': 966, 'kernel': 'linear'},
  {'C': 967, 'kernel': 'linear'},
  {'C': 968, 'kernel': 'linear'},
  {'C': 969, 'kernel': 'linear'},
  {'C': 970, 'kernel': 'linear'},
  {'C': 971, 'kernel': 'linear'},
  {'C': 972, 'kernel': 'linear'},
  {'C': 973, 'kernel': 'linear'},
  {'C': 974, 'kernel': 'linear'},
  {'C': 975, 'kernel': 'linear'},
  {'C': 976, 'kernel': 'linear'},
  {'C': 977, 'kernel': 'linear'},
  {'C': 978, 'kernel': 'linear'},
  {'C': 979, 'kernel': 'linear'},
  {'C': 980, 'kernel': 'linear'},
  {'C': 981, 'kernel': 'linear'},
  {'C': 982, 'kernel': 'linear'},
  {'C': 983, 'kernel': 'linear'},
  {'C': 984, 'kernel': 'linear'},
  {'C': 985, 'kernel': 'linear'},
  {'C': 986, 'kernel': 'linear'},
  {'C': 987, 'kernel': 'linear'},
  {'C': 988, 'kernel': 'linear'},
  {'C': 989, 'kernel': 'linear'},
  {'C': 990, 'kernel': 'linear'},
  {'C': 991, 'kernel': 'linear'},
  {'C': 992, 'kernel': 'linear'},
  {'C': 993, 'kernel': 'linear'},
  {'C': 994, 'kernel': 'linear'},
  {'C': 995, 'kernel': 'linear'},
  {'C': 996, 'kernel': 'linear'},
  {'C': 997, 'kernel': 'linear'},
  {'C': 998, 'kernel': 'linear'},
  {'C': 999, 'kernel': 'linear'},
  {'C': 1, 'gamma': 0.001, 'kernel': 'rbf'},
  ...],
 'rank_test_score': array([   3,    4,    4, ..., 1003,    4,    1]),
 'split0_test_score': array([0.85 , 0.8  , 0.8  , ..., 0.625, 0.8  , 0.875]),
 'split0_train_score': array([0.7875, 0.7625, 0.7625, ..., 0.675 , 0.7625, 0.7875]),
 'split1_test_score': array([0.65 , 0.65 , 0.65 , ..., 0.625, 0.65 , 0.7  ]),
 'split1_train_score': array([0.9   , 0.875 , 0.8625, ..., 0.6375, 0.875 , 0.8875]),
 'split2_test_score': array([0.825, 0.825, 0.825, ..., 0.675, 0.825, 0.85 ]),
 'split2_train_score': array([0.825 , 0.825 , 0.825 , ..., 0.65  , 0.825 , 0.8125]),
 'std_fit_time': array([8.14977627e-04, 4.68229216e-04, 9.98958356e-07, ...,
        1.12391596e-07, 4.71482745e-04, 1.12391596e-07]),
 'std_score_time': array([0.00047171, 0.00046981, 0.00047081, ..., 0.00047137, 0.00047137,
        0.00047126]),
 'std_test_score': array([0.08897565, 0.07728015, 0.07728015, ..., 0.02357023, 0.07728015,
        0.07728015]),
 'std_train_score': array([0.04677072, 0.04602234, 0.0412479 , ..., 0.01559024, 0.04602234,
        0.04249183])}

Zadanie

  1. Jaki model wygrał?
  2. Zmień inne parametry modelu SVC i zobacz ich wpływ.
  3. Zobacz czy dodanie cech poprawia jakość klasyfikacji.
  4. Wykonaj kalibrację dla regresji logistycznej.
  5. Wykonaj kalibrację dla k-NN.

Ocena jakości modelu

Aby ocenić jak dobrze model klasyfikuje, czy przeprowadza regresję, używamy wielu metryk, które mają za zadanie skupić się na poszczególnych parametrach modelu. Podstawowym testem który używaliśmy jest funkcja classification_report:


In [48]:
y_true = iris_test['target']
y_pred = svc.predict(iris_test[features])

print(classification_report(y_true, y_pred))


             precision    recall  f1-score   support

          0       1.00      1.00      1.00        11
          1       0.88      0.64      0.74        11
          2       0.64      0.88      0.74         8

avg / total       0.86      0.83      0.83        30

Funkcja ta prezentuje prezentuje precyzję i dokładność (precision and recall) w wersji wieloklasowej. Możemy też policzyć te parametry osobno jako średnie:


In [49]:
from sklearn.metrics import precision_score, recall_score, f1_score

avg = 'macro'
print('Precision: {:.4f}'.format(precision_score(y_true, y_pred, average=avg)))
print('Recall: {:.4f}'.format(recall_score(y_true, y_pred, average=avg)))
print('F1: {:.4f}'.format(f1_score(y_true, y_pred, average=avg)))


Precision: 0.8371
Recall: 0.8371
F1: 0.8246

Lub dla każdej klasy jak w raporcie:


In [50]:
from sklearn.metrics import precision_recall_fscore_support

precision, recall, f1, support = precision_recall_fscore_support(y_true, y_pred)
precision, recall, f1, support


Out[50]:
(array([1.        , 0.875     , 0.63636364]),
 array([1.        , 0.63636364, 0.875     ]),
 array([1.        , 0.73684211, 0.73684211]),
 array([11, 11,  8], dtype=int64))

Parametry powyższe liczone są na macierzy pomyłek (confusion matrix), w tym przypadku w wersji wieloklasowej. Możemy otrzymać numeryczną wersję tej macierzy używając funkcji confusion_matrix:


In [51]:
from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_true, y_pred)
cm


Out[51]:
array([[11,  0,  0],
       [ 0,  7,  4],
       [ 0,  1,  7]], dtype=int64)

W formie graficznej:


In [52]:
import itertools

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

plot_confusion_matrix(cm, iris_ds.target_names)


Confusion matrix, without normalization
[[11  0  0]
 [ 0  7  4]
 [ 0  1  7]]

Są też inne rodzaje metryk o których można poczytać w dokumentacji. Przykładowo, jest też prosta [miara Jaccarda], która opiera się na podobieństwie zbiorów:


In [ ]:
from sklearn.metrics import jaccard_similarity_score

jaccard_similarity_score(y_true, y_pred)

Inną popularną metryką jest krzywa ROC (Receiver operating characteristic). Krzywa ta mówi o jakości klasyfikacji jako poziom rozdzielenia dwóch klas od siebie.

Jako wartość metryki stosuje się powierzchnię pod krzywą (Area Under the Curve, AUC).

Wprawdzie metrykę stosuje się dla klasyfikacji binarnej, można ją policzyć dla każdej klasy i uśrednić, jak pokazano poniżej; wieloklasowy przykład na podstawie dokumentacji.


In [53]:
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle

from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from scipy import interp

# Import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Binarize the output
y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]

# Add noisy features to make the problem harder
random_state = np.random.RandomState(0)
n_samples, n_features = X.shape
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]

# shuffle and split training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,
                                                    random_state=0)

# Learn to predict each class against the other
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', 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):
    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"])

# Compute macro-average ROC curve and ROC area

# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))

# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
    mean_tpr += interp(all_fpr, fpr[i], tpr[i])

# Finally average it and compute AUC
mean_tpr /= n_classes

fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])

# Plot all ROC curves
plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
         label='micro-average ROC curve (area = {0:0.2f})'
               ''.format(roc_auc["micro"]),
         color='deeppink', linestyle=':', linewidth=4)

plt.plot(fpr["macro"], tpr["macro"],
         label='macro-average ROC curve (area = {0:0.2f})'
               ''.format(roc_auc["macro"]),
         color='navy', linestyle=':', linewidth=4)

colors = cycle(['aqua', 'darkorange', 'cornflowerblue'])
for i, color in zip(range(n_classes), colors):
    plt.plot(fpr[i], tpr[i], color=color, lw=4,
             label='ROC curve of class {0} (area = {1:0.2f})'
             ''.format(i, roc_auc[i]))

plt.plot([0, 1], [0, 1], 'k--', lw=4)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Some extension of Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
plt.show()



In [54]:
import graphviz
import pandas as pd
from sklearn import linear_model, neighbors, svm, tree, datasets
from sklearn.model_selection import train_test_split


iris_ds = datasets.load_iris()
iris = pd.DataFrame(iris_ds.data, columns=iris_ds.feature_names).assign(target=iris_ds.target)
iris.columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'target']
iris_train, iris_test = train_test_split(iris, test_size=0.2)

features = ['sepal_length', 'sepal_width']
dtc = tree.DecisionTreeClassifier()
dtc.fit(iris_train[features], iris_train['target'])

dot_data = tree.export_graphviz(dtc,
    out_file=None,
    feature_names=features,
    class_names=iris_ds.target_names,
    filled=True,
    rounded=True,
    special_characters=True)

graph = graphviz.Source(dot_data, )
graph


Out[54]:
Tree 0 sepal_length ≤ 5.45 gini = 0.666 samples = 120 value = [42, 41, 37] class = setosa 1 sepal_width ≤ 2.8 gini = 0.176 samples = 41 value = [37, 4, 0] class = setosa 0->1 True 8 sepal_length ≤ 6.15 gini = 0.557 samples = 79 value = [5, 37, 37] class = versicolor 0->8 False 2 gini = 0.0 samples = 3 value = [0, 3, 0] class = versicolor 1->2 3 sepal_length ≤ 5.3 gini = 0.051 samples = 38 value = [37, 1, 0] class = setosa 1->3 4 gini = 0.0 samples = 32 value = [32, 0, 0] class = setosa 3->4 5 sepal_width ≤ 3.2 gini = 0.278 samples = 6 value = [5, 1, 0] class = setosa 3->5 6 gini = 0.0 samples = 1 value = [0, 1, 0] class = versicolor 5->6 7 gini = 0.0 samples = 5 value = [5, 0, 0] class = setosa 5->7 9 sepal_width ≤ 3.45 gini = 0.508 samples = 35 value = [5, 23, 7] class = versicolor 8->9 32 sepal_length ≤ 7.05 gini = 0.434 samples = 44 value = [0, 14, 30] class = virginica 8->32 10 sepal_length ≤ 5.75 gini = 0.358 samples = 30 value = [0, 23, 7] class = versicolor 9->10 31 gini = 0.0 samples = 5 value = [5, 0, 0] class = setosa 9->31 11 sepal_width ≤ 2.75 gini = 0.133 samples = 14 value = [0, 13, 1] class = versicolor 10->11 18 sepal_length ≤ 6.05 gini = 0.469 samples = 16 value = [0, 10, 6] class = versicolor 10->18 12 gini = 0.0 samples = 7 value = [0, 7, 0] class = versicolor 11->12 13 sepal_length ≤ 5.65 gini = 0.245 samples = 7 value = [0, 6, 1] class = versicolor 11->13 14 sepal_width ≤ 2.9 gini = 0.444 samples = 3 value = [0, 2, 1] class = versicolor 13->14 17 gini = 0.0 samples = 4 value = [0, 4, 0] class = versicolor 13->17 15 gini = 0.0 samples = 1 value = [0, 0, 1] class = virginica 14->15 16 gini = 0.0 samples = 2 value = [0, 2, 0] class = versicolor 14->16 19 sepal_width ≤ 3.1 gini = 0.497 samples = 13 value = [0, 7, 6] class = versicolor 18->19 30 gini = 0.0 samples = 3 value = [0, 3, 0] class = versicolor 18->30 20 sepal_width ≤ 2.95 gini = 0.496 samples = 11 value = [0, 5, 6] class = virginica 19->20 29 gini = 0.0 samples = 2 value = [0, 2, 0] class = versicolor 19->29 21 sepal_length ≤ 5.9 gini = 0.494 samples = 9 value = [0, 5, 4] class = versicolor 20->21 28 gini = 0.0 samples = 2 value = [0, 0, 2] class = virginica 20->28 22 sepal_width ≤ 2.75 gini = 0.48 samples = 5 value = [0, 2, 3] class = virginica 21->22 25 sepal_width ≤ 2.45 gini = 0.375 samples = 4 value = [0, 3, 1] class = versicolor 21->25 23 gini = 0.5 samples = 4 value = [0, 2, 2] class = versicolor 22->23 24 gini = 0.0 samples = 1 value = [0, 0, 1] class = virginica 22->24 26 gini = 0.5 samples = 2 value = [0, 1, 1] class = versicolor 25->26 27 gini = 0.0 samples = 2 value = [0, 2, 0] class = versicolor 25->27 33 sepal_width ≤ 2.4 gini = 0.475 samples = 36 value = [0, 14, 22] class = virginica 32->33 68 gini = 0.0 samples = 8 value = [0, 0, 8] class = virginica 32->68 34 gini = 0.0 samples = 2 value = [0, 2, 0] class = versicolor 33->34 35 sepal_length ≤ 6.95 gini = 0.457 samples = 34 value = [0, 12, 22] class = virginica 33->35 36 sepal_width ≤ 3.35 gini = 0.444 samples = 33 value = [0, 11, 22] class = virginica 35->36 67 gini = 0.0 samples = 1 value = [0, 1, 0] class = versicolor 35->67 37 sepal_width ≤ 2.75 gini = 0.458 samples = 31 value = [0, 11, 20] class = virginica 36->37 66 gini = 0.0 samples = 2 value = [0, 0, 2] class = virginica 36->66 38 sepal_length ≤ 6.35 gini = 0.32 samples = 5 value = [0, 1, 4] class = virginica 37->38 43 sepal_width ≤ 2.95 gini = 0.473 samples = 26 value = [0, 10, 16] class = virginica 37->43 39 sepal_width ≤ 2.6 gini = 0.444 samples = 3 value = [0, 1, 2] class = virginica 38->39 42 gini = 0.0 samples = 2 value = [0, 0, 2] class = virginica 38->42 40 gini = 0.5 samples = 2 value = [0, 1, 1] class = versicolor 39->40 41 gini = 0.0 samples = 1 value = [0, 0, 1] class = virginica 39->41 44 sepal_length ≤ 6.45 gini = 0.494 samples = 9 value = [0, 5, 4] class = versicolor 43->44 53 sepal_width ≤ 3.05 gini = 0.415 samples = 17 value = [0, 5, 12] class = virginica 43->53 45 sepal_width ≤ 2.85 gini = 0.444 samples = 6 value = [0, 2, 4] class = virginica 44->45 52 gini = 0.0 samples = 3 value = [0, 3, 0] class = versicolor 44->52 46 gini = 0.0 samples = 3 value = [0, 0, 3] class = virginica 45->46 47 sepal_length ≤ 6.25 gini = 0.444 samples = 3 value = [0, 2, 1] class = versicolor 45->47 48 gini = 0.0 samples = 1 value = [0, 1, 0] class = versicolor 47->48 49 sepal_length ≤ 6.35 gini = 0.5 samples = 2 value = [0, 1, 1] class = versicolor 47->49 50 gini = 0.0 samples = 1 value = [0, 0, 1] class = virginica 49->50 51 gini = 0.0 samples = 1 value = [0, 1, 0] class = versicolor 49->51 54 gini = 0.0 samples = 4 value = [0, 0, 4] class = virginica 53->54 55 sepal_width ≤ 3.15 gini = 0.473 samples = 13 value = [0, 5, 8] class = virginica 53->55 56 sepal_length ≤ 6.55 gini = 0.5 samples = 6 value = [0, 3, 3] class = versicolor 55->56 61 sepal_length ≤ 6.55 gini = 0.408 samples = 7 value = [0, 2, 5] class = virginica 55->61 57 gini = 0.0 samples = 1 value = [0, 0, 1] class = virginica 56->57 58 sepal_length ≤ 6.8 gini = 0.48 samples = 5 value = [0, 3, 2] class = versicolor 56->58 59 gini = 0.444 samples = 3 value = [0, 2, 1] class = versicolor 58->59 60 gini = 0.5 samples = 2 value = [0, 1, 1] class = versicolor 58->60 62 sepal_length ≤ 6.35 gini = 0.5 samples = 4 value = [0, 2, 2] class = versicolor 61->62 65 gini = 0.0 samples = 3 value = [0, 0, 3] class = virginica 61->65 63 gini = 0.5 samples = 2 value = [0, 1, 1] class = versicolor 62->63 64 gini = 0.5 samples = 2 value = [0, 1, 1] class = versicolor 62->64

In [ ]: