np.linalg.inv(Sw)
时间: 2023-11-19 16:28:16 浏览: 146
这段代码使用了 NumPy 库中的 linalg.inv() 函数,用于计算矩阵的逆。
具体来说,Sw 是一个矩阵,调用 linalg.inv(Sw) 将返回 Sw 的逆矩阵。
需要注意的是,只有方阵才有逆矩阵。如果 Sw 不是方阵,那么 linalg.inv() 函数将会抛出一个 LinAlgError 异常。如果 Sw 是奇异矩阵,也就是行列式为 0 的矩阵,那么 linalg.inv() 函数将会抛出一个 LinAlgError 异常。因此,在使用 linalg.inv() 函数之前,需要确保输入的矩阵 Sw 是方阵且非奇异矩阵。
相关问题
把这个代码改写成Python语言 W = Sw \ (mu1 - mu2)';
Sure! 这是 Python 代码:
```python
import numpy as np
# Sw, mu1, and mu2 are assumed to be defined beforehand
W = np.dot(np.linalg.inv(Sw), (mu1 - mu2).T)
```
.有以下三类样本,根据Fisher线性判决的原理,以每两类样本为1组,对样本进行分类,并测试xx1(-0.7,0.58,0.089)和xx2(0.047,-0.4,1.04)属于哪一类。画出图像,并给出代码
三类样本如下:
类别1:(-0.42,0.58,0.089)、(0.27,-0.45,-0.087)、(-0.39,0.17,-0.901)、(-0.13,0.31,0.61)、(-0.41,0.12,0.81)、(0.16,0.09,0.61)、(-0.23,0.15,0.057)、(0.27,0.3,0.87)、(-0.16,-0.07,0.21)、(-0.04,-0.17,0.81)
类别2:(-0.72,-0.82,-0.69)、(-1.47,-1.38,-0.12)、(0.18,-0.11,-0.77)、(-0.11,-0.3,-0.22)、(-0.46,-0.03,-0.22)、(-0.08,-0.23,-0.11)、(0.31,-1.08,-0.32)、(0.0,-0.13,-0.34)、(-0.43,-0.52,0.65)、(0.09,-0.21,0.05)
类别3:(1.22,1.27,0.89)、(0.69,1.27,0.99)、(1.03,0.94,0.55)、(1.16,1.03,0.64)、(0.51,0.39,-0.49)、(1.19,0.79,-0.03)、(0.49,0.99,0.91)、(0.99,0.87,1.18)、(0.59,0.63,0.27)、(1.0,0.61,0.68)
根据Fisher线性判决的原理,我们需要进行以下步骤:
1. 计算每一类样本的均值向量和协方差矩阵
2. 计算总体均值向量和总体协方差矩阵
3. 计算投影向量w
4. 对测试样本进行分类
代码如下:
import numpy as np
import matplotlib.pyplot as plt
# 定义三类样本
class1 = np.array([[-0.42,0.58,0.089],[0.27,-0.45,-0.087],[-0.39,0.17,-0.901],[-0.13,0.31,0.61],[-0.41,0.12,0.81],[0.16,0.09,0.61],[-0.23,0.15,0.057],[0.27,0.3,0.87],[-0.16,-0.07,0.21],[-0.04,-0.17,0.81]])
class2 = np.array([[-0.72,-0.82,-0.69],[-1.47,-1.38,-0.12],[0.18,-0.11,-0.77],[-0.11,-0.3,-0.22],[-0.46,-0.03,-0.22],[-0.08,-0.23,-0.11],[0.31,-1.08,-0.32],[0.0,-0.13,-0.34],[-0.43,-0.52,0.65],[0.09,-0.21,0.05]])
class3 = np.array([[1.22,1.27,0.89],[0.69,1.27,0.99],[1.03,0.94,0.55],[1.16,1.03,0.64],[0.51,0.39,-0.49],[1.19,0.79,-0.03],[0.49,0.99,0.91],[0.99,0.87,1.18],[0.59,0.63,0.27],[1.0,0.61,0.68]])
# 计算每一类样本的均值向量和协方差矩阵
mean_class1 = np.mean(class1, axis=0)
mean_class2 = np.mean(class2, axis=0)
mean_class3 = np.mean(class3, axis=0)
cov_class1 = np.cov(class1.T)
cov_class2 = np.cov(class2.T)
cov_class3 = np.cov(class3.T)
# 计算总体均值向量和总体协方差矩阵
total_mean = np.mean(np.concatenate((class1, class2, class3), axis=0), axis=0)
total_cov = np.cov(np.concatenate((class1, class2, class3), axis=0).T)
# 计算投影向量w
Sw = cov_class1 + cov_class2 + cov_class3
w = np.dot(np.linalg.inv(Sw), (mean_class1 - mean_class2 + mean_class3))
# 对测试样本进行分类
x1 = np.array([-0.7, 0.58, 0.089])
x2 = np.array([0.047, -0.4, 1.04])
g1 = np.dot(w.T, x1) - np.dot(w.T, total_mean)
g2 = np.dot(w.T, x2) - np.dot(w.T, total_mean)
if g1 > g2:
print("x1属于类别1")
else:
print("x1属于类别2")
if g2 > g1:
print("x2属于类别2")
else:
print("x2属于类别1")
# 画出图像
plt.scatter(class1[:, 0], class1[:, 1], color='r', label='class1')
plt.scatter(class2[:, 0], class2[:, 1], color='b', label='class2')
plt.scatter(class3[:, 0], class3[:, 1], color='g', label='class3')
plt.plot([-w[1], w[1]], [w[0], -w[0]], color='k', label='Fisher')
plt.legend()
plt.show()
阅读全文