统计学习方法例2.1实现(转)

对应李航《统计学习方法》
P29的
例2.1

# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# @Author: appleyuchi
# @Date:   2018-10-02 21:54:30
# @Last Modified by:   appleyuchi
# @Last Modified time: 2018-10-02 21:55:51
#-*- coding:utf-8 -*-
import os
import sys
from numpy import *

reload(sys)
sys.setdefaultencoding('utf-8')
#首先将书上的训练集输入
def loadDataset():
    dataset=[[3,3],[4,3],[1,1]]
    labels=[1,1,-1]
    return dataset,labels

#定义sign函数
def function_sign(x):
    if x>=0:return 1
    else:return -1

#算法主程序
def perceptron(dataSet,labels):
    dataMat=mat(dataSet) #将数据集转换成矩阵用于计算
    m,n=shape(dataMat)
    w=mat(zeros((n,1)));b=0 #创建与示例维度相同的初始化w
    while([function_sign(i) for i in array(dataMat * w+b)]) != labels:  #当预测结果与实际结果不符时则循环更新我,w,b
        for i in xrange(m):
            if labels[i]*(dataMat[i]*w+b)<=0:
                w += labels[i]*dataMat[i].T
                b += labels[i]
    return w,b
if __name__ == '__main__':
    dataSet,labels=loadDataset()
    w,b=perceptron(dataSet,labels)
    print w,'\n',b


你可能感兴趣的:(机器学习算法)