+ -
当前位置:首页 → 问答吧 → 高手请进,关于《集体编程》书中的一个不能正常运行的代码?

高手请进,关于《集体编程》书中的一个不能正常运行的代码?

时间:2011-04-17

来源:互联网

集体编程第九章:高阶分类:核方法与SVM方法,但是在其中一段代码上,原版照抄或者原版复制,都不能在python2.6上正常运行,求解?具体代码与错误提示如下:
  1. def scaledata1(rows):
  2.     low=[999999999.0]*len(rows[0].data)
  3.     high=[-999999999.0]*len(rows[0].data)
  4.     # Find the lowest and highest values
  5.     for row in rows:
  6.         d=row.data
  7.         for i in range(len(d)):
  8.             if d[i]<low[i]: low[i]=d[i]
  9.             if d[i]>high[i]: high[i]=d[i]
  10.     # Create a function that scales data
  11.     def scaleinput(d):
  12.         return [(d.data[i]-low[i])/(high[i]-low[i])for i in range(len(low))]
  13.     # Scale all the data
  14.     newrows=[matchrow(scaleinput(row.data)+[row.match])for row in rows]
  15.     # Return the new data and the function
  16.     return newrows,scaleinput
复制代码
错误提示如下:
  1. Traceback (most recent call last):
  2.   File "<pyshell#67>", line 1, in <module>
  3.     scaleset,scalef=advancedclassify.scaledata1(numericalset)
  4.   File "D:\python26\svm\advancedclassify.py", line 101, in scaledata1
  5.     newrows=[matchrow(scaleinput(row.data)+[row.match])for row in rows]
  6.   File "D:\python26\svm\advancedclassify.py", line 99, in scaleinput
  7.     return [(d.data[i]-low[i])/(high[i]-low[i])for i in range(len(low))]
  8. AttributeError: 'list' object has no attribute 'data'
复制代码
另外一些线索是:
1.rows是一个列表,单独使用即rows[0].data是没问题的;
2.感觉问题在于row in rows这里不能正常的获取列表中的其中内容即rows[0]~row[n]

请高手指点指点?

作者: qq35601224   发布时间: 2011-04-17

如果单独使用rows的话,也是没问题的,如下:
  1. for row in rows:
  2.         print row.data+[row.match]
复制代码
输出结果:
[39.0, 1, -1, 43.0, -1, 1, 0, 0, 0]
[23.0, -1, -1, 30.0, -1, -1, 0, 0, 1]
[50.0, -1, -1, 49.0, 1, 1, 0, 0, 0]
[46.0, -1, 1, 19.0, -1, -1, 0, 0, 0]
[36.0, 1, 1, 29.0, -1, 1, 0, 0, 1]

作者: qq35601224   发布时间: 2011-04-17