python row stack_用Python对numpy数组进行变形

本教程和备忘单提供可视化帮助您了解numpy如何重塑多维数组。

179571281_1_20200104044914519

Cheatsheet for Python numpy reshape, stack, and flatten (created by Hause Lin and available here)

numpy reshape()方法如何重整数组? 您是否感到困惑或难以理解其工作原理? 本教程将引导您逐步重塑numpy。 如果您需要上述备忘单的pdf副本,则可以在此处(https://github.com/hauselin/rtutorialsite/blob/master/_posts/2019-12-20-numpy-reshape/reshape.pdf)下载。

创建一个Python numpy数组

使用np.arange()生成一个numpy数组,其中包含从1到12的数字序列。请参见此处的文档。import numpy as npa1 = np.arange(1, 13) # numbers 1 to 12print(a1.shape)> (12,)print(a1)> [ 1 2 3 4 5 6 7 8 9 10 11 12]

179571281_2_20200104044914613

使用reshape()方法重塑

使用reshape()方法将a1数组整形为3 x 4维数组。 让我们使用3_4来表示它的尺寸:3是第0个尺寸(轴),4是第1个尺寸(轴)(请注意,Python索引从0开始)。 请参阅此处的文档。a1_2d = a1.reshape(3, 4) # 3_4print(a1_2d.shape)> (3, 4)print(a1_2d)> [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]

179571281_3_20200104044914675

如果希望numpy自动确定特定尺寸应为多少尺寸/长度,请将该尺寸指定为-1。a1.reshape(3, 4)a1.reshape(-1, 4) # same as above: a1.reshape(3, 4)a1.reshape(3, 4)a1.reshape(3, -1) # s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值