一开始用python写的,后来发现要延伸到二维的贝叶斯分类,然后想画一个二元正态分布图就很难....后来改用matlab
基础知识:Bayesian Principle:(查询书《模式分类》第二章
Task 1
According to the above principle and theory in section 2.2, design a Bayesian classifier for the classification of two classes of patterns which are subjected to Gaussian normal distribution and compile the corresponding programme codes.
What we have: 1. two classes and the data in this two classes; 2. four loss paras 3.prior probability of two classes
Teacher has given us the data ,the loss parameters and the prior probability. So we just need to calculate the P(x|wi) to get the P(wi|x). I decided to use the normal distribution to calculate the conditional probability(the parameters of the normal distribution were calculated according to the given data). Here is my code:
import math import matplotlib.pyplot as plt import numpy as np #'''This part is used to define classes which has attributes the average and the variance''' class w: def __init__(self, data, prob): self.mu = 0 self.sigma = 0 self.data = data # a list of data self.priProb = prob # prior probability bar = 0 for i in self.data: self.mu += i self.mu = self.mu / len(self.data) for i in self.data: bar += (i - self.mu) ** 2 self.sigma = math.sqrt(bar / len(self.data)) #'''used to caculate the conditional probability''' def condProb(mu, sigma, x): # input the number and return the conditional prob condp = 1/(2.5066*sigma) * math.exp(-0.5*(x-mu)**2/sigma**2) return condp
#'''calculate the posterior probability''' def postProb(x, w1, w2): # input the number and the class, suppose there are two classes num = condProb(w1.mu, w1.sigma, x) * w1.priProb den = condProb(w1.mu, w1.sigma, x) * w1.priProb + condProb(w2.mu, w2.sigma, x) * w2.priProb return num/den
#'''The most important part: smallest risk Bayesian Classifier'''
def classify(x, cls): # a list of unclassified object, two classes result = [] for i in x: risk = [] risk.append(condProb(cls[0].mu, cls[0].sigma,i) * cls[0].priProb * 6) risk.append(condProb(cls[1].mu, cls[1].sigma,i) * cls[1].priProb * 1) if min(risk) == risk[0]: result.append(2) else: result.append(1) return result
下次再更二维空间上的贝叶斯分类器设计,比较复杂,我还要去复习复习基础知识....