本网站可以出售:只需60000元直接拥有。QQ:939804642
您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:详解Numpy扩充矩阵维度(np.expand_dims, np.newaxis)和删除维度(np.squeeze)的方法

51自学网 2021-10-30 22:48:39
  python
这篇教程详解Numpy扩充矩阵维度(np.expand_dims, np.newaxis)和删除维度(np.squeeze)的方法写得很实用,希望能帮到您。

在操作矩阵的时候,不同的接口对于矩阵的输入维度要求不同,输入可能为1-D,2-D,3-D等等。下面介绍一下使用Numpy进行矩阵维度变更的相关方法。主要包括以下几种:

1、np.newaxis扩充矩阵维度

2、np.expand_dims扩充矩阵维度

3、np.squeeze删除矩阵中维度大小为1的维度

np.newaxis,np.expand_dims扩充矩阵维度:

import numpy as np x = np.arange(8).reshape(2, 4)print(x.shape) # 添加第0维,输出shape -> (1, 2, 4)x1 = x[np.newaxis, :]print(x1.shape) # 添加第1维, 输出shape -> (2, 1, 4)x2 = np.expand_dims(x, axis=1)print(x2.shape)

输出结果:

(2, 4)
(1, 2, 4)
(2, 1, 4)

np.squeeze降低矩阵维度:

""" squeeze 函数:从数组的形状中删除单维度条目,即把shape中为1的维度去掉 用法:numpy.squeeze(a,axis = None)  1)a表示输入的数组;  2)axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;  3)axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空,则删除所有单维度的条目;  4)返回值:数组  5) 不会修改原数组;"""import numpy as np print("#" * 40, "原始数据", "#" * 40)x = np.arange(10).reshape(1, 1, 10, 1)print(x.shape)print(x) print("#" * 40, "去掉axis=0这个维度", "#" * 40)x_squeeze_0 = np.squeeze(x, axis=0)print(x_squeeze_0.shape, x_squeeze_0) print("#" * 40, "去掉axis=3这个维度", "#" * 40)x_squeeze_3 = np.squeeze(x, axis=3)print(x_squeeze_3.shape, x_squeeze_3) print("#" * 40, "去掉axis=0, axis=1这两个维度", "#" * 40)x_squeeze_0_1 = np.squeeze(x, axis=(0, 1))print(x_squeeze_0_1.shape, x_squeeze_0_1) print("#" * 40, "去掉所有1维的维度", "#" * 40)x_squeeze = np.squeeze(x)print(x_squeeze.shape, x_squeeze) print("#" * 40, "去掉不是1维的维度,抛异常", "#" * 40)try: x_squeeze = np.squeeze(x, axis=2) print(x_squeeze.shape, x_squeeze)except Exception as e: print(e)

输出结果:

######################################## 原始数据 ########################################
(1, 1, 10, 1)
[[[[0]
   [1]
   [2]
   [3]
   [4]
   [5]
   [6]
   [7]
   [8]
   [9]]]]
######################################## 去掉axis=0这个维度 ########################################
(1, 10, 1) [[[0]
  [1]
  [2]
  [3]
  [4]
  [5]
  [6]
  [7]
  [8]
  [9]]]
######################################## 去掉axis=3这个维度 ########################################
(1, 1, 10) [[[0 1 2 3 4 5 6 7 8 9]]]
######################################## 去掉axis=0, axis=1这两个维度 ########################################
(10, 1) [[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]
 [8]
 [9]]
######################################## 去掉所有1维的维度 ########################################
(10,) [0 1 2 3 4 5 6 7 8 9]
######################################## 去掉不是1维的维度,抛异常 ########################################
cannot select an axis to squeeze out which has size not equal to one

参考链接

到此这篇关于详解Numpy扩充矩阵维度(np.expand_dims, np.newaxis)和删除维度(np.squeeze)的方法的文章就介绍到这了,更多相关Numpy扩充矩阵维度和删除维度内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net!


Numpy中的shape函数的用法详解
numpy的squeeze函数使用方法
51自学网自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1