-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclassifier_sklearn_wrapper.py
55 lines (42 loc) · 1.5 KB
/
classifier_sklearn_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
"""
Pipeline Classification with scikit-learn wrapped Classifier
============================================================
In this example, we show how to use scikit-learn wrapper's
`fit(X, y)` and `predict(X)` functions.
"""
from jubakit.wrapper.classifier import LinearClassifier, NearestNeighborsClassifier
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report
from sklearn.pipeline import Pipeline
from sklearn.utils import shuffle
# load hand-writtern number recognition dataset
digits = load_digits()
# shuffle and separate the dataset
X, y = shuffle(digits.data, digits.target, random_state=42)
n_train = int(X.shape[0] / 2)
X_train, y_train = X[:n_train], y[:n_train]
X_test, y_test = X[n_train:], y[n_train:]
# launch linear classifier (AROW)
clf = LinearClassifier(method='AROW', embedded=False, seed=42)
# scale dataset
scaled_pipeline = Pipeline([
('scaler', StandardScaler()),
('classifier', clf)
])
# decompose dataset
pca_pipeline = Pipeline([
('pca', PCA()),
('classifier', clf)
])
# evaluate each pipelines
pipelines = [clf, scaled_pipeline, pca_pipeline]
for pipeline in pipelines:
print(pipeline)
pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_test)
print(classification_report(y_test, y_pred))