09 ML standard regression

2016-06-07  本文已影响0人  peimin
1.png

来自: https://github.com/start-program/machinelearninginaction/blob/master/Ch08/regression.py

from numpy import *
import matplotlib.pyplot as plt

def loadDataSet(fileName):      #general function to parse tab -delimited floats
    numFeat = len(open(fileName).readline().split('\t')) - 1 #get number of fields 
    dataMat = []
    labelMat = []

    fr = open(fileName)
    for line in fr.readlines():
        lineArr =[]
        curLine = line.strip().split('\t')
        for i in range(numFeat):
            lineArr.append(float(curLine[i]))
        dataMat.append(lineArr)
        labelMat.append(float(curLine[-1])) # last is label
    return dataMat,labelMat

def standRegres(xArr,yArr):
    xMat = mat(xArr)
    yMat = mat(yArr).T

    xTx = xMat.T*xMat

    if linalg.det(xTx) == 0.0:
        print "This matrix is singular, cannot do inverse"
        return

    ws = xTx.I * (xMat.T*yMat) # calc ws
    return ws

xArr, yArr = loadDataSet('ex0.txt')
print('xArr', xArr[0:2])
print('yArr', yArr[0:2])

ws = standRegres(xArr, yArr)
print('ws', ws)

xMat = mat(xArr) # 2 dimension array to matrix
yMat = mat(yArr)

print('xMat', xMat[0:2])

fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xMat[:, 1].flatten().A[0], yMat.T[:, 0].flatten().A[0]) # scatter plot

xCopy = xMat.copy()
xCopy.sort(0)

yHat = xCopy * ws # calc y
ax.plot(xCopy[:, 1], yHat) # draw y line
plt.show()
上一篇 下一篇

猜你喜欢

热点阅读