forked from udooz/learningdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_3_MulticlassClassification.py
145 lines (113 loc) · 3.17 KB
/
02_3_MulticlassClassification.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
140
141
142
143
144
145
#%% [markdown]
# # Multiclass classification
#
# More than two labels. For example, image is cat, dog, dove
# Selection of activation for multiclass classification requires attention due to mutually exclusive classes, independent classes
# E.g. organizing blog posts using tags (independent)
# Categorize movies into genre horror, action, thriller
#
# Use softmax function when dealing mutually exclusive classes
#%% [markdown]
# ## 0. Import Libraries
#%%
with open('common.py') as fin:
exec(fin.read())
with open('matplotlibconf.py') as fin:
exec(fin.read())
#%% [markdown]
# ## 1. Load data
# Iris dataset
#%%
df = pd.read_csv('data/iris.csv')
df.head()
#%%
df.describe()
#%%
X = df.drop('species', axis=1)
X.head()
#%% [markdown]
# ## 3. Data Pre-processing
# Encoding class labels and create y
#%%
from sklearn.preprocessing import LabelEncoder
#%%
class_le = LabelEncoder()
y = class_le.fit_transform(df['species'].values)
y
#%% [markdown]
# To use the labels in ANN, we need to expand this into 3 binary dummy columns
#%%
from keras.utils import to_categorical
#%%
y_cat = to_categorical(y)
#%% [markdown]
# Let's check out what the data looks like by looking at the first 5 values:
#%%
y_cat[:5]
#%%
from sklearn.model_selection import train_test_split
#%%
X_train, X_test, y_train, y_test = train_test_split(X.values, y_cat, test_size=0.22,
random_state=6, stratify=y)
#%% [markdown]
# ## 2. Define Model
# * 4 input features
# * No hidden layer
# * 3 output layer
# * softmax activation
#%%
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD, Adam
#%%
model = Sequential()
model.add(Dense(3, input_dim=4, activation='softmax'))
model.compile(Adam(lr=0.1),
loss='categorical_crossentropy',
metrics=['accuracy'])
#%% [markdown]
# ## 3. Fit the model
#%%
model.fit(X_train, y_train,
validation_split=0.1,
epochs=30, verbose=0)
#%% [markdown]
# ## 4. Evaluate the model
#%%
y_pred = model.predict(X_test)
y_pred[:5]
#%% [markdown]
# Which class does our network think each flower is? We can obtain the predicted class with the `np.argmax`, which finds the index of the maximum value in an array:
#%%
y_test_class = np.argmax(y_test, axis=1)
y_pred_class = np.argmax(y_pred, axis=1)
#%%
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
#%%
print(classification_report(y_test_class, y_pred_class))
#%%
cm = confusion_matrix(y_test_class, y_pred_class)
pd.DataFrame(cm, index = class_le.classes_,
columns = ['pred_'+c for c in class_le.classes_])
#%%
plt.imshow(cm, cmap='Blues')
#%%
plt.scatter(X.loc[y==0,'sepal_length'],
X.loc[y==0,'petal_length'])
plt.scatter(X.loc[y==1,'sepal_length'],
X.loc[y==1,'petal_length'])
plt.scatter(X.loc[y==2,'sepal_length'],
X.loc[y==2,'petal_length'])
plt.xlabel('sepal_length')
plt.ylabel('petal_length')
plt.legend(class_le.classes_)
plt.title("The Iris Dataset")
#%%
import seaborn as sns
#%%
g = sns.pairplot(df, hue="species")
g.fig.suptitle("The Iris Dataset")
#%% [markdown]
# Homework - Deep Network
# Based on this let us perform optimization