import numpy as np
class Solution:
def inverse_matrix(self, matrix):
u, s, vh = np.linalg.svd(matrix, full_matrices=False)
# print(u, s, vh)
c = np.linalg.multi_dot([u * s, vh])
inverse = np.linalg.multi_dot([vh.T * 1 / s, u.T])
return inverse
def linear_regression(self, test, data, label):
y = np.array(label)
x_test = np.array(test)
x = np.array(data)
row_nums = x.shape[0]
x_1 = np.ones(row_nums)
x = np.c_[x, x_1]
e = np.identity(row_nums)
w = np.linalg.multi_dot([self.inverse_matrix(np.dot(x.T, x) + 0.2 * e), x.T, y.T]) # 核心公式,参考 景老师 上课推导的公式
f_x = np.dot(x_test.T, w)
return f_x
线性回归 岭回归
最新推荐文章于 2024-10-30 16:52:22 发布