用Python写个机器学习的线性回归算法的东西,在输出最佳拟合直线时报错
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
ax.plot(xCopy[:,1],yHat)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4137, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 295, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 213, in _xy_from_xy
bx = self.axes.xaxis.update_units(x)
File "C:\Python27\lib\site-packages\matplotlib\axis.py", line 1336, in update_units
converter = munits.registry.get_converter(data)
File "C:\Python27\lib\site-packages\matplotlib\units.py", line 148, in get_converter
converter = self.get_converter(xravel[0])
converter = self.get_converter(xravel[0])
File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 305, in __getitem__
out = N.ndarray.__getitem__(self, index)
RuntimeError: maximum recursion depth exceeded
shell上运行的代码执行到18行报错
后面发现是版本问题, 用1.3.0就好。
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
ax.plot(xCopy[:,1],yHat)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4137, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 295, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 213, in _xy_from_xy
bx = self.axes.xaxis.update_units(x)
File "C:\Python27\lib\site-packages\matplotlib\axis.py", line 1336, in update_units
converter = munits.registry.get_converter(data)
File "C:\Python27\lib\site-packages\matplotlib\units.py", line 148, in get_converter
converter = self.get_converter(xravel[0])
converter = self.get_converter(xravel[0])
File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 305, in __getitem__
out = N.ndarray.__getitem__(self, index)
RuntimeError: maximum recursion depth exceeded
开始以为是递归深度问题,后来加了sys.setrecursionlimit(10000)运行ax.plot(xCopy[:,1],yHat)然后IDE就restart了。。。
import os
os.chdir('F:\machinelearninginaction\Ch08')
import regression
from numpy import *
import sys
import matplotlib.pyplot as plt
xArr,yArr = regression.loadDataSet('ex0.txt')
xMat = mat(xArr)
yMat = mat(yArr)
ws = regression.standRegres(xArr,yArr)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xMat[:,1].flatten().A[0],yMat.T[:,0].flatten().A[0])
xCopy = xMat.copy()
xCopy.sort(0)
yHat = xCopy * ws
sys.setrecursionlimit(3000)
ax.plot(xCopy[:,1],yHat)
plt.show()
shell上运行的代码执行到18行报错
引用的regression算法
from numpy import *
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]))
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)
return ws
def lwlr(testPoint,xArr,yArr,k=1.0):
xMat = mat(xArr); yMat = mat(yArr).T
m = shape(xMat)[0]
weights = mat(eye((m)))
for j in range(m): #next 2 lines create weights matrix
diffMat = testPoint - xMat[j,:] #
weights[j,j] = exp(diffMat*diffMat.T/(-2.0*k**2))
xTx = xMat.T * (weights * xMat)
if linalg.det(xTx) == 0.0:
print "This matrix is singular, cannot do inverse"
return
ws = xTx.I * (xMat.T * (weights * yMat))
return testPoint * ws
后面发现是版本问题, 用1.3.0就好。