-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtry_ml.py
More file actions
249 lines (222 loc) · 6.08 KB
/
try_ml.py
File metadata and controls
249 lines (222 loc) · 6.08 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
####################################################################################
# #
# These are some basic starter code for different models #
# #
####################################################################################
'''
===============================
linear regression using covariance and variance
===============================
'''
# import matplotlib.pyplot as plt
# from sklearn.linear_model import LinearRegression
# import numpy as np
#
# X = [[6],[8],[10],[14],[18]]
# y = [[7],[9],[13],[17.5],[18]]
#
# plt.figure()
# plt.xlabel("diameter in inches")
# plt.ylabel("price in dollar")
# plt.plot(X,y,'k.')
#
# model = LinearRegression()
# model.fit(X,y)
#
# #mean
# print "prdicted is", model.predict(X)
# print "r squared is ", np.mean((model.predict(X)-y)**2)
#
# #variance
# print np.var(X,ddof=1)
#
# #covariance
# print np.cov([6,8,10,14,18],[7,9,13,17.5,18])[0][1]
# plt.axis([0,25,0,25])
# plt.grid(True)
# plt.show()
'''
===============================
multivariate and normal eq
===============================
'''
# from numpy.linalg import inv
# from numpy import dot, transpose
# X= [[1,6,2],[1,8,1],[1,10,0],[1,14,2],[1,18,0]]
# Y =[[7],[9],[13],[17.5],[18]]
#
# b = dot(inv(dot(transpose(X),X)),dot(transpose(X),Y))
# print b
# X_test = [[1,8,2],[1,9,0],[1,11,2],[1,16,2],[1,12,0]]
#
# pred_y = dot(X_test,b)
#
# print "predicted manually with normal equation"
# print pred_y
#
#
# from sklearn.linear_model import LinearRegression
#
# X_train= [[6,2],[8,1],[10,0],[14,2],[18,0]]
# Y_train =[[7],[9],[13],[17.5],[18]]
#
# model = LinearRegression()
# model.fit(X_train,Y_train)
#
#
# X_test = [[8,2],[9,0],[11,2],[16,2],[12,0]]
# Y_test = [[11],[8.5],[15],[18],[11]]
#
# print "prdicted using sklearn"
# prediction = model.predict(X_test)
# print prediction
#
# for i,pred in enumerate(prediction):
# print "prdiction=",pred,"original=",Y_test[i]
'''
===============================
polynomial regression
===============================
'''
# import numpy as np
# import matplotlib.pyplot as plt
# from sklearn.linear_model import LinearRegression
# from sklearn.preprocessing import PolynomialFeatures
#
# X_train = [[6],[8],[10],[14],[18]]
# Y_train = [[7],[9],[13],[17.5],[18]]
#
# X_test= [[6],[8],[11],[16]]
# Y_test =[[8],[12],[15],[18]]
#
# regressor =LinearRegression()
# regressor.fit(X_train,Y_train)
#
# xx = np.linspace(0,26,100)
# yy = regressor.predict(xx.reshape(xx.shape[0],1))
#
# plt.plot(xx,yy)
'''
===============================
pixel intensities
===============================
'''
# from sklearn import datasets
# digits = datasets.load_digits()
# print digits
# print "Digigt:",digits.target[3]
# print digits.images[3]
# print "feature vector:", digits.images[3].reshape(-1,64)
# import numpy as nps
# from skimage.feature import corner_harris,corner_peaks
# from skimage.color import rgb2gray
# import matplotlib.pyplot as plt
# import skimage.io as io
# from skimage.exposure import equalize_hist
#
# def show_corners(corners,image):
# fig = plt.figure()
# plt.gray()
# plt.imshow(image)
# y_corner,x_corner = zip(*corners)
# plt.plot(x_corner,y_corner,'or')
# plt.xlim(0,image.shape[1])
# plt.ylim(image.shape[0],0)
# fig.set_size_inches(nps.array(fig.get_size_inches())*1.5)
# plt.show()
#
# image =io.imread('new.jpg')
# image = equalize_hist(rgb2gray(image))
# corners =corner_peaks(corner_harris(image),min_distance=2)
# show_corners(corners,image)
'''
===============================
bag of words
===============================
'''
# from sklearn.feature_extraction.text import CountVectorizer
# corpus=["UNC played Duke in basketball,Duke lost the basketball game,the game was fixed"]
#
# vectorizer = CountVectorizer(stop_words="english")
# print vectorizer.fit_transform(corpus).todense()
# print vectorizer.vocabulary_
'''
===============================
kmeans
===============================
'''
# import numpy as np
# from sklearn.cluster import KMeans
# from sklearn import metrics
# import matplotlib.pyplot as plt
# plt.subplot(3,2,1)
# x1 = np.array([1,2,3,1,5,6,5,5,6,7,8,9,7,9])
# x2 = np.array([1,3,2,2,8,6,7,6,7,1,2,1,1,3])
#
# X = np.array(zip(x1,x2)).reshape(len(x1),2)
# print X
#
# plt.xlim(0,10)
# plt.ylim(0,10)
# plt.title('instnces')
#
# plt.scatter(x1,x2)
#
# # plt.show()
#
# colors = ['b','g','r','c','m','y','k','b']
# markers = ['o','s','D','v','^','p','*','+']
# tests =[2,3,4,5,8]
#
# subplot_counter = 1
# for t in tests:
# subplot_counter+=1
# plt.subplot(3,2,subplot_counter)
# kmeans_model = KMeans(n_clusters=t).fit(X)
# for i,l in enumerate(kmeans_model.labels_):
# plt.plot(x1[i],x2[i],color=colors[l],marker = markers[l],ls='None')
# plt.xlim([0,10])
# plt.ylim([0,10])
# plt.title('k=%s, silhoutee=%0.3f' %(t,metrics.silhouetee_score(X,kmeans_model.labels_,metric='euclidian')))
# plt.show()
#
#suft feature extraction
# import mahotas as mh
# from mahotas.features import surf
#
# image = mh.imread('new.jpg',as_grey=True)
# print 'The first SURF description:',surf.surf(image)[0]
# print 'extracted SURF description',len(surf.surf(image)[0])
'''
===============================
pca
===============================
'''
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
data = load_iris()
y = data.target
X = data.data
# print y
print X
pca = PCA(n_components=2)
reduced_x = pca.fit_transform(X)
print reduced_x
red_x,red_y = [],[]
blue_x,blue_y = [],[]
green_x,green_y = [],[]
for i in range(len(reduced_x)):
if y[i] ==0:
red_x.append(reduced_x[i][0])
red_y.append(reduced_x[i][1])
elif y[i]==1:
blue_x.append(reduced_x[i][0])
blue_y.append(reduced_x[i][1])
else:
green_x.append(reduced_x[i][0])
green_y.append(reduced_x[i][1])
plt.scatter(red_x,red_y,c='r',marker='x')
plt.scatter(blue_x,blue_y,c='b',marker='D')
plt.scatter(green_x,green_y,c='g',marker='.')
plt.show()