【问题标题】:AttributeError: 'tuple' object has no attribute 'shape'AttributeError:“元组”对象没有属性“形状”
【发布时间】:2017-05-03 01:29:02
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 也许尝试将您的Data 转换为numpy.array
  • 什么是Data

标签: python python-2.7 numpy attributeerror


【解决方案1】:

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

preprocess(numpy.array(Data))

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2021-09-05
    • 2016-05-09
    • 2015-05-07
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多