Python AttributeError: 'tuple' 对象没有属性 'shape'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41198144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:33:08  来源:igfitidea点击:

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

pythonpython-2.7numpyattributeerror

提问by Ferial Mohammed

So I have been writing a code to standardize the elements of a matrix and the function I used is as follows:

所以我一直在写代码来对矩阵的元素进行标准化,我使用的函数如下:

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

It works very well outside the class but when used inside of it it gives me this error:

它在课堂外工作得很好,但在课堂内使用时,它给了我这个错误:

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

Any idea on how I can fix it?? P.S. This is a KNN classification code

关于如何修复它的任何想法?PS这是一个KNN分类代码

采纳答案by José Sánchez

According to the error you posted, Datais of type tuple and there is no attribute shapedefined for data. You could try casting Datawhen you call your preprocessfunction, e.g.:

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

preprocess(numpy.array(Data))