|
a |
|
b/utils.py |
|
|
1 |
from sklearn.linear_model import LogisticRegression, LinearRegression |
|
|
2 |
import random |
|
|
3 |
import numpy as np |
|
|
4 |
import matplotlib.colors as mcolors |
|
|
5 |
import matplotlib.pyplot as plt |
|
|
6 |
|
|
|
7 |
def plot_predictions(X, y, model = None, title = ""): |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
h = .025 |
|
|
11 |
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 |
|
|
12 |
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 |
|
|
13 |
plt.figure() |
|
|
14 |
if model != None: |
|
|
15 |
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) |
|
|
16 |
|
|
|
17 |
# Obtain labels for each point in mesh. Use last trained model. |
|
|
18 |
Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) |
|
|
19 |
|
|
|
20 |
|
|
|
21 |
# Put the result into a color plot |
|
|
22 |
Z = Z.reshape(xx.shape) |
|
|
23 |
to_rgb = mcolors.ColorConverter().to_rgb |
|
|
24 |
|
|
|
25 |
plt.clf() |
|
|
26 |
plt.imshow(Z, interpolation='nearest', |
|
|
27 |
extent=(xx.min(), xx.max(), yy.min(), yy.max()), |
|
|
28 |
cmap=plt.cm.brg, |
|
|
29 |
aspect='auto', origin='lower') |
|
|
30 |
|
|
|
31 |
# # Plot also the training points |
|
|
32 |
colors = ["blue", "red", "green", 'orange'] |
|
|
33 |
if y.shape[1] == 2: |
|
|
34 |
colors = ['blue', 'green'] |
|
|
35 |
# #print compress(y) |
|
|
36 |
y_c = compress(y) |
|
|
37 |
labs = np.unique(list(range(y.shape[1]))) |
|
|
38 |
|
|
|
39 |
for i, color in zip(labs, colors): |
|
|
40 |
#print i |
|
|
41 |
idx = np.where(y_c == i) |
|
|
42 |
plt.scatter(X[idx, 0], X[idx, 1], c=color) |
|
|
43 |
plt.title(title) |
|
|
44 |
plt.axis('tight') |
|
|
45 |
|
|
|
46 |
# colors = [to_rgb(x) for x in ['red','blue', 'green']] |
|
|
47 |
|
|
|
48 |
# plt.plot(X[:, 0], X[:, 1], '.', markersize=4) |
|
|
49 |
plt.show() |
|
|
50 |
|
|
|
51 |
def sparsify_data(X, sparsity = .5, noise = .01): |
|
|
52 |
d = X.shape[1] |
|
|
53 |
d_sparse = int(d / sparsity) |
|
|
54 |
new_X = np.random.rand(X.shape[0], d_sparse) * noise - noise / 2. |
|
|
55 |
new_X[:, :d] = X |
|
|
56 |
return new_X |
|
|
57 |
|
|
|
58 |
def compress(array): |
|
|
59 |
return np.matrix([[i for i in range(len(x)) if x[i] == max(x)][0] for x in array]).transpose() |
|
|
60 |
|
|
|
61 |
def prediction_accuracy(X, y, model): |
|
|
62 |
y_hat = model.predict(X) |
|
|
63 |
correct = sum([int(y[i,y_hat[i]] == 1) for i in range(len(y_hat))]) |
|
|
64 |
return 1. * correct / len(y_hat) |
|
|
65 |
|
|
|
66 |
def generate_data(means = [(0, 0), (5, 0), (2.5, 2.5)], stdev = 1., classes = 3, n = 50): |
|
|
67 |
#gaussian 1 |
|
|
68 |
K = len(means) |
|
|
69 |
X = np.empty((n*K, max([len(x) for x in means]))) |
|
|
70 |
y = np.zeros((n*K, classes)) |
|
|
71 |
for k in range(K): |
|
|
72 |
for i in range(len(means[k])): |
|
|
73 |
X[k*n : (k+1)*n,i] = [random.gauss(means[k][i], stdev) for j in range(n)] |
|
|
74 |
y[k*n : (k+1)*n, k % classes] = 1 |
|
|
75 |
return (X, y) |
|
|
76 |
|
|
|
77 |
def make_synthetic_data(means = [(0, 0), (2.5, -2.5), (5, 0), (2.5, 2.5)], stdev = 1., classes = 2, n = 50): |
|
|
78 |
pass |