python +=和=+

import numpy as np
import pandas as pd
import requests


def getdata(url):
    content=requests.get(url).content
    content=content.decode("utf-8")
    content=content.split("\n")
    X = []
    Y = []
    for line in content[:-1]:
        x1,y1 = line.split("\t")
        x1 = x1.split()
        for i in range(len(x1)):
            x1[i] = float(x1[i])
        X.append([1]+x1)
        Y.append(int(y1))
    X=np.array(X)
    Y=np.array(Y)
    return X,Y


def init(X):
    w = np.array([0]*len(X[1]),dtype=float)
    return w


def inter_iter(X,Y,w):
    OK = True
    for i in range(len(X)):
        mul = np.dot(w,X[i])
        if mul<0:
            res = -1
        else :
            res = 1
        if res != Y[i]:
            OK = False
            w += Y[i]*X[i]
    return OK


def PLA(url):
    X,Y = getdata(url)
    w = init(X)
    cnt = 1
    while (inter_iter(X,Y,w)!=True):
        cnt += 1
    print(cnt)


if __name__=='__main__':
    PLA("https://www.csie.ntu.edu.tw/~htlin/mooc/datasets/mlfound_math/hw1_15_train.dat")

第43行w += Y[i]*X[i]如果改成w = w + Y[i]*X[i]就会出错,刚开始数字还对,循环几次之后答案会不对了,有大佬知道为什么吗?

转载于:https://www.cnblogs.com/woria/p/10991311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值