統(tǒng)計(jì)學(xué)習(xí)方法第二章:感知機(jī)(perceptron)算法及python實(shí)現(xiàn)
統(tǒng)計(jì)學(xué)習(xí)方法第三章:k近鄰法(k-NN),kd樹(shù)及python實(shí)現(xiàn)
統(tǒng)計(jì)學(xué)習(xí)方法第四章:樸素貝葉斯法(naive Bayes)恃锉,貝葉斯估計(jì)及python實(shí)現(xiàn)
統(tǒng)計(jì)學(xué)習(xí)方法第五章:決策樹(shù)(decision tree),CART算法,剪枝及python實(shí)現(xiàn)
統(tǒng)計(jì)學(xué)習(xí)方法第五章:決策樹(shù)(decision tree),ID3算法肪跋,C4.5算法及python實(shí)現(xiàn)
歡迎關(guān)注公眾號(hào):常失眠少年土砂,大學(xué)生的修煉手冊(cè)!
完整代碼:
https://github.com/xjwhhh/LearningML/tree/master/StatisticalLearningMethod
歡迎follow和star
感知器(perceptron)是二類分類的線性分類模型吴叶,其輸入為實(shí)例的特征向量序臂,輸出為實(shí)例的類別,取+1和-1二值逊彭。
感知器對(duì)應(yīng)于輸出空間(特征空間)中將實(shí)例劃分為正負(fù)兩類的分離超平面构订,屬于判別模型。感知器學(xué)習(xí)旨在求出將訓(xùn)練數(shù)據(jù)進(jìn)行線性劃分的分離超平面签赃,為此分尸,導(dǎo)入基于誤分類的損失函數(shù),利用梯度下降法對(duì)損失函數(shù)進(jìn)行極小化孔庭,求得感知機(jī)模型。
感知器學(xué)習(xí)算法具有簡(jiǎn)單而易于實(shí)現(xiàn)的優(yōu)點(diǎn)圆到,分為原始形式和對(duì)偶形式。
感知器預(yù)測(cè)是用學(xué)習(xí)得到的感知機(jī)模型對(duì)新的輸入實(shí)例進(jìn)行分類马绝。
下圖是感知機(jī)學(xué)習(xí)算法的原始形式:
更詳細(xì)的說(shuō)明和證明可以在《統(tǒng)計(jì)學(xué)習(xí)方法》或者其他博客里看到富稻,我在這里不再贅述白胀,直接看代碼。
我的python實(shí)現(xiàn)也是基于這個(gè)算法哪怔,使用的是MINST數(shù)據(jù)集向抢,代碼如下:
<pre><code>
import pandas as pd
import random
import time
import logging
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
def log(func):
def wrapper(*args, *kwargs):
start_time = time.time()
logging.debug('start %s()' % func.name)
ret = func(args, **kwargs)
end_time = time.time()
logging.debug('end %s(), cost %s seconds' % (func.__name__, end_time - start_time))
return ret
return wrapper
class Perceptron(object):
def __init__(self):
self.learning_step = 0.00001
self.max_iteration = 5000
def predict_(self, x):
wx = 0
for i in range(len(self.w)):
wx += self.w[i] * x[i]
return int(wx > 0)
@log
def train(self, features, labels):
# (1)
self.w = [0.0] * (len(features[0]) + 1)
correct_count = 0
while True:
# (2)
# 有可能隨機(jī)生成相同的數(shù)字,使得correct_count對(duì)一個(gè)數(shù)據(jù)有重復(fù)計(jì)算元暴,但無(wú)傷大雅
index = random.randint(0, len(labels) - 1)
x = list(features[index])
x.append(1.0)
if labels[index] == 1:
y = 1
else:
y = -1
wx = 0
for i in range(len(self.w)):
wx += self.w[i] * x[i]
# 驗(yàn)證正確
if wx * y > 0:
correct_count += 1
# 訓(xùn)練集大約有兩萬(wàn)多數(shù)據(jù)兄猩,這里可隨意取適宜的值鉴未,用來(lái)跳出while循環(huán)
if correct_count > 10000:
break
continue
# (3)
# 驗(yàn)證錯(cuò)誤铜秆,修改w值
for i in range(len(self.w)):
self.w[i] += self.learning_step * (y * x[i])
@log
def predict(self, features):
predict_labels = []
for feature in features:
x = list(feature)
x.append(1)
predict_labels.append(self.predict_(x))
return predict_labels
if name == 'main':
# 記錄
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
raw_data = pd.read_csv('../data/train_binary.csv', header=0)
data = raw_data.values
images = data[0:, 1:]
labels = data[:, 0]
# 選取 2/3 數(shù)據(jù)作為訓(xùn)練集, 1/3 數(shù)據(jù)作為測(cè)試集
train_features, test_features, train_labels, test_labels = train_test_split(
images, labels, test_size=0.33, random_state=1)
# 模型訓(xùn)練
p = Perceptron()
p.train(train_features, train_labels)
# 使用測(cè)試集預(yù)測(cè)
test_predict = p.predict(test_features)
# 計(jì)算準(zhǔn)確率
# 因?yàn)槭请S機(jī)的核蘸,每次得到的準(zhǔn)確率都不同
score = accuracy_score(test_labels, test_predict)
print("The accuracy score is ", score)
</code></pre>
結(jié)果如下啸驯,還是比較準(zhǔn)確的
水平有限罚斗,如有錯(cuò)誤,希望指出