-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
136 lines (92 loc) · 3.33 KB
/
main.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
# -*- coding: utf-8 -*-
"""Decison Trees.ipynb
Automatically generated by Colaboratory.
"""
# Commented out IPython magic to ensure Python compatibility.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline
from google.colab import drive
data = pd.read_csv('mydata.csv')
data.head()
x = data['Classification']
ax = sns.countplot(x=x, data=data)
y = data.columns[:-1]
x = data.columns[-1]
def violin_plots(x, y, data):
for i, col in enumerate(y):
plt.figure(i)
sns.set(rc={'figure.figsize':(11.7,8.27)})
ax = sns.violinplot(x=x, y=col, data=data)
violin_plots(x, y, data)
for col in data.columns:
print("{} : {}".format(col, data[col].isnull().sum()))
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
data['Classification'] = le.fit_transform(data['Classification'])
data.head()
from sklearn.model_selection import train_test_split
y = data['Classification'].values.reshape(-1, 1)
X = data.drop('Classification', 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)
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)
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.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
decision_tree_cm = confusion_matrix(y_test, y_pred)
plot_confusion_matrix(decision_tree_cm, [0, 1])
plt.show()
from sklearn.ensemble import BaggingClassifier
bagging_clf = BaggingClassifier()
bagging_clf.fit(X_train, y_train.ravel())
y_pred_bag = bagging_clf.predict(X_test)
bag_cm = confusion_matrix(y_test, y_pred_bag)
plot_confusion_matrix(bag_cm, [0, 1])
plt.show()
from sklearn.ensemble import RandomForestClassifier
random_clf = RandomForestClassifier(100)
random_clf.fit(X_train, y_train.ravel())
y_pred_random = random_clf.predict(X_test)
random_cm = confusion_matrix(y_test, y_pred_random)
plot_confusion_matrix(random_cm, [0, 1])
plt.show()
from sklearn.ensemble import GradientBoostingClassifier
boost_clf = GradientBoostingClassifier()
boost_clf.fit(X_train, y_train.ravel())
y_pred_boost = boost_clf.predict(X_test)
boost_cm = confusion_matrix(y_test, y_pred_boost)
plot_confusion_matrix(boost_cm, [0, 1])
plt.show()