AttributeError: ‘list‘ object has no attribute ‘reshape‘ ‘list‘ object has no attribute ‘reshape‘

def image2vector(image):
    """
    Argument:
    image -- a numpy array of shape (length, height, depth)
    
    Returns:
    v -- a vector of shape (length*height*depth, 1)
    """
    

    将三维变为一维输出
    v = np.array(image).reshape(np.array(image).shape[1]*np.array(image).shape[0]*np.array(image).shape[2],1)

    return v
a = [[[1,2,3],
     [3,4,5]],
     [[5,6,7],
      [8,9,10]]
    ]
s = image2vector(a)
print (s)
#注意这里a未改为多维数组,故在image2vector()函数中将image改为三维数组类型
# Python虽然也提供了array模块,但其只支持一维数组,不支持多维数组
#当a被声明为数组类型时,则函数中代码为 v = image.reshape(image.shape[0]*image.shape[1]*image.shape[2],1)

注意a未转格式为数组,会出现错误。函数np.array()将image改为多维数组类型.。Python也提供array模块,但只支持一维数组,不支持多维数组。numpy模块填补这一缺点

或者如下修改


def image2vector(image):
    """
    Argument:
    image -- a numpy array of shape (length, height, depth)
    
    Returns:
    v -- a vector of shape (length*height*depth, 1)
    """
    

    v = image.reshape(image.shape[0]*image.shape[1]*image.shape[2],1)

    
    return v
a = np.array([[[1,2,3],
     [3,4,5]],
     [[5,6,7],
      [8,9,10]]])
s = image2vector(a)
print(s)

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alocus_

如果我的内容帮助到你,打赏我吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值