多元线性回归示例

今天主要说一下,监督学习里的多元线性回归模型算法的运用,下面就看一下这个例子:主要的例子来理解多元线性回归,公式代码相结合(参考斯坦福机器学习笔记):

import numpy as np
import matplotlib.pyplot as plt
'''训练集+测试集'''
filetran=ccpp_train.txt'
filetest='ccpp_test.txt'

datatran=np.loadtxt(filetran,delimiter=',')
datatest=np.loadtxt(filetest,delimiter=',')

tranX=datatran[:,:4]
tranNum=datatran.shape[0]
tranY=datatran[:,-1].reshape(tranNum,1)

testX=datatest[:,:4]
testNum=datatest.shape[0]
testY=datatest[:,-1].reshape(testNum,1)

tranMean=np.mean(tranX,0)
tranSig=np.std(tranX,0,ddof=1)

tranX-=np.tile(tranMean,(tranNum,1))
tranX/=np.tile(tranSig,(tranNum,1))

testX-=np.tile(tranMean,(testNum,1))
testX/=np.tile(tranSig,(testNum,1))

def costFunction(x,y,theta):
    m=y.shape[0]
    cost = np.sum((x.dot(theta)-y)**2)/(2*m)
    return cost

tranX=np.hstack((np.ones((tranNum,1)),tranX))
theta=np.array([1,1,1,1,1])
cost=costFunction(tranX,tranY,theta)
print("tran is cost=",cost)

def desgident(x,y,theta,a=0.01,iter_num=1000):
    m=y.shape[0]
    result=[]
    change=np.zeros(iter_num)
    for i in range(iter_num):
        cost=costFunction(x,y,theta)
        change[i]=cost
        deal=x.T.dot(x.dot(theta)-y)
        theta-=(a/m)*deal
    result.append(theta)
    result.append(change)
    return result

theta=np.zeros((5,1))
result=desgident(tranX,tranY,theta)
print("预测的theta=",result[0].T)

tranTheta=result[0]
testX=np.hstack((np.ones((testNum,1)),testX))
twoValue=testX.dot(tranTheta)
difference=np.hstack((twoValue,testY,twoValue-testY))
print("预测值-实际值-差值")
print(difference)
plt.scatter(twoValue,testY,marker='o',c='r')
plt.show()

多元线性回归示例_第1张图片多元线性回归示例_第2张图片

你可能感兴趣的:(多元线性回归示例)