1. 程式人生 > >機器學習基石作業三python實現

機器學習基石作業三python實現

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

問題6,7,8,10程式碼實現如下:

import numpy as np

def E(u,v):
    return np.exp(u)+np.exp(2*v)+np.exp(u*v)+u*u-2*u*v+2*v*v-3*u-2*v
def gradU(func,u,v):
    return (func(u+0.0001,v)-func(u-0.0001,v))/0.0002
def gradV(func,u,v):
    return (func(u,v+0.0001)-func(u,v-0.0001))/0.0002
def pro6():
    print(gradU(E,0,0),gradV(E,0,0))
def pro7(N):
    u0=0
    v0=0
    eta=0.01
    u=u0
    v=v0
    for i in range(N):
        u0-=eta*gradU(E,u,v)
        v0-=eta*gradV(E,u,v)
        u=u0
        v=v0
    print(E(u,v))
def pro8():
    u=0
    v=0
    b=E(u,v)
    bv=gradV(E,u,v)
    bu=gradU(E,u,v)
    buu=gradU(lambda u,v:gradU(E,u,v),u,v)#lambda u,v;gradU(E,u,v)先生成E關於u,v的函式,而不是帶入u,v的值
    buv=gradU(lambda u,v:gradV(E,u,v),u,v)
    bvu=gradV(lambda u,v:gradU(E,u,v),u,v)
    bvv=gradV(lambda u,v:gradV(E,u,v),u,v)
    print(buu/2,bvv/2,(bvu+buv)/2,bu,bv,b)
def pro10(N):
    u,v=0,0
    for i in range(N):
        bv = gradV(E, u, v)
        bu = gradU(E, u, v)
        buu = gradU(lambda u, v: gradU(E, u, v), u, v)  # lambda u,v;gradU(E,u,v)先生成E關於u,v的函式,而不是帶入u,v的值
        buv = gradU(lambda u, v: gradV(E, u, v), u, v)
        bvu = gradV(lambda u, v: gradU(E, u, v), u, v)
        bvv = gradV(lambda u, v: gradV(E, u, v), u, v)
        hession=np.mat([[buu,buv],[bvu,bvv]])
        grad=np.array([[bu],[bv]])
        delta=hession.I*grad
        delta=delta.tolist()
        u-=delta[0][0]
        v-=delta[1][0]
    print(E(u,v))
if __name__=='__main__':
    pro6()
    pro7(5)
    pro8()
    pro10(5)

在這裡插入圖片描述

import numpy as np
import random

def sign(x):
    if x>=0:
        return 1
    else:
        return -1

def func(x1,x2):
    return sign(x1*x1+x2*x2-0.6)

def generateX(N):
    X=[]
    Y=[]
    for i in range(N):
        x1=random.uniform(-1,1)
        x2=random.uniform(-1,1)
        y=func(x1,x2)
        noise=random.random()
        if(noise<=0.1):
            y=-y
        X.append([1,x1,x2])
        Y.append([y])
    return np.mat(X),np.array(Y)

def tranLR(x,y):
    pseudo_inverse=np.linalg.pinv(x)
    w=pseudo_inverse*y
    return np.array(w)

def error(w,x,y):
    y1=x*w
    y1=list(map(sign,y1))
    y = list(map(lambda x: x[0], y))
    errorNum=np.sum(np.array(y1)!=np.array(y))
    return errorNum/len(y)

if __name__=='__main__':
    errorSum=0
    for i in range(1000):
        x,y=generateX(1000)
        w=tranLR(x,y)
        errorSum+=error(w,x,y)
    print(errorSum/1000)

在這裡插入圖片描述

import numpy as np
import random

def sign(x):
    if x>=0:
        return 1
    else:
        return -1

def func(x1,x2):
    return sign(x1*x1+x2*x2-0.6)

def generateX(N):
    X=[]
    Y=[]
    for i in range(N):
        x1=random.uniform(-1,1)
        x2=random.uniform(-1,1)
        y=func(x1,x2)
        noise=random.random()
        if(noise<=0.1):
            y=-y
        X.append([1,x1,x2,x1*x2,x1*x1,x2*x2])
        Y.append([y])
    return np.mat(X),np.array(Y)

def tranLR(x,y):
    pseudo_inverse=np.linalg.pinv(x)
    w=pseudo_inverse*y
    return np.array(w)

def error(w,x,y):
    y1=x*w
    y1=list(map(sign,y1))
    y = list(map(lambda x: x[0], y))
    errorNum=np.sum(np.array(y1)!=np.array(y))
    return errorNum/len(y)

if __name__=='__main__':
    errorSum=0
    for i in range(1000):
        x,y=generateX(1000)
        w=np.array([[-1],[-0.05],[0.08],[0.13],[1.5],[1.5]])
        errorSum+=error(w,x,y)
    print(errorSum/1000)

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

18.19.20題,程式碼相近,只需要稍微改動即可。

import numpy as np

def getData(path):
    dataMat=[]
    with open(path,'r') as fr:
        rawData=fr.readlines()
    dataMatrix = np.zeros((len(rawData), len(rawData[0].strip().split())))
    labelMat = np.zeros((len(rawData),1))
    for i in range(len(rawData)):
        lineArr=rawData[i].strip().split()
        dataMat.append(1.0)
        dataMat.extend(list(map(float,(lineArr[0:-1]))))
        dataMatrix[i,:]=dataMat
        dataMat=[]
        labelMat[i]=int(lineArr[-1])
    return dataMatrix,labelMat
def sigmoid(x):
    return 1.0/(1+np.exp(-x))
def gradient(dataMat,labelMat):
    T=2000
    eta=0.01
    weights=np.ones((1,len(dataMat[0,:])))
    for i in range(T):
        tmp = -labelMat * (np.dot(dataMat, weights.transpose()))
        gradE = (np.sum(sigmoid(tmp)*-labelMat*dataMat,axis=0).reshape(1,len(weights.T)))/len(labelMat)
        weights-=eta*gradE
    return weights
def stochaticGradient(dataMat,labelMat):
    T=2000
    eta=0.001
    weights=np.ones((1,len(dataMat[0,:])))
    j=0
    for i in range(T):
        tmp = -labelMat[j] * (np.dot(dataMat[j,:], weights.transpose()))
        gradE = sigmoid(tmp) * -labelMat[j] * dataMat[j,:]
        weights -= eta * gradE
        j+=1
        j%=len(labelMat)
    return weights
if __name__=='__main__':
    dataMat,labelMat=getData('hw3_train.dat.txt')
    weights=stochaticGradient(dataMat,labelMat)
    dataTestMat,labelTestMat=getData('hw3_test.dat.txt')
    sum=0
    label=[]
    for i in range(len(dataTestMat)):
        value=np.dot(dataTestMat[i,:],np.transpose(weights))
        if sigmoid(value)>=0.5:
            label.append(1)
        else:
            label.append(-1)
    for i in range(len(label)):
        if labelTestMat[i]!=label[i]:
            sum+=1
    print(sum/len(label))