This repository has been archived by the owner on Oct 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_3.py
203 lines (156 loc) · 5.22 KB
/
chapter_3.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Copyright Jeremy Nation <[email protected]>.
Licensed under the MIT license.
Almost entirely copied from code created by Sebastian Raschka, also licensed under the MIT license.
"""
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import (
LogisticRegression,
Perceptron,
)
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from visualization import plot_decision_regions
def gini(p):
return 2 * p * (1-p)
def entropy(p):
return -p * np.log2(p) - (1-p) * np.log2(1-p)
def error(p):
return 1 - max(p, 1-p)
def plot_impurity_indexes():
probs = np.arange(0.0, 1.0, 0.01)
entropies = [entropy(p) if p != 0 else None for p in probs]
scaled_entropies = [e * 0.5 if e is not None else None for e in entropies]
errors = [error(p) for p in probs]
plt.figure()
ax = plt.subplot(111)
plots = (
(entropies, 'Entropy', '-', 'black'),
(scaled_entropies, 'Entropy (scaled)', '-', 'lightgray'),
(gini(probs), 'Gini Impurity', '--', 'red'),
(errors, 'Misclassification Error', '-.', 'green'),
)
for y, label, linestyle, color in plots:
ax.plot(probs, y, label=label, linestyle=linestyle, lw=2, color=color)
ax.legend(
loc='upper center',
bbox_to_anchor=(0.5, 1.15),
ncol=3,
fancybox=True,
shadow=False,
)
ax.axhline(y=0.5, linewidth=1, color='k', linestyle='--')
ax.axhline(y=1.0, linewidth=1, color='k', linestyle='--')
plt.ylim([0, 1.1])
plt.xlabel('p(i=1)')
plt.ylabel('Impurity Index')
plt.show()
def plot_iris_with_classifier(clf, print_accuracy=False, standardize=True):
iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.3,
random_state=0,
)
if standardize:
sc = StandardScaler()
sc.fit(X_train)
X_train = sc.transform(X_train)
X_test = sc.transform(X_test)
units = 'standardized'
else:
units = 'cm'
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
if print_accuracy:
print("Misclassified samples: %d" % (y_test != y_pred).sum())
print("Accuracy: %.2f" % accuracy_score(y_test, y_pred))
X_combined = np.vstack((X_train, X_test))
y_combined = np.hstack((y_train, y_test))
plot_decision_regions(
X=X_combined,
y=y_combined,
classifier=clf,
test_index=range(105, 150),
)
plt.xlabel("petal length [%s]" % units)
plt.ylabel("petal width [%s]" % units)
plt.legend(loc='upper left')
plt.show()
def plot_lr_regularization():
iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target
X_train, _, y_train, _ = train_test_split(
X,
y,
test_size=0.3,
random_state=0,
)
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
weights = []
params = []
for c in np.logspace(-5, 4, num=10):
lr = LogisticRegression(C=c, random_state=0)
lr.fit(X_train_std, y_train)
weights.append(lr.coef_[1])
params.append(c)
weights = np.array(weights)
plt.plot(params, weights[:, 0], label='petal length')
plt.plot(params, weights[:, 1], linestyle='--', label='petal width')
plt.ylabel('weight coefficient')
plt.xlabel('C')
plt.legend(loc='upper left')
plt.xscale('log')
plt.show()
def sigmoid(z):
return 1.0 / (1.0 + np.exp(-z))
def plot_sigmoid():
z = np.arange(-7, 7, 0.1)
phi_z = sigmoid(z)
plt.plot(z, phi_z)
plt.axvline(0.0, color='k')
plt.ylim(-0.1, 1.1)
plt.xlabel('z')
plt.ylabel('$\phi (z)$')
plt.yticks([0.0, 0.5, 1.0])
ax = plt.gca()
ax.yaxis.grid(True)
plt.show()
def plot_xor():
np.random.seed(0)
X_xor = np.random.randn(200, 2)
y_xor = np.logical_xor(X_xor[:, 0] > 0, X_xor[:, 1] > 0)
y_xor = np.where(y_xor, 1, -1)
svm = SVC(kernel='rbf', random_state=0, gamma=0.1, C=10.0)
svm.fit(X_xor, y_xor)
plot_decision_regions(X_xor, y_xor, classifier=svm)
plt.legend(loc='upper left')
plt.show()
if __name__ == '__main__':
# clf = Perceptron(n_iter=40, eta0=0.1, random_state=0)
# clf = LogisticRegression(C=1000.0, random_state=0)
# clf = SVC(kernel='linear', C=1.0, random_state=0)
# clf = SVC(kernel='rbf', random_state=0, gamma=0.2, C=1.0)
# clf = SVC(kernel='rbf', random_state=0, gamma=100.0, C=1.0)
clf = KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski')
plot_iris_with_classifier(clf)
# clf = DecisionTreeClassifier(criterion='entropy', max_depth=3, random_state=0)
# clf = RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=1, n_jobs=2)
# plot_iris_with_classifier(clf, standardize=False)
# plot_sigmoid()
# plot_lr_regularization()
# plot_xor()
# plot_impurity_indexes()