forked from HendryLi/mlclass
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathex8.py
149 lines (96 loc) · 3.23 KB
/
ex8.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
#!/usr/local/Cellar/python/2.7.6/bin/python
# -*- coding: utf-8 -*-
import sys
from numpy import *
import scipy.misc, scipy.io, scipy.optimize
from matplotlib import pyplot, cm, colors, lines
from mpl_toolkits.mplot3d import Axes3D
import inspect
def multivariateGaussian( X, mu, sigma2 ):
k = len( mu )
m, n = shape( X )
temp_sigma2 = diag( sigma2 )
temp_X = X - mu
p = (2.0 * pi) ** (-k / 2.0) * linalg.det( temp_sigma2 ) ** -0.5 \
* exp(-0.5 * sum((temp_X * diagonal( linalg.pinv( temp_sigma2 ) )) * temp_X, axis=1))
return p
def estimateGaussian( X ):
mu = mean( X, axis=0 )
sigma2 = var( X, axis=0 )
return mu, sigma2
def plot( X ):
pyplot.plot( X[:, 0], X[:, 1], 'bx' )
pyplot.ylim([0, 30])
pyplot.xlim([0, 30])
pyplot.xlabel('Latency (ms)')
pyplot.ylabel('Throughput (mb/s)')
def visualizeFit( X, mu, sigma2 ):
X1 = mgrid[0:35.5:0.5]
X2 = mgrid[0:35.5:0.5]
X1, X2 = meshgrid( X1, X2 )
Z = multivariateGaussian( c_[X1.T.ravel().T, X2.T.ravel().T], mu, sigma2 )
Z = Z.reshape( shape(X1)[0], shape(X1)[1] ).T
a = arange( -20, 0, 3 )
b = ones( shape( a ) ) * 10
pyplot.contour(X1, X2, Z, pow( b, a ) )
def selectThreshold( y_val, p_val ):
best_epsilon = 0
best_f1 = 0
f1 = 0
step_size = (max(p_val) - min(p_val)) / 1000.0
for epsilon in arange(min(p_val), max(p_val)+step_size, step_size ):
prediction = (p_val < epsilon).reshape( shape( y_val)[0], 1 )
true_pos = sum((prediction == 1) & (y_val == 1))
false_pos = sum((prediction == 1) & (y_val == 0))
false_neg = sum((prediction == 0) & (y_val == 1))
precision = true_pos * 1.0 / (true_pos + false_pos)
recall = true_pos * 1.0 / (true_pos + false_neg)
f1 = (2 * precision * recall) / (precision + recall)
if f1 > best_f1:
best_f1 = f1
best_epsilon = epsilon
return best_f1, best_epsilon
def part1_1():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex8-004/mlclass-ex8/ex8data1.mat" )
X = mat['X']
X_val, y_val = mat['Xval'], mat['yval']
plot( X )
def part1_2():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex8-004/mlclass-ex8/ex8data1.mat" )
X = mat['X']
mu, sigma2 = estimateGaussian( X )
print mu
print sigma2
plot( X )
visualizeFit( X, mu, sigma2 )
pyplot.show( block=True )
def part1_3():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex8-004/mlclass-ex8/ex8data1.mat" )
X = mat['X']
X_val, y_val = mat['Xval'], mat['yval']
mu, sigma2 = estimateGaussian( X )
p = multivariateGaussian( X, mu, sigma2 )
p_val = multivariateGaussian( X_val, mu, sigma2 )
f1, epsilon = selectThreshold( y_val, p_val )
outliers = where( p < epsilon )
plot( X )
visualizeFit( X, mu, sigma2 )
pyplot.plot( X[outliers, 0], X[outliers, 1], 'ro', lw=2, ms=10 )
pyplot.show()
def part1_4():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex8-004/mlclass-ex8/ex8data2.mat" )
X = mat['X']
X_val, y_val = mat['Xval'], mat['yval']
mu, sigma2 = estimateGaussian( X )
p_val = multivariateGaussian( X_val, mu, sigma2 )
f1, epsilon = selectThreshold( y_val, p_val )
print epsilon
print f1
def main():
set_printoptions(precision=6, linewidth=200)
part1_1()
part1_2()
part1_3()
part1_4()
if __name__ == '__main__':
main()