worriia 2019-06-08 18:29 采纳率: 0%
浏览 135

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")

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

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-09 18:33
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗