-
Notifications
You must be signed in to change notification settings - Fork 0
/
2try_classification.py
139 lines (115 loc) · 5.16 KB
/
2try_classification.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Try different Classifiers
parent_dir = ''
this_dir = './'
plot_dir = 'plots/'
path_to_plot_dir = parent_dir+this_dir+plot_dir
from pathlib import Path
Path(path_to_plot_dir).mkdir(parents=True, exist_ok=True)
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from matplotlib.colors import ListedColormap
import plot_metrics
from evaluate import *
from sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
import sys
model_names = ('logit', 'knn', 'adaboost', 'rf', 'naive_bayes', 'svm', 'LDA', 'QDA')
try:
model_name = sys.argv[1]
except:
print("You must specify a classifier method, using naive bayes as default one.\n")
model_name = 'naive_bayes'
##########################################################################################################
##################### Importing the dataset
from sklearn.decomposition import PCA
#define types
types_dict = {0:'Setosa', 1:'Versicolour', 2:'Virginica'}
#import data
dataset = pd.read_csv('./data-iris.csv')
_X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1].astype(int)
pca = PCA(n_components=2)
X = pca.fit_transform(_X)
#########################################################################################################
# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
# Feature Scaling
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Fitting model to the Training set
if model_name == model_names[0]:
print('Using Logistic Regression.')
classifier = LogisticRegression(class_weight='balanced')
elif model_name == model_names[1]:
print('Using K-Nearest Neighbors Classifier.')
classifier = KNeighborsClassifier(n_neighbors=5, metric='minkowski', p=2)
elif model_name == model_names[2]:
print('Using AdaBoost Classifier.')
classifier = AdaBoostClassifier()
elif model_name == model_names[3]:
print('Using Random Forest Classifier.')
classifier = RandomForestClassifier(class_weight='balanced')
elif model_name == model_names[4]:
print('Using Naive Bayes Classifier.')
classifier = GaussianNB()
elif model_name == model_names[5]:
print('Using Support Vector Machines Classifier.')
classifier = SVC(probability=True, class_weight='balanced')
elif model_name == model_names[6]:
print('Using Linear Discriminant Analysis.')
classifier = LinearDiscriminantAnalysis()
elif model_name == model_names[7]:
print('Using Quadratic Discriminant Analysis.')
classifier = QuadraticDiscriminantAnalysis()
else:
print('Unknown option for classifier, using naive bayes as default one!')
model_name = 'naive_bayes'
classifier = GaussianNB()
classifier.fit(X_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test)
# just to calculate also training Confusion Matrix
y_pred_train = classifier.predict(X_train)
# define sets
set_label_list = ['Training', 'Testing']
# Plot the Confusion Matrix
classes = [types_dict[i] for i in range(3)]
title_cm = 'Iris Types Confusion Matrix ('+model_name.upper()+')'
y_lists = [[y_train, y_pred_train],[y_test, y_pred]]
initial_path = path_to_plot_dir+model_name+'-'+'confusion-matrix-'
for ((true_y, pred_y), label) in zip(y_lists, set_label_list):
plot_metrics.plot_confusion_matrix(y_true=true_y, y_pred=pred_y, classes=classes,
title=title_cm+' ('+label+' set)',
path=initial_path+label.upper()+'.png')
# Visualising the results for both training and testing set
set_list = [[X_train, y_train], [X_test, y_test]]
for ((X_set, y_set), label) in zip(set_list, set_label_list) :
plt.figure(figsize=(10,10))
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green', 'yellow')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = [ListedColormap(('darkred', 'darkgreen', 'tan'))(i)], label = j)
plt.title(model_name.upper() + ' (' + label + ' set)')
plt.legend((types_dict[0], types_dict[1], types_dict[2]),loc='upper right', bbox_to_anchor=(1.05, 1.17), ncol=3)
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.legend()
plt.savefig(path_to_plot_dir+model_name+'-results-' + label.upper() + '.png', dpi=250)
plt.clf()
plt.close()
print(model_name.upper() + ' PERFORMANCE:')
print_some_metrics(y_test, y_pred)