Skip to content

Commit f6c887e

Browse files
author
quangh
committed
ML - least square
1 parent 5daac85 commit f6c887e

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import numpy as np
2+
from numpy import linalg as la
3+
import matplotlib.pyplot as plt
4+
5+
X = np.array([[147, 150, 153, 158, 163, 165, 168, 170, 173, 175, 178, 180, 183]]).T
6+
y = np.array([[49, 50, 51, 54, 58, 59, 60, 62, 63, 64, 66, 67, 68]]).T
7+
plt.plot(X,y,'ro')
8+
plt.axis([140, 190, 45, 75])
9+
#plt.show()
10+
11+
one = np.ones((X.shape[0],1))
12+
13+
X = np.hstack((one,X))
14+
15+
#print X
16+
17+
A = np.dot(X.T,X)
18+
b = np.dot(X.T,y)
19+
w = np.dot(la.pinv(A),b)
20+
21+
print "w= " + str(w)
22+
23+
x0 = np.linspace(145,185,2)
24+
y0 = w[0][0] + w[1][0]*x0
25+
26+
plt.plot(x0,y0)
27+
plt.show()

Machine Learning/practicing numpy.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
from numpy import linalg as la
3+
# a = np.arange(20).reshape(4,5)
4+
# print a
5+
# print a.ndim
6+
# print a.shape[0]
7+
# print a.size
8+
#
9+
# b = np.array([[5,6],[1,2,3]])
10+
# print b
11+
#
12+
# c = np.zeros((3,4))
13+
# print c
14+
#
15+
# d = np.ones((3,4))
16+
# print d
17+
#
18+
# e = np.arange(10,20,3)
19+
# print e
20+
#
21+
# print np.pi
22+
23+
a = np.arange(15).reshape(3,-1)
24+
# print a.astype(float)
25+
#
26+
# print np.random.random((3,4))
27+
# print np.random.rand(3)
28+
print a.ravel()
29+
print a
30+
print a.T
31+
32+
c = np.floor(10*np.random.random((2,4)))
33+
d = np.floor(10*np.random.random((2,4)))
34+
# print c
35+
# print d
36+
# print np.vstack((c,d))
37+
# print np.vstack((d,c))
38+
39+
# print np.concatenate((c,d), axis=1)
40+
41+
a = np.floor(10*np.random.random((4,4)))
42+
print a
43+
print la.inv(a)
44+
print np.eye(5,5)
45+
print np.dot(a,a)
46+
print np.trace(a)

0 commit comments

Comments
 (0)