summaryrefslogtreecommitdiff
path: root/regression
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2020-02-23 13:47:52 +0800
committerzhang <zch921005@126.com>2020-02-23 13:47:52 +0800
commit6cd95662a9f96438159b6b5245096f0e4d04c44a (patch)
tree718e494e623bfbbfbba0c0dfa1998f8e4976af36 /regression
parent3ae278f2024f193fe2feb8bde3788f1835777731 (diff)
最小二乘解析解
Diffstat (limited to 'regression')
-rw-r--r--regression/least_square_fit.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/regression/least_square_fit.py b/regression/least_square_fit.py
new file mode 100644
index 0000000..71900b9
--- /dev/null
+++ b/regression/least_square_fit.py
@@ -0,0 +1,14 @@
+import numpy as np
+from numpy.linalg import inv
+
+x = np.array([1, 2, 3, 4, 5]).reshape((-1, 1))
+y = np.array([2, 4, 5, 4, 5]).reshape((-1, 1))
+
+
+x_aug = np.hstack([np.ones_like(x), x])
+
+# (X^TX)^(-1)*X^T*y
+# y = 2.2 + 0.6*x
+beta = inv(x_aug.transpose().dot(x_aug)).dot(x_aug.transpose()).dot(y)
+
+print(beta)