1
我是python和编程的新手,正在通过“机器学习:算法透视”的方式工作。我被告知要对数据进行规范化处理,将其分为训练和测试数据,恢复测试向量,然后使用总和最小平方误差。我一直得到,AttributeError:函数'object has no attribute'linreg'
文件 “/用户/ shaune /升降梭箱/ Shaune博士/ auto-mpg.py”,第34行,在 的β= linreg.linreg(赖宁,traintgt)
AttributeError的:“功能
import os
import pylab as pl
import numpy as np
from pylab import *
from numpy import *
import linreg
os.chdir('/Users/shaune/Dropbox/Shaune PhD')
auto=np.loadtxt('auto-mpg.data.txt',comments='"')
#normalise the data
auto=(auto-auto.mean(axis=0))/auto.var(axis=0)
#seperate the training and testing data
trainin=auto[::2,:8]
testin=auto[1::2,:8]
traintgt=auto[::2,1:2]
testtgt=auto[1::2,1:2]
#recover the beta vector
def linreg(trainin,traintgt):
trainin=np.concatenate((trainin,-np.ones((np.shape(trainin)[0],1))),axis=1)
beta=np.dot(np.dot(np.linalg.inv(np.dot(np.transpose(trainin),trainin)),np.transpose(trainin)),traintgt)
traintgt=np.dot(trainin, beta)
#sum of squares error to get predicted values on test set (want small values)
beta=linreg.linreg(trainin,traintgt)
testin=concatenate((testin,-np.ones((np.shape(testin)[0],1))),axis=1)
testout=dot(testin,beta)
error=sum((testout-testtgt)**2)
print error
请帮助:运行以下时,'对象有没有属性 'linreg'
!谢谢。
非常感谢你。这工作!但我不明白现在发生了什么。我以为我在用linreg来分离数据。当我更改名称时,似乎并没有再次引用这些行。它给了我什么?我也试过返回,但它说:SyntaxError:'return'外部函数。 – Shaune 2014-09-03 00:38:07