1

这似乎在 Stack Overflow 上被打死了,但似乎没有一个问题与我的问题相匹配。无论如何,直接进入代码。

这是 Edge.py

from __future__ import division
import sys
from numpy import *


class EdgeList:
    def __init__(self, mat):
        self.id1 = mat[:,0]
        self.id2 = mat[:,1]
        self.value = mat[:,2]
    def is_above(self):
        return self.value>average(self.value)
    def stats(self):
        pass #omitted; too long and irrelevant here.

这是 AHsparse.py

from __future__ import division

import sys
from numpy import *
from Edge import EdgeList

class AHvector:
    def __init__(self, mat):
        self.el = EdgeList(mat)
    def multiply(self, other):
        v=zeros(max(len(self.el.val), len(other.el.val)))
        for index in self.id1:
            v[index] = self.el.val[index] * other.el.val[index]
        return v

这是一些测试代码(其他测试通过)

import sys
from numpy import *
from Edge import EdgeList
from AHsparse import AHvector

testmat =loadtxt('test.data', delimiter=';')
st = EdgeList(testmat)
stv = AHvector(st)
stv = stv.multiply(stv)
print(stv)

错误发生在 AHvector 类的init方法中,但回调到 Edge.py:

Traceback (most recent call last):
  File "/Users/syntaxfree/Dropbox/nina/nina lives in objects/sparse_test.py", line 8, in <module>
    stv = AHvector(st)
  File "/Users/syntaxfree/Dropbox/nina/nina lives in objects/AHsparse.py", line 9, in __init__
    self.el = EdgeList(mat)
  File "/Users/syntaxfree/Dropbox/nina/nina lives in objects/Edge.py", line 7, in __init__
    self.id1 = mat[:,0]
AttributeError: EdgeList instance has no attribute '__getitem__'
[Finished in 0.6s with exit code 1]

恐怕我没有什么要补充的了——除了我能够自己初始化 EdgeList 并在其他测试代码中运行 stats 方法,我完全困惑为什么这不起作用。

4

1 回答 1

3

当你跑

 stv = AHvector(st)

st是一个边缘列表。然后 AHvectorinit尝试制作st. 也许 AHvector 应该说明

 self.el = mat # Instead of EdgeList(mat)?

或者也许 AHvector 不应该接收st,而是testmat

于 2012-09-17T13:46:44.473 回答