-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlutils.py
200 lines (167 loc) · 7.47 KB
/
mlutils.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
import numpy as np
import codecs
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import silhouette_samples, silhouette_score
from sklearn.cluster import KMeans
def load_SMS_dataset(path):
X, y = [], []
with codecs.open(path, "r", "utf-8") as fd:
for line in fd:
parts = line.split("\t")
X.append(parts[1].strip())
y.append(parts[0].strip())
return np.array(X), np.array(y)
def plot_silhouette(n_clusters, X):
# Kôd preuzet s http://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_silhouette_analysis.html
# Create a subplot with 1 row and 2 columns
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.set_size_inches(18, 7)
# The 1st subplot is the silhouette plot
# The silhouette coefficient can range from -1, 1 but in this example all
# lie within [-0.1, 1]
ax1.set_xlim([-0.1, 1])
# The (n_clusters+1)*10 is for inserting blank space between silhouette
# plots of individual clusters, to demarcate them clearly.
ax1.set_ylim([0, len(X) + (n_clusters + 1) * 10])
# Initialize the clusterer with n_clusters value and a random generator
# seed of 10 for reproducibility.
clusterer = KMeans(n_clusters=n_clusters, random_state=10)
cluster_labels = clusterer.fit_predict(X)
# The silhouette_score gives the average value for all the samples.
# This gives a perspective into the density and separation of the formed
# clusters
silhouette_avg = silhouette_score(X, cluster_labels)
print("For n_clusters =", n_clusters,
"The average silhouette_score is :", silhouette_avg)
# Compute the silhouette scores for each sample
sample_silhouette_values = silhouette_samples(X, cluster_labels)
y_lower = 10
for i in range(n_clusters):
# Aggregate the silhouette scores for samples belonging to
# cluster i, and sort them
ith_cluster_silhouette_values = \
sample_silhouette_values[cluster_labels == i]
ith_cluster_silhouette_values.sort()
size_cluster_i = ith_cluster_silhouette_values.shape[0]
y_upper = y_lower + size_cluster_i
cmap = plt.cm.get_cmap("Spectral")
color = cmap(float(i) / n_clusters)
ax1.fill_betweenx(np.arange(y_lower, y_upper),
0, ith_cluster_silhouette_values,
facecolor=color, edgecolor=color, alpha=0.7)
# Label the silhouette plots with their cluster numbers at the middle
ax1.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i))
# Compute the new y_lower for next plot
y_lower = y_upper + 10 # 10 for the 0 samples
ax1.set_xlabel("Vrijednosti koeficijenta siluete")
ax1.set_ylabel("Oznaka grupe")
# The vertical line for average silhouette score of all the values
ax1.axvline(x=silhouette_avg, color="red", linestyle="--")
ax1.set_yticks([]) # Clear the yaxis labels / ticks
ax1.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1])
# 2nd Plot showing the actual clusters formed
cmap = plt.cm.get_cmap("Spectral")
colors = cmap(cluster_labels.astype(float) / n_clusters)
ax2.scatter(X[:, 0], X[:, 1], marker='.', s=30, lw=0, alpha=0.7,
c=colors, edgecolor='k')
# Labeling the clusters
centers = clusterer.cluster_centers_
# Draw white circles at cluster centers
ax2.scatter(centers[:, 0], centers[:, 1], marker='o',
c="white", alpha=1, s=200, edgecolor='k')
for i, c in enumerate(centers):
ax2.scatter(c[0], c[1], marker='$%d$' % i, alpha=1,
s=50, edgecolor='k')
ax2.set_xlabel(r"$x_1$")
ax2.set_ylabel(r"$x_2$")
plt.show()
def plot_2d_clf_problem(X, y, h=None):
'''
Plots a two-dimensional labeled dataset (X,y) and, if function h(x) is given,
the decision surfaces.
'''
assert X.shape[1] == 2, "Dataset is not two-dimensional"
if h!=None :
# Create a mesh to plot in
r = 0.02 # mesh resolution
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, r),
np.arange(y_min, y_max, r))
XX=np.c_[xx.ravel(), yy.ravel()]
try:
Z_test = h(XX)
if Z_test.shape == ():
# h returns a scalar when applied to a matrix; map explicitly
Z = np.array(list(map(h,XX)))
else :
Z = Z_test
except ValueError:
# can't apply to a matrix; map explicitly
Z = np.array(list(map(h,XX)))
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Pastel1)
# Plot the dataset
plt.scatter(X[:,0],X[:,1], c=y, cmap=plt.cm.tab20b, marker='o', s=50);
def plot_2d_svc_problem(X, y, svc=None):
'''
Plots a two-dimensional labeled dataset (X,y) and, if SVC object is given,
the decision surfaces (with margin as well).
'''
assert X.shape[1] == 2, "Dataset is not two-dimensional"
if svc!=None :
# Create a mesh to plot in
r = 0.03 # mesh resolution
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, r),
np.arange(y_min, y_max, r))
XX=np.c_[xx.ravel(), yy.ravel()]
Z = np.array([svc_predict(svc, x) for x in XX])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Pastel1)
# Plot the dataset
plt.scatter(X[:,0],X[:,1], c=y, cmap=plt.cm.Paired, marker='o', s=50)
#plt.show()
def svc_predict(svc, x) :
h = svc.decision_function([x])
if h >= -1 and h <= 1:
return 0.5
else:
return max(-1, min(1, h))
def plot_error_surface(err, c_range=(0,5), g_range=(0,5)):
c1, c2 = c_range[0], c_range[1]
g1, g2 = g_range[0], g_range[1]
plt.xticks(range(0,g2-g1+1,5),range(g1,g2,5)); plt.xlabel("gamma")
plt.yticks(range(0,c2-c1+1,5),range(c1,c2,5)); plt.ylabel("C")
p = plt.contour(err);
plt.imshow(1-err, interpolation='bilinear', origin='lower',cmap=plt.cm.gray)
plt.clabel(p, inline=1, fontsize=10)
#plt.show()
def knn_eval(n_instances=100, n_features=2, n_classes=2, n_informative=2,
test_size=0.3, k_range=(1, 20), n_trials=100):
train_errors = []
test_errors = []
ks = list(range(k_range[0], k_range[1] + 1))
for i in range(0, n_trials):
X, y = make_classification(n_instances, n_features, n_classes=n_classes,
n_informative=n_informative, n_redundant=0, n_clusters_per_class=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
train = []
test = []
for k in ks:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
train.append(1 - knn.score(X_train, y_train))
test.append(1 - knn.score(X_test, y_test))
train_errors.append(train)
test_errors.append(test)
train_errors = np.mean(np.array(train_errors), axis=0)
test_errors = np.mean(np.array(test_errors), axis=0)
best_k = ks[np.argmin(test_errors)]
return ks, best_k, train_errors, test_errors