zoukankan      html  css  js  c++  java
  • 《机器学习》第三章——对率回归

    
    
    import numpy as np
    from numpy import random
    def dataload(filename,l,r):#导入数据,感觉导入的有点困难
        f=open(filename)
        ar=f.readlines()
        num=len(ar)
        mat=np.zeros((r-l+1,num))
        ind=0
        for line in ar:
            line.split('
    ')
            linelist=line.split(' ')
            mat[0:r-l,ind]=linelist[l:r]
            mat[r-l:r-l+1,ind]=1.0
            ind=ind+1
        return mat
    x=dataload("1.txt",0,2)
    y=dataload("1.txt",2,3)
    beta=random.random(size=(3,1))#随机生成初始的beta矩阵
    def p1(mat,p):
        ha=np.dot(mat.T,x[:,p])
        return np.exp(ha)/(1+np.exp(ha))
    def one(mat):#求关于beta函数的一阶导
        tep=np.zeros((3,1))
        for i in range(17):
            temp=np.zeros((3,1))
            for j in range(3):
                temp[j,0]=x[j,i]
            tep=tep+temp*(y[0,i]-p1(mat,i))
        return -1.0*tep
    def two(mat):#二阶导
        tep=np.zeros((3,3))
        for i in range(17):
            temp=np.zeros((3,1))
            for j in range(3):
                temp[j,0]=x[j,i]
            tep=tep+np.dot(temp,temp.T)*p1(mat,i)*(1-p1(mat,i))
        return tep
    cnt=10000
    for i in range(cnt):#使用牛顿法迭代cnt次得到beta矩阵
        tep=two(beta)
        if(np.linalg.det(tep)==0):
            break
        else :
            tep=np.linalg.inv(tep)
            beta=beta-np.dot(tep,one(beta))
    ans=np.dot(beta.T,x)
    def sigmoid(p):#sigmoid 函数
        return 1.0/(1+np.exp(-p))
    for i in range(17):
        print(sigmoid(ans[0,i]))
    这是用对率回归得到的数据集3.0的结果,第一次写python的代码,好多东西不会,希望有大神能指点指点我,感觉没人交流我都不知到我自学的对不对,毕竟学习能力不行
  • 相关阅读:
    leetcode刷题11. 盛最多水的容器
    docker报错Service 'pwn_deploy_chroot' failed to build: Get https://registry-1.docker.io/v2/library/ubuntu/manifests/16.04:net/http: request canceled
    常用断点记录
    c++继承学习
    leetcode刷题正则表达式
    x64类型的程序逆向思考
    vs2013下配置x64版c++
    MFC学习RepositionBars
    flask权限控制
    leetcode刷题七<整数反转>
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/7071540.html
Copyright © 2011-2022 走看看