首页 新闻 会员 周边

python +=与=+

0
悬赏园豆:100 [待解决问题]
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")

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

woria的主页 woria | 初学一级 | 园豆:102
提问于:2019-06-08 18:32
< >
分享
所有回答(1)
0
jello chen | 园豆:7336 (大侠五级) | 2019-06-09 11:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册