简介

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. MB506/MB510 ULTRA HIGH FREQUENCY PRESCALER

    MB506 ULTRA HIGH FREQUENCY PRESCALER The Fujitsu MB506 is a high frequency, up to 2.4GHz, prescaler ...

  2. FreeRTOS Memory Management ( IAR )

    http://www.freertos.org/a00111.html The RTOS kernel allocates RAM each time a task, queue, mutex, so ...

  3. (原)将Oracle迁移到SQLServer

    背景:中了一个标,Oracle改成SQLServer解决办法: 1.首先想到微软的解决方案:Microsoft SQL Server Migration Assistant v7.4 for Orac ...

  4. (转)找回vss超级管理员密码

    原文:http://www.cnblogs.com/446557021/archive/2011/01/05/1926213.html 如果忘记了VSS管理员密码,打开vss数据库所在的文件夹,打开d ...

  5. [Android 源码] Android源码下载

    Android源码下载 为了能够顺利的下载Android的源码,同时也为了避免在网络上再次搜寻如何下载源码的麻烦,我把下载过程记录在这篇文档中. 官网中也有详细的介绍: http://source.a ...

  6. Single Number and Single Number II

    [] Given an array of integers, every element appears twice except for one. Find that single one. [] ...

  7. Java中Action层、Service层、Modle层和Dao层的功能区分

    一.Java中Action层.Service层.Modle层和Dao层的功能区分: 首先,这是现在最基本的分层方式,结合了SSH架构. modle层就是对应的数据库表的实体类.(即domain) Da ...

  8. myBatis + SpringMVC上传、下载文件

    摘自: http://limingnihao.iteye.com/blog/1069503 环境:maven+SpringMVC + Spring + MyBatis + MySql 本文主要说明如何 ...

  9. TortoiseSVN 源代码下载

    SVN的客户端软件TortoiseSVN http://tortoisesvn.tigris.org/ 这是我采用的 TortoiseSVN 的官方网站,页面上的两只小乌龟真的很好看. 目前最新的版本 ...

  10. 分享六个基于Bootstrap的实用开发教程和模板演示

    关于Bootstrap,相信大家一定不陌生,它已经成为现在主流产业的一个重要工具,Bootstrap提供了优雅的HTML和CSS规范,它基于jQuery框架开发的,它在jQuery框架的基础上进行了更 ...