您的位置:首页 > 其它

使用感知机训练加法模型

2017-03-31 22:27 183 查看
感知机此处不介绍,这里只是简单的做了一个使用感知机思路,训练一个y=a+b计算模型.

# -*-coding:utf-8-*-
'@author: xijun.gong'
import numpy as np
import random
import math

class Perceptron:
def __init__(self, learnRate, maxIter, bit_len):
"""
:param bit_len
:param learnRate:
:param maxIter:  最大迭代次数
"""
self.learmRate = learnRate;
self.weight = None;
self.maxIter = maxIter;
# produce map
self.bit_len = bit_len;
self.nummap = None;
self.initMap()
pass

def initMap(self):
maxNum = (1 << self.bit_len);  # 该位数下的最大值
self.nummap = np.zeros((maxNum, self.bit_len), dtype=np.int);  # include zero
for _id in xrange(maxNum):
for index in xrange(self.bit_len):
self.nummap[_id][index] = 1 & (_id >> index);
pass

def initWeight(self):
"""
:return:
"""
self.weight = np.ones(self.bit_len) / self.bit_len;

def fit(self, fds, labels):
"""
:param fds: 训练样本集合
:param labels:
:return:
"""
feature_nums = fds.shape[1]  # 样本中的特征参数数量
self.initWeight()
for iter in xrange(self.maxIter):
print 'train as iter is {} '.format(iter)
acc_cnt = 0
for _ind, sample in enumerate(fds):
a = self.nummap[int(sample[0])];
b = self.nummap[int(sample[1])];
label_y = sum(self.weight * (a + b))
# 计算var_w 表示倒三角w
print 'the reality:{} , predict {}'.format(labels[_ind], label_y);
if math.fabs(labels[_ind] - label_y) <= 0.000001:
acc_cnt += 1;
continue;
var_w = self.learmRate * (labels[_ind] - label_y) * (a + b)
self.weight += var_w;
print 'accuary is {}'.format(acc_cnt / (len(fds) * 1.0))
if acc_cnt == len(fds):
np.save('weight.npy', {'weight': self.weight});
return;
pass

def load(self, path='weight.npy'):
return np.load(path)['weight']

def predict(self, fd):
a = self.nummap[fd[0]];
b = self.nummap[fd[1]];
return sum(self.weight * (a + b))

def predict_prod(self):
pass

if __name__ == '__main__':
import time

perceptron = Perceptron(learnRate=0.01, maxIter=2000, bit_len=5);
xa = np.arange(31);
xb = np.zeros(31);
labels = np.zeros(31)
for i in xrange(31):
xb[i] = random.randint(0, (int(time.time() + 1)) % 31)
labels[i] = xb[i] + xa[i]
perceptron.fit(np.array([xa, xb]).T, labels)
print 'predict is {}'.format(perceptron.predict([24, 13]))


运行结果:

train as iter is 277
the reality:0.0 , predict 0.0
the reality:16.0 , predict 16.0000005749
the reality:16.0 , predict 15.9999994995
the reality:3.0 , predict 3.00000059084
the reality:18.0 , predict 17.999999818
the reality:15.0 , predict 15.0000000195
the reality:20.0 , predict 19.9999998534
the reality:22.0 , predict 22.0000009642
the reality:10.0 , predict 9.99999911021
the reality:22.0 , predict 21.9999996143
the reality:23.0 , predict 22.9999990943
the reality:17.0 , predict 17.0000000549
the reality:25.0 , predict 24.9999994128
the reality:18.0 , predict 18.0000008934
the reality:20.0 , predict 19.9999998534
the reality:15.0 , predict 15.0000000195
the reality:27.0 , predict 26.999999038
the reality:31.0 , predict 30.9999993919
the reality:25.0 , predict 25.0000003525
the reality:21.0 , predict 20.9999999986
the reality:35.0 , predict 34.9999997457
the reality:29.0 , predict 28.9999993564
the reality:39.0 , predict 38.9999996894
the reality:26.0 , predict 26.0000009079
the reality:31.0 , predict 30.9999993919
the reality:25.0 , predict 24.9999990026
the reality:33.0 , predict 32.9999994273
the reality:32.0 , predict 31.9999999473
the reality:32.0 , predict 31.9999991549
the reality:34.0 , predict 34.0000002657
the reality:33.0 , predict 32.9999994273
accuary is 1.0
predict is 36.9999984312
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐