机器学习复现1.简单感知机

感知机根据学习方法的不同分为原始形式和对偶形式。
更新的模型参数:
原始形式:权值w,偏置项b
对偶形式:学习过程中各训练样本误分类次数a,偏置项b。

##简单感知机的实现

import numpy as np

class model(object):
    '''
        model:根据策略得到的感知机模型 y = sign(w.x + b)
    '''
    w = np.array(object)
    b = 0

class perceptron(object):
    N = 2    ##特征空间为N维

    '''
    train_dataset:训练数据
    e.g. x1 = [3,3]T   y1 = 1(正样本)
         x2 = [4,3]T   y2 = 1(正样本)
         x3 = [1,1]T   y3 = -1(负样本)
    '''
    train_dataset_x = np.array([[3,3],[4,3],[1,1]])
    train_dataset_y = np.array([1 ,1 ,-1])
    model = model()


    def predict_oper(self,model,predict):
        return 1 if (np.dot(model.w,predict) + model.b) >= 0 else -1

	##感知机预测函数
    def predict(self,model,predict_x):
        if(len(predict_x)!=self.N):
            print("Improper sample size!\n")
        else:
            print(self.predict_oper(model = model,predict=predict_x))

class origin_perceptron(perceptron):
    '''
        普通形式感知机
    '''
    def loss_function(self,w,bias,sample):
        return -self.train_dataset_y[sample] * (np.dot(w,self.train_dataset_x[sample]) + bias)
    def train(self):
        ##初始化
        self.model.w = np.array([0] * self.N)
        self.model.b = 0

        learning_rate = 1
        correct_count = 0  ##分类正确数
        ##训练
        while correct_count < len(self.train_dataset_x):
            correct_count = 0
            for i in range(len(self.train_dataset_x)):
                if (self.loss_function(w = self.model.w, bias = self.model.b,sample=i) < 0):
                    ##正确分类
                    correct_count += 1
                    continue
                else:
                    ##误分类
                    ##更新a和bias
                    self.model.w += learning_rate * self.train_dataset_x[i] * self.train_dataset_y[i]
                    self.model.b += learning_rate * self.train_dataset_y[i]
                    correct_count = 0
                    break
        return

class dual_perceptron(perceptron):
    '''
        对偶形式感知机
    '''
    def gram(self):
    	'''
    		计算gram矩阵 
    		[[x1x1 x1x2 x1x3]
     		[x2x1 x2x2 x2x3]
     		[x3x1 x3x2 x3x3]]
    	'''
        gram = np.empty((len(self.train_dataset_x),len(self.train_dataset_x)))
        for i in range(len(self.train_dataset_x)):
            tmp = []
            for j in range(len(self.train_dataset_x)):
                tmp.append(np.dot(self.train_dataset_x[i],self.train_dataset_x[j]))
            gram[i] = tmp
        return gram

    def loss_function(self,gram,a,bias,sample):
        '''
        :param gram: gram矩阵
        :param a: a向量
        :param bias: 偏置项
        :param sample: 误分类样本
        :return: 对偶形式损失函数
        '''
        return -self.train_dataset_y[sample] *\
               (np.dot(a * self.train_dataset_y,gram[sample])+bias)

    def train(self):
        ##初始化
        a = [0] * len(self.train_dataset_x)
        bias = 0
        learning_rate = 1
        gram = self.gram()
        correct_count = 0   ##分类正确数
        ##训练
        while correct_count < len(self.train_dataset_x):
            correct_count = 0
            for i in range(len(self.train_dataset_x)):
                if(self.loss_function(gram = gram,a = a,bias = bias,sample = i) < 0):
                    ##正确分类
                    correct_count += 1
                    continue
                else:
                    ##误分类
                    ##更新a和bias
                    a[i] += learning_rate
                    bias += learning_rate * self.train_dataset_y[i]
                    correct_count = 0
                    break
        ##还原参数
        w = np.array([0] * self.N)
        for i in range(self.N):
            for j in range(len(self.train_dataset_x)):
                w[i] += a[j] * self.train_dataset_y[j] * self.train_dataset_x[j][i]
        self.model.w = w
        self.model.b = bias
        return
    pass

##使用
p = origin_perceptron()
p.train()
test_dataset_x = [1,0]
p.predict(p.model,test_dataset_x)

你可能感兴趣的:(机器学习,python,numpy)