简介

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. /etc/fstab 解析

    http://www.codesec.net/view/39930.html root@qs-wg-db1 /]# cat /etc/fstab LABEL=/ / ext3 defaults 1 1 ...

  2. windows系统上安装与使用Android NDK r8d(二)

    四.    在eclipse中集成c/c++开发环境    1. 装Eclipse的C/C++环境插件:CDT,这里选择在线安装.          首先登录http://www.eclipse.or ...

  3. cocos3.0使用cocostudio动画帧结合地图对象键值创建精灵动画

    内容例如以下: #include "cocos2d.h" #include "cocostudio/CocoStudio.h" //精灵猫和其它精灵的tag t ...

  4. mysql_fetch_assoc 跟mysql_fetch_array 有什么区别?

    mysql_fetch_assoc 得到的是关联数组. Array ( [0] => Array ( [title] => 特价9.9包邮 EFOLAR/依芙拉 BB粉润腮红粉 饼 蘑菇 ...

  5. 一张图彻底了解Unity脚本的生命周期

    以前没好好看Unity的脚本手册,原来写的是这么的全...尤其起yield  www  协同 这部分看了这张图彻底的懂了..

  6. iOS开发 贝塞尔曲线UIBezierPath(2)

    使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...

  7. Multiline ComboBox

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. mac 查看cpu个数

    mac 查看cpu个数   同是unix系统的mac,如何查看cpu个数及其相关信息呢?

  9. (转载)uCOS-II的嵌入式串口通信模块设计

    在嵌入式应用中,使用RTOS的主要原因是为了提高系统的可靠性,其次是提高开发效率.缩短开发周期.uCOS-II是一个占先式实时多任务内核,使用对象是嵌入式系统,对源代码适当裁减,很容易移植到8~32位 ...

  10. css3动画属性系列之transform细讲旋转rotate

    1.语法: transform: none |  <transform-function> [<transform-function>]* 2.取值: none         ...