画贝叶斯分界线,贝叶斯分类,协方差相等和不相等

###第一题:协方差不相等时
import numpy as np
import matplotlib.pyplot as plt
import math

#画出数据散点图
DX1 = np.array([[1,1,2],[1,0,-1]])
DX2 = np.array([[-1,-1,-2],[1,0,-1]])
plt.scatter(DX1[0],DX1[1],c='yellow')
plt.scatter(DX2[0],DX2[1],c='blue')
#待分类点
def Display(m,n):
    plt.scatter(m,n,marker = '*',c='r')
    return
Display(2,0)

###第一类的均值、协方差、逆矩阵、行列式等值计算############
input = [[2],[0]]
a_x = np.matrix(np.cov(DX1))
a_cov_h1 = np.linalg.det(a_x)
a_cov_l = np.linalg.inv(a_x)
mean1 = np.matrix([[4/3],[0]])
####第二类的均值、协方差、逆矩阵、行列式等值计算###########
a_x2 = np.matrix(np.cov(DX2))
a_cov_h2 = np.linalg.det(a_x2)
a_cov_2 = a_x2.I
mean2 = np.matrix([[-4/3],[0]])

#判别函数
g = (1/2)*np.matrix(input -mean1).T*a_cov_l*np.matrix(input-mean1)-(1/2)*np.matrix(input - mean2).T * a_cov_2*np.matrix(input-mean2)+\
(1/2)*math.log(abs(a_cov_h1/a_cov_h2))
print('两类协方差不等时,g2-g1= %d'%g)
if g<0:
    print('X属于w1类')
else:
    print('X属于w2类')

######判别函数各系数
W1 = -1/2 * a_cov_l 
w1 = (a_cov_l * mean1).T
w10 = -1/2 * np.matrix(mean1).T * a_cov_l * mean1- 1/2 * math.log(abs(a_cov_h1))+ math.log(1/2)                                                                                              
########第二类
W2 = -1/2 * a_cov_2 
w2 = (a_cov_2 * mean2).T 
w20 = -1/2 * np.matrix(mean2).T * a_cov_2 * mean2 - 1/2 * math.log(abs(a_cov_h2)) + math.log(1/2)
#分界线各系数
W12 = W1 - W2#W第一个系数
w12 = w1 - w2#w第二个系数                                             
fxx = W12[0,0]
fyy = W12[1,1]
fxy = W12[0,1]+W12[1,0]
fx = w12[0,0]
fy = w12[0,1]
fc=w10-w20#第三个系数
           
print("协方差矩阵不等情况下,12分界线方程为:%f x1**2 + %f x2**2 + %f x1*x2 + %f x1 + %f x2 + %f = 0" %(fxx, fyy,fxy, fx, fy,fc))
x1 = np.arange(-5,5,0.01)
x2 = np.arange(-5,5,0.01)
x1, x2 = np.meshgrid(x1, x2)
f12 = fxx*(x1**2) + fyy*(x2**2) + fxy*(x1* x2) + fx*x1 + fy*x2 +fc
plt.xlabel('x1')
plt.ylabel('x2')
plt.title('covs is not equal')
plt.contour(x1, x2, f12, 0, colors='green')

 


#新的协方差
#画出数据散点图
import numpy as np
import matplotlib.pyplot as plt
import math

DX1 = np.array([[1,1,2],[1,0,-1]])
DX2 = np.array([[-1,-1,-2],[1,0,-1]])
plt.scatter(DX1[0],DX1[1],c='yellow')
plt.scatter(DX2[0],DX2[1],c='blue')
#待分类点
def Display(m,n):
    plt.scatter(m,n,marker = '*',c='r')
    return
Display(2,0)
a_xxx = a_x + a_x2 
a_cov_hhh = np.linalg.det(a_xxx)
a_cov_lll = np.linalg.inv(a_xxx)
g111 = -1/2 * np.matrix(input-mean1).T * a_cov_lll * np.matrix(input-mean1) + math.log(1/2)
print('两类协方差相等时,判别函数g111值= %d'%g111)

g222 = -1/2 * np.matrix(input-mean2).T * a_cov_lll * np.matrix(input-mean2) + math.log(1/3)
print('两类协方差相等时,判别函数g222值= %d'%g222)   
if   g111 > g222 :
    print('所以在协方差相等的情况下,(2, 0)属于第一类')
elif g111 < g222:
    print('所以在协方差相等的情况下,(2, 0)属于第二类')
       
#判决函数各系数
w11 = (a_cov_lll * mean1).T
w10_1 = -1/2 * np.matrix(mean1).T * a_cov_lll * mean1- 1/2 * math.log(abs(a_cov_hhh)) + math.log(1/3) 
w22 = (a_cov_lll * mean2).T 
w20_1 = -1/2 * np.matrix(mean2).T * a_cov_lll * mean2 - 1/2 * math.log(abs(a_cov_hhh)) + math.log(1/3)

#分界线各系数
w12_1 = w11 - w22                                             
fx12=w12_1[0,0]
fy12=w12_1[0,1]
fc12=w10_1-w20_1  
print("协方差矩阵相等情况下,12分界线方程为: %f x1 + %f x2 + %f = 0" % (fx12, fy12, fc12))


x1 = np.arange(-5,5,0.01)
x2 = np.arange(-5,5,0.01)
x1, x2 = np.meshgrid(x1, x2)
f121 = fx12*x1 + fy12*x2 +fc12
plt.xlabel('x1')
plt.ylabel('x2')
plt.title('cov is equal')
plt.contour(x1, x2, f121, 0, colors='green')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值