import numpy as np
x_0 = [[1, 2, 3], [3, 1, 5]]
x_1 = [[2, 3, 4], [1, 3, 5]]
x_0 = np.array(x_0)
x_1 = np.array(x_1)
len_r_0 = x_0.shape[0]
len_r_1 = x_1.shape[0]
sum_vector_0 = sum_vector_1 = 0
for i in range(len_r_0):
sum_vector_0 += x_0[i]
u_0 = sum_vector_0 / len_r_0
for i in range(len_r_1):
sum_vector_1 += x_1[i]
u_1 = sum_vector_1 / len_r_1
def s_w(x_0, x_1):
sums_0 = np.zeros((x_0.shape[1], x_0.shape[1]))
sums_1 = np.zeros((x_1.shape[1], x_1.shape[1]))
for i in range(len_r_0):
sums_0 += (x_0[i] - u_0).T @ (x_0[i] - u_0)
for j in range(len_r_1):
sums_1 += (x_1[j] - u_1).T @ (x_1[j] - u_1)
sums = sums_0 + sums_1
return sums
def inverse_matrix(matrix): # 利用svd分解求逆矩阵
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
w = inverse_matrix(s_w(x_0, x_1)) @ (u_0 - u_1).T
print(w)
print(w @ u_0.T)
print(w @ u_1.T)
print(w @ np.array([1, 1, 1]))
LDA 线性判别分析
最新推荐文章于 2024-08-19 13:32:52 发布