首页 > 编程语言 >【python】numpy数组升维函数expand_dims()

【python】numpy数组升维函数expand_dims()

时间:2022-08-24 22:57:51浏览次数:69  
标签:tests 升维 python dims shape 一维 print expand

expand_dims(a, axis),其中a为输入的数组,axis为整型指定要增加的维数位置
可以结合shape()来看,shape()返回的是一个tuple,把其看成一个数组并指定下标。如果shape为(1, 2),则axis=0就是在shape下标0的地方插入一维,则shape变为(1, 1, 2);如果axis=2就是在shape下标2的地方插入一维,则shape变为(1, 2, 1)。
下面结合代码来看:

# 合并数组为二维
test1 = np.array([5, 10, 16, 26])
test2 = np.array([2.1, 5.4, 10.7, 11])
# 将二者均升维为二维数组
test1 = np.expand_dims(test1, 0)    # expand_dims()方法
test2 = test2[np.newaxis, :]        # newaxis方法,这里和expand_dims(, 0)一样
print(test1)
print(test2)
all_tests = np.concatenate([test1, test2])
print(all_tests)

这里的输出是:

[[ 5 10 16 26]]
[[ 2.1  5.4 10.7 11. ]]
[[ 5.  10.  16.  26. ]
 [ 2.1  5.4 10.7 11. ]]

下面对一维数组一步步用expand_dims()来升维:

# expand_dims()说明
test = np.array([5, 10, 16, 26])    # 一维
print(test.shape)                   # (4, )  一维且一维的长度是4
test = np.expand_dims(test, 0)      # (1, 4) 二维且一维长度是1,二维长度是4
print(test.shape)
print(test)
test = np.expand_dims(test, 1)      # (1, 1, 4) 三维且一维长度是4,二维长度是1,三维长度是1
print(test.shape)
print(test)

这里的输出为:

(4,)
(1, 4)
[[ 5 10 16 26]]
(1, 1, 4)
[[[ 5 10 16 26]]]

下面对二维数组一步步用expand_dims()来升维,下面的all_test是第一段代码里合并的结果:

print(all_tests.shape)                      # (2, 4) 换成all_tests来测试expand_dims()
all_tests = np.expand_dims(all_tests, 0)    # (1, 2, 4)  因为在shape下标为0的地方插入一维,
print(all_tests.shape)                      # 则一维长度为1,二维长度为4,三维长度为2,四维长度为1
print(all_tests)
all_tests = np.expand_dims(all_tests, 1)    # (1, 1, 2, 4)  在shape下标为1的地方插入一维
print(all_tests.shape)                      # 则一维长度为1,二维长度为1,三维长度为2,四维长度为4
print(all_tests)

这里的输出是:

(2, 4)
(1, 2, 4)
[[[ 5.  10.  16.  26. ]
  [ 2.1  5.4 10.7 11. ]]]
(1, 1, 2, 4)
[[[[ 5.  10.  16.  26. ]
   [ 2.1  5.4 10.7 11. ]]]]

最后说明一下对数组的n维度的认识:除数组最外面一层中括号,往里一层是一维,依次往里是二、三……n维。看对应维数长度就是看对应层中括号的对数,直到最里层就是看数组的长度。

如:[1, 2, 3, 4]是(4, ),即一维长度是4;
[[1, 2, 3, 4], [5, 6, 7, 8]]是(2, 4)
[[[1, 2, 3, 4], [5, 6, 7, 8]]]是(1, 2, 4)

标签:tests,升维,python,dims,shape,一维,print,expand
From: https://www.cnblogs.com/easternE/p/16622557.html

相关文章

  • python 数据库建表操作
    fromdjango.dbimportmodels#Createyourmodelshere.classUser(models.Model):name=models.CharField(max_length=32)age=models.IntegerField()reg......
  • 《Python源码剖析》PDF高清版试读
      《Python源码剖析》PDF高清版免费下载地址  内容简介  · · · · · ·作为主流的动态语言,Python不仅简单易学、移植性好,而且拥有强大丰富的库的......
  • Python小游戏——外星人入侵(保姆级教程)第一章 09重构check_events()
    系列文章目录第一章:武装飞船09重构check_events()一、重构1.重构原因随着游戏的开发,方法_check_events()将越来越长。因此将其部分代码放在两个方法中,其中一个处理KEY......
  • python基础——字典 集合
    字典集合字典列表的延伸,列表只能通过下标找,不好找,指点可以通过名字查找信息,就像查字典一样。创建dict1={}dict2={'key1':'value1','key2':'value2'}dict3=dic......
  • Pybind11实现python调取C++
    1、一些处理矩阵运算,图像处理算法,直接采用python实现可能速度稍微慢,效率不高,或者为了直接在python中调用其他C++第三方库。图像,矩阵在python中通常表示为numpy.ndarray,......
  • python基础——列表 元组
    列表和元组列表的作用是一次性存储多个数据,可以对这些数据进行的操作有:增,删,改,查列表格式[数据1,数据2,数据3,....]创建方式#创建空列表li1=[]#创建空列表li2......
  • Python字典核心底层原理
    字典核心底层原理......
  • Python自学教程5-字符串有哪些常用操作
    任何编程语言,不管是Python、Java还是Golang,字符串都是最重要的一种数据类型。但是字符串的操作又很多,初学者经常毫无头绪,不知道从哪儿学起,也不知道哪些操作用得多,今天......
  • Python自学教程4-数据类型学什么
    Hi,我是九柄,全网同号,今天我们说说Python的数据类型。python数据类型有什么特点每一门编程语言都要学数据类型的,每种类型的操作会稍微有一点区别。Python是一门非常灵活的......
  • 【python】根据进程名获取pid, 并杀死进程
    1、pypihttps://pypi.org/project/psutil/ 2、githubhttps://github.com/giampaolo/psutil 3、dochttps://psutil.readthedocs.io/en/latest/ 4、获取chromedr......