AttributeError: 'tuple' object has no attribute 'shape'

AttributeError: 'tuple' object has no attribute 'shape'

所以一直在写一个矩阵元素标准化的代码,用到的函数如下:

def preprocess(Data):
    if stdn ==True:
       st=np.empty((Data.shape[0],Data.shape[1]))
       for i in xrange(0,Data.shape[0]):
           st[i,0]=Data[i,0]
       for i in xrange(1,Data.shape[1]):
           st[:,i]=((Data[:,i]-np.min(Data[:,i]))/(np.ptp(Data[:,i])))       
           np.random.shuffle(st)
       return st
    else:
       return Data

它在 class 之外工作得很好,但在它内部使用时它给我这个错误:

  AttributeError: 'tuple' object has no attribute 'shape'

知道如何解决吗?? P.S。这是KNN class验证码

根据您发布的错误,Data 是元组类型,没有为数据定义属性 shape。您可以在调用 preprocess 函数时尝试转换 Data,例如:

preprocess(numpy.array(Data))