|
a |
|
b/ClassifierCompare.py |
|
|
1 |
from sklearn.linear_model import RidgeClassifier |
|
|
2 |
|
|
|
3 |
print(__doc__) |
|
|
4 |
|
|
|
5 |
# Code source: Gaël Varoquaux |
|
|
6 |
# Andreas Müller |
|
|
7 |
# Modified for documentation by Jaques Grobler |
|
|
8 |
# License: BSD 3 clause |
|
|
9 |
import pandas as pd |
|
|
10 |
import numpy as np |
|
|
11 |
import matplotlib.pyplot as plt |
|
|
12 |
from matplotlib.colors import ListedColormap |
|
|
13 |
from sklearn.model_selection import train_test_split |
|
|
14 |
from sklearn.preprocessing import StandardScaler |
|
|
15 |
from sklearn.datasets import make_moons, make_circles, make_classification |
|
|
16 |
from sklearn.neural_network import MLPClassifier |
|
|
17 |
from sklearn.neighbors import KNeighborsClassifier |
|
|
18 |
from sklearn.svm import SVC |
|
|
19 |
from sklearn.gaussian_process import GaussianProcessClassifier |
|
|
20 |
from sklearn.gaussian_process.kernels import RBF |
|
|
21 |
from sklearn.tree import DecisionTreeClassifier |
|
|
22 |
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier |
|
|
23 |
from sklearn.naive_bayes import GaussianNB |
|
|
24 |
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis, LinearDiscriminantAnalysis |
|
|
25 |
|
|
|
26 |
h = .02 # step size in the mesh |
|
|
27 |
|
|
|
28 |
names = ["KNN", "Linear SVM", |
|
|
29 |
"Naive Bayes", "LDA", "QDA"] |
|
|
30 |
|
|
|
31 |
classifiers = [ |
|
|
32 |
KNeighborsClassifier(), |
|
|
33 |
SVC(kernel="linear", C=0.025), |
|
|
34 |
GaussianNB(), |
|
|
35 |
LinearDiscriminantAnalysis(), |
|
|
36 |
QuadraticDiscriminantAnalysis()] |
|
|
37 |
|
|
|
38 |
train_original = pd.read_csv("DataUsed/method23_real2.csv") |
|
|
39 |
test_original = pd.read_csv("DataUsed/method23_real2_valid.csv") |
|
|
40 |
df = train_original.append(test_original, ignore_index=True) |
|
|
41 |
|
|
|
42 |
# df.insert(3, "num2", num2) |
|
|
43 |
targetIndex = -1 |
|
|
44 |
# df = df.iloc[pd.isna(df.iloc[:, targetIndex]).values == False, :] |
|
|
45 |
# df = df.drop(columns=["Num1"]) |
|
|
46 |
|
|
|
47 |
vars = df.columns[range(len(df.columns) - 1)] |
|
|
48 |
df = df.values |
|
|
49 |
X1 = df[:, [0, -2]] |
|
|
50 |
X2 = df[:, [0, -6]] |
|
|
51 |
X3 = df[:, [-6, -2]] |
|
|
52 |
y = df[:, targetIndex] |
|
|
53 |
|
|
|
54 |
datasetsNames = ["450,810 nm", "450, 610 nm", "610,810 nm"] |
|
|
55 |
C450810 = (X1, y) |
|
|
56 |
C450610 = (X2, y) |
|
|
57 |
C610810 = (X3, y) |
|
|
58 |
|
|
|
59 |
datasets = [C450810, C450610, C610810] |
|
|
60 |
|
|
|
61 |
figure = plt.figure(figsize=(27, 9)) |
|
|
62 |
i = 1 |
|
|
63 |
# iterate over datasets |
|
|
64 |
for ds_cnt, ds in enumerate(datasets): |
|
|
65 |
# preprocess dataset, split into training and test part |
|
|
66 |
X, y = ds |
|
|
67 |
X = StandardScaler().fit_transform(X) |
|
|
68 |
X_train, X_test, y_train, y_test = \ |
|
|
69 |
train_test_split(X, y, test_size=.8) |
|
|
70 |
|
|
|
71 |
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 |
|
|
72 |
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 |
|
|
73 |
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), |
|
|
74 |
np.arange(y_min, y_max, h)) |
|
|
75 |
|
|
|
76 |
# just plot the dataset first |
|
|
77 |
cm = plt.cm.RdBu |
|
|
78 |
cm_bright = ListedColormap(['#FF0000', '#0000FF']) |
|
|
79 |
ax = plt.subplot(len(datasets), len(classifiers) + 1, i) |
|
|
80 |
if ds_cnt == 0: |
|
|
81 |
ax.set_title("Input data") |
|
|
82 |
# Plot the training points |
|
|
83 |
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright, |
|
|
84 |
edgecolors='k') |
|
|
85 |
# Plot the testing points |
|
|
86 |
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6, |
|
|
87 |
edgecolors='k') |
|
|
88 |
ax.set_xlim(xx.min(), xx.max()) |
|
|
89 |
ax.set_ylim(yy.min(), yy.max()) |
|
|
90 |
ax.set_xticks(()) |
|
|
91 |
ax.set_yticks(()) |
|
|
92 |
ax.set_ylabel(datasetsNames[ds_cnt]) |
|
|
93 |
i += 1 |
|
|
94 |
|
|
|
95 |
# iterate over classifiers |
|
|
96 |
for name, clf in zip(names, classifiers): |
|
|
97 |
ax = plt.subplot(len(datasets), len(classifiers) + 1, i) |
|
|
98 |
clf.fit(X_train, y_train) |
|
|
99 |
score = clf.score(X_test, y_test) |
|
|
100 |
|
|
|
101 |
# Plot the decision boundary. For that, we will assign a color to each |
|
|
102 |
# point in the mesh [x_min, x_max]x[y_min, y_max]. |
|
|
103 |
if hasattr(clf, "decision_function"): |
|
|
104 |
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) |
|
|
105 |
else: |
|
|
106 |
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1] |
|
|
107 |
|
|
|
108 |
# Put the result into a color plot |
|
|
109 |
Z = Z.reshape(xx.shape) |
|
|
110 |
ax.contourf(xx, yy, Z, cmap=cm, alpha=.8) |
|
|
111 |
|
|
|
112 |
# Plot the training points |
|
|
113 |
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright, |
|
|
114 |
edgecolors='k') |
|
|
115 |
# Plot the testing points |
|
|
116 |
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, |
|
|
117 |
edgecolors='k', alpha=0.6) |
|
|
118 |
|
|
|
119 |
ax.set_xlim(xx.min(), xx.max()) |
|
|
120 |
ax.set_ylim(yy.min(), yy.max()) |
|
|
121 |
ax.set_xticks(()) |
|
|
122 |
ax.set_yticks(()) |
|
|
123 |
if ds_cnt == 0: |
|
|
124 |
ax.set_title(name) |
|
|
125 |
ax.text(xx.max() - .3, yy.min() + .3, ('Accuracy: %.2f' % score).lstrip('0'), |
|
|
126 |
size=15, horizontalalignment='right') |
|
|
127 |
i += 1 |
|
|
128 |
|
|
|
129 |
plt.tight_layout() |
|
|
130 |
plt.show() |
|
|
131 |
pass |