통계 학습 방법 제2장 예제 2.1 코드 실천
3973 단어 통계 학습 방법 예제 코드 실천
#-*- 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
# w,b
dataSet,labels=loadDataset()
w,b=perceptron(dataSet,labels)
print w,'
',b
:
[[ 1.]
[ 1.]]
-3
made by zcl at CUMT