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
AttributeError: 'tuple' object has no attribute 'shape'
提问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, Data
is of type tuple and there is no attribute shape
defined for data. You could try casting Data
when you call your preprocess
function, e.g.:
根据您发布的错误,Data
是元组类型,并且没有shape
为数据定义属性。您可以Data
在调用preprocess
函数时尝试强制转换,例如:
preprocess(numpy.array(Data))