简介

numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数。有时候我们可能需要知道某一维的特定维数。

二维情况

>>> import numpy as np
>>> y = np.array([[1,2,3],[4,5,6]])
>>> print(y)
[[1 2 3]
[4 5 6]]
>>> print(y.shape)
(2, 3)
>>> print(y.shape[0])
2
>>> print(y.shape[1])
3

可以看到y是一个两行三列的二维数组,y.shape[0]代表行数,y.shape[1]代表列数。

三维情况

>>> x  = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[0,1,2]],[[3,4,5],[6,7,8]]])
>>>> print(x)
[[[1 2 3]
[4 5 6]] [[7 8 9]
[0 1 2]] [[3 4 5]
[6 7 8]]]
>>> print(x.shape)
(3, 2, 3)
>>> print(x.shape[0])
3
>>> print(x.shape[1])
2
>>> print(x.shape[2])
3

可以看到x是一个包含了3个两行三列的二维数组的三维数组,x.shape[0]代表包含二维数组的个数,x.shape[1]表示二维数组的行数,x.shape[2]表示二维数组的列数。

总结

可以看到,shape[0]表示最外围的数组的维数,shape[1]表示次外围的数组的维数,数字不断增大,维数由外到内。

len():返回对象的长度,注意不是length()函数
len([1,2,3]),返回值为3
len([[1,2,3],[3,4,5]]),返回值为2 count():计算包含对象个数
[1,1,1,2].count(1),返回值为3
‘asddf’.count(‘d’),返回值为2 size()和shape () 是numpy模块中才有的函数 size():计算数组和矩阵所有数据的个数
a = np.array([[1,2,3],[4,5,6]])
np.size(a),返回值为 6
np.size(a,1),返回值为 3 shape ():得到矩阵每维的大小
np. shape (a),返回值为 (2,3) 另外要注意的是,shape和size既可以作为函数,也可以作为ndarray的属性
a.size,返回值为 6,
a.shape,返回值为 (2,3)

  

https://blog.csdn.net/songyunli1111/article/details/78079904

https://blog.csdn.net/liuweiyuxiang/article/details/79384435

python 中 numpy array 中的维度的更多相关文章

  1. Python 将numpy array由浮点型转换为整型

    Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:

  2. ECMAScript 5中对Array中新增了9个方法

    ECMAScript 5中对Array中新增了9个方法: 5个迭代方法(循环操作数组中的各个项):forEach(),map(),filter(),every()和some() 2个归并方法(迭代数组 ...

  3. 【python】numpy array特殊数据统一处理

    array中的某些数据坏掉,想要统一处理,找到了这个方法,做个笔记. 比如,把数组中所有小于0的数字置为0 import numpy as np t = np.array([-2, -1, 0, 1, ...

  4. Python的numpy库中rand(),randn(),randint(),random_integers()的使用

    1.numpy.random.rand() 用法是:numpy.random.rand(d0,d1,…dn) 以给定的形状创建一个数组,并在数组中加入在[0,1]之间均匀分布的随机样本. 用法及实现: ...

  5. numpy.array 中的运算

    简单运算 现在有有个需求,给定一个数组,让数组中每一个数乘以2,怎么做呢 n = 10 L = [i for i in range(n)] L # [0, 1, 2, 3, 4, 5, 6, 7, 8 ...

  6. python的numpy.array

    为什么要用numpy Python中提供了list容器,可以当作数组使用.但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3].就需要三个指针和三 ...

  7. [Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate

    数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.arr ...

  8. [Python Cookbook] Numpy Array Slicing and Indexing

    1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...

  9. [Python Cookbook] Numpy Array Manipulation

    1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...

随机推荐

  1. Restrict form resize -- Delphi

    http://www.delphipages.com/forum/showthread.php?t=58391 Hi, How would I restrict a form from being r ...

  2. dwz关闭当前dialog

    首先,前台代码如下: <form method="post" class="pageForm required-validate" onsubmit=&q ...

  3. docker学习总结--安装、卸载

    参考:http://blog.csdn.net/u012562943/article/details/50463400 https://docs.docker.com/engine/getstarte ...

  4. ASP.NET 5 的Roadmap(转)

    这次随 Visual Studio 2015 发布的 ASP.NET 版本是 ASP.NET 4.6 与 ASP.NET 5 beta5.在 VS2015 发布的同时,微软也发布了 ASP.NET 5 ...

  5. 织梦(Dedecms)select_soft_post.php页面变量未初始漏洞

    漏洞版本: Dedecms 5.5 漏洞描述: 漏洞产生文件位于include\dialog\select_soft_post.php,其变量$cfg_basedir没有正确初始化,导致可以饶过身份认 ...

  6. json 数组操作

    用js有很久了,但都没有深究过js的数组形式.这段时间做的一个项目,用到数组的地方很多,自以为js还可以的自己居然无从下手,一下狠心,我学!呵呵. 1.数组的创建 var arrayObj = new ...

  7. 关于docker的15个小tip

    1. 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubuntu): $ ID=$(docker run ubuntu echo hello world) he ...

  8. oracle 11g physical standby switchover

    简介 SWITCHOVERS主要是在计划停机维护时用来降低DOWNTIME,如硬件维护.操作系统升级或是数据库rolling upgrade, 也可用来进行特殊情况下的数据库迁移. SWITCHOVE ...

  9. WinForm 窗口缩放动画效果

    using System; using System.Collections.Generic; using System.Text; using System.Threading; using Sys ...

  10. WCF 之 生成元数据和代理

    在WCF开发概述中讲解了手工方式的WCF应用,其实实际开发中使用更多的使用配置方式和元数据来实现WCF,下面我们来看一个具体的Demo,这个例子和WCF开发概述中使用的是同一个例子,只是实现方式不同, ...