感知机预测NBA总冠军

2018-06-17 23:46:06来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

import numpy as np
# 激活函数
def sign(x):
    if x>=0:
        return 1
    else:
        return 0
# 计算预测准确率函数
def score(x,y):
    z=0
    for i in range(len(x)):
        if x[i]==y[i]:
            z += 1
    print("准确率:",z/len(x))

# 训练样本 2000-2012年 NBA总决赛两只球队数据

#y_train[]中1表示胜,0表示负

#五项数据分别为[常规赛总胜场,常规赛交手战绩,队员最佳阵容积分,常规赛场均得分,常规赛场均失分]减去对方的五项数据


x_train=np.array([[-1,0,-1,-4.6,-4.4],[-1,2,-3,-1.9,1.4],[7,0,4,2.5,1.4],[6,-2,1,5.9,4.9],
         [9,2,1,-8.1,-11],[8,-2,1,1.7,-2.8],[-8,0,-1,0.8,2.9],[5,0,2,2.9,-1.1],[-2,0,-4,-8.1,-10],
         [11,0,1,0.3,0.3],[6,0,3,5.1,2.1],[0,0,0,5.9,6.8],[9,0,5,-0.5,-4.4],[1,0,1,4.6,4.4],
         [1,-2,3,1.9,-1.4],[-7,0,-4,-2.5,-1.4],[-6,2,-1,-5.9,-4.9],[-9,-2,-1,8.1,11],[-8,2,-1,-1.7,2.8],
         [8,0,1,-0.8,-2.9],[-5,0,-2,-2.9,1.1],[2,0,4,8.1,10],[-11,0,-1,-0.3,-0.3],[-6,0,-3,-5.1,-2.1],
         [0,0,0,-5.9,-6.8],[-9,0,-5,0.5,4.4]])
y_train=[1]*13+[0]*13

 

# 测试样本 2013-2017年 NBA总决赛两只球队数据


x_test=np.array([[16,0,2,5.6,-2.9],[-16,-2,-3,-10.6,-5.8],[14,0,0,6.9,1.2],[8,0,-1,3.2,0.2],[8,2,-1,-0.1,-1.6],[-16,0,-2,-5.6,2.9],
                 [16,2,3,10.6,5.8],[-14,0,0,-6.9,-1.2],[-8,0,1,-3.2,-0.2],[-8,-2,1,0.1,1.6]])
y_test=[1,1,1,1,1,0,0,0,0,0]
w=[0,0,0,0,0]

# 设置学习率
a = 0.1
c = 0
# 进行1000轮实验
while c < 1000:
    for i in range(len(x_train)):
        # 如果某次分类错误,则修改权值和偏差
        if y_train[i] * (np.sum(w * x_train[i]) ) <= 0:
            w += a * y_train[i] * x_train[i]
    c += 1
# 最后在测试集上检验正确率
count = 0
print("w =",w)

l=[]
# 检验
for i in range(len(x_test)):
    v=np.sum(w * x_test[i])
    l.append(sign(v))

print("真实结果:",y_test,"\n测试结果:",l)
score(y_test,l)

运行结果:

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:matplotlib坐标轴设置续-【老鱼学matplotlib】

下一篇:python-内置常量