numpy中轴处理的实现

作者:wugou2014 时间:2021-08-14 12:06:16 

numpy中轴参数的意义

指定的轴是被压缩的轴

沿轴的时候可以指定两个轴,即面被压缩,以面作为输入

numpy中轴转动

numpy中轴处理的实现

numpy中轴处理的实现

numpy中轴处理的实现

numpy中轴处理的实现

numpy中添加新轴

np.newaxis 在使用和功能上等价于 None,其实就是 None 的一个别名

import numpy as np

a=np.array([1,2,3])

b=a[np.newaxis,:]
c=a[:,np.newaxis]

numpy中轴扩展

numpy.expand_dims(a,axis)

numpy中轴处理的实现

numpy中轴处理的实现

扩展一个维就是把一个维往后挪,或者增加

Numpy中广播数据维

numpy.broadcast_to函数将数组广播到新形状。它在原始数组上返回只 读视图。它通常不连续。如果新形状不符合 NumPy 的广播规则,该函数可能会抛出ValueError。

numpy.broadcast_to(array,shape, subok)

import numpy as np

a = np.arange(4).reshape(1, 4)

print(">>>")
print(a.shape)
print(a)
print()

b = np.broadcast_to(a, (4, 4))
print(">>>")
print(b.shape)
print(b)

>>>
(1, 4)
[[0 1 2 3]]

>>>
(4, 4)
[[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]]

numpy中缩减轴

numpy中轴处理的实现

numpy中轴处理的实现

numpy交换两个轴

numpy.swapaxes(arr,axis1, axis2)

其中:

  • arr:要交换其轴的输入数组

  • axis1:对应第一个轴的整数

  • axis2:对应第二个轴的整数

numpy滚动轴

numpy.rollaxis()函数向后滚动特定的轴,直到一个特定位置。这个函数接受三个参数:

numpy.rollaxis(arr,axis, start)

其中:

  • arr:输入数组

  • axis:要向后滚动的轴,其它轴的相对位置不会改变

  • start:默认为零,表示完整的滚动。会滚动到特定位置。

a = np.arange(24).reshape(2, 3, 4)
print(">>>")
print(a.shape)
print(a)
print()

roll = np.rollaxis(a, 2) # 把第2个轴滚动到0维位置
print(">>>")
print(roll.shape)
print(roll)
print()

roll2 = np.rollaxis(a, 2, 1) # 把第2个轴滚动到1维位置
print(">>>")
print(roll2.shape)
print(roll2)
print()

>>>
(2, 3, 4)
[[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

[[12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]]

>>>
(4, 2, 3)
[[[ 0  4  8]
 [12 16 20]]

[[ 1  5  9]
 [13 17 21]]

[[ 2  6 10]
 [14 18 22]]

[[ 3  7 11]
 [15 19 23]]]

>>>
(2, 4, 3)
[[[ 0  4  8]
 [ 1  5  9]
 [ 2  6 10]
 [ 3  7 11]]

[[12 16 20]
 [13 17 21]
 [14 18 22]
 [15 19 23]]]

numpy中数据拼接操作concatenate

numpy中轴处理的实现

concatenate:沿现有轴加入一系列数组

stack:沿新轴加入一系列数组,即stack(arrays, axis=0)

column_stack:把1维数组当作列堆叠成二维数组

为了向后兼容

dstack:沿深度方向堆叠数组

hstack:沿水平方法堆叠数组

vstack:沿垂直方向堆叠数组

numpy中轴处理的实现

numpy中轴处理的实现

numpy中轴处理的实现

numpy中轴处理的实现

numpy中轴处理的实现

numpy中轴处理的实现

来源:https://blog.csdn.net/wugou2014/article/details/12946460

标签:numpy,轴处理
0
投稿

猜你喜欢

  • Python异步之迭代器如何使用详解

    2023-09-10 17:48:34
  • 数据仓库基本报表制作过程中的SQL写法

    2008-11-25 12:17:00
  • Mac系统重置PostgreSQL密码的方法示例代码

    2024-01-28 13:46:30
  • pytorch中F.avg_pool1d()和F.avg_pool2d()的使用操作

    2023-11-30 01:20:41
  • python检测远程端口是否打开的方法

    2022-01-28 01:45:19
  • mysql5.6 解析JSON字符串方式(支持复杂的嵌套格式)

    2024-01-22 15:03:58
  • 对于Python装饰器使用的一些建议

    2022-05-26 09:05:43
  • thinkphp框架实现删除和批量删除

    2024-06-07 15:29:22
  • asp脚本延时 自定义的delay函数

    2008-04-07 12:59:00
  • 一台电脑(windows系统)安装两个版本MYSQL方法步骤

    2024-01-19 18:22:21
  • PyCharm 2020.2.2 x64 下载并安装的详细教程

    2023-11-29 13:36:32
  • 用python将word文档合并实例代码

    2021-08-13 06:17:01
  • Python直接赋值与浅拷贝和深拷贝实例讲解使用

    2021-06-16 08:21:21
  • Ubuntu中更改MySQL数据库文件目录的方法

    2024-01-15 06:39:18
  • python实现代码审查自动回复消息

    2021-09-15 22:20:46
  • MAC下Anaconda+Pyspark安装配置详细步骤

    2021-02-11 18:15:51
  • Python+OpenCV实现基于颜色的目标识别

    2022-10-28 02:57:27
  • CentOS 6.5下安装Python 3.5.2(与Python2并存)

    2021-06-24 04:33:54
  • Python高级特性——详解多维数组切片(Slice)

    2023-01-20 17:04:13
  • MySQL每天自动增加分区的实现

    2024-01-23 16:18:37
  • asp之家 网络编程 m.aspxhome.com