np.reshape(-1)、np.reshape(-1, 1)、np.reshape(1, -1)详解

np.reshape()规范:新的排布(shape)应与原始排布兼容

Numpy允许给新排布的参数设置为 -1(如(2, -1), (-1, 3),但是不允许(-1, -1)),它指的是未知的维数,而Numpy自己会根据输入值的排布,用reshape()方法得出未知维数的值,使得输出值符合np.reshape()的规范。

Code 1: np.reshape(-1)

original = np.array([2, 4, 1, 3],
			[1, 2, 5, 2])

orginal.shape # (2, 4)

new_1 = orginal.reshape(-1)
print(new_1) # 新排布为(1,8)
array([2, 4, 1, 3, 1, 2, 5, 2])

Code 2: np.reshape(-1, 1)

# 设定新排布的列数为1,行数为未知
new_2 = orginal.reshape(-1, 1)
print(new_2)  # 新排布为(8,1)
array([[ 2],
[ 4],
[ 1],
[ 3],
[ 1],
[ 2], 
[ 5],
[ 2]])

Code 3:np.reshape(1, -1)

# 设定新排布的行数为1,列数为未知
new_3 = orginal.reshape(1, -1)
print(new_3)  # 新排布为(1,8)
array([2, 4, 1, 3, 1, 2, 5, 2])

Code 4: np.reshape(-1, 2)

# 设定新排布的列数为2,行数为未知
new_4 = original.reshape(-1, 2)
print(new_4) # 新排布为(4,2)
array([[2, 4],
[1, 3],
[1, 2],
[5, 2]
])

参考网址:
https://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape

  • 7
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
np.reshape(-1, 2)是一个numpy的函数调用,用于改变数组的形状。其中的参数-1表示根据数组的大小自动计算另一个维度的大小,而2表示目标数组每个子数组的大小为2。这个函数的作用是将原始数组按照每两个元素为一组重新排列,并生成一个新的数组。具体来说,它会将原始数组的元素按照行优先的顺序重新排列,并且每行有两个元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [numpy reshape](https://blog.csdn.net/ynkgyangxw/article/details/78464757)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [numpy.reshape( )用法总结](https://blog.csdn.net/qq_29373285/article/details/82753306)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [详解numpy.ndarray.reshape()函数的参数问题](https://download.csdn.net/download/weixin_38562725/14840309)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值