对numpy中轴与维度的理解


Posted in Python onApril 18, 2018

NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes. The number of axes is rank.

For example, the coordinates of a point in 3D space [1, 2, 1] is an array of rank 1, because it has one axis. That axis has a length of 3. In the example pictured below, the array has rank 2 (it is 2-dimensional). The first dimension (axis) has a length of 2, the second dimension has a length of 3.

[[ 1., 0., 0.],
 [ 0., 1., 2.]]

ndarray.ndim

数组轴的个数,在python的世界中,轴的个数被称作秩

>> X = np.reshape(np.arange(24), (2, 3, 4))
  # 也即 2 行 3 列的 4 个平面(plane)
>> X
array([[[ 0, 1, 2, 3],
    [ 4, 5, 6, 7],
    [ 8, 9, 10, 11]],
    [[12, 13, 14, 15],
    [16, 17, 18, 19],
    [20, 21, 22, 23]]])

shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度,比如shape[0]就是读取矩阵第一维度的长度。

shape(x)

(2,3,4)

shape(x)[0]

2

或者

x.shape[0]

2

再来分别看每一个平面的构成:

>> X[:, :, 0]
array([[ 0, 4, 8],
    [12, 16, 20]])
>> X[:, :, 1]
array([[ 1, 5, 9],
    [13, 17, 21]])
>> X[:, :, 2]
array([[ 2, 6, 10],
    [14, 18, 22]])
>> X[:, :, 3]
array([[ 3, 7, 11],
    [15, 19, 23]])

也即在对 np.arange(24)(0, 1, 2, 3, ..., 23) 进行重新的排列时,在多维数组的多个轴的方向上,先分配最后一个轴(对于二维数组,即先分配行的方向,对于三维数组即先分配平面的方向)

reshpae,是数组对象中的方法,用于改变数组的形状。

二维数组

#!/usr/bin/env python 
# coding=utf-8 
import numpy as np 
 
a=np.array([1, 2, 3, 4, 5, 6, 7, 8]) 
print a 
d=a.reshape((2,4)) 
print d

对numpy中轴与维度的理解

三维数组

#!/usr/bin/env python 
# coding=utf-8 
import numpy as np 
 
a=np.array([1, 2, 3, 4, 5, 6, 7, 8]) 
print a 
f=a.reshape((2, 2, 2)) 
print f

对numpy中轴与维度的理解

形状变化的原则是数组元素不能发生改变,比如这样写就是错误的,因为数组元素发生了变化。

#!/usr/bin/env python 
# coding=utf-8 
import numpy as np 
 
a=np.array([1, 2, 3, 4, 5, 6, 7, 8]) 
print a 
print a.dtype 
e=a.reshape((2,2)) 
print e

对numpy中轴与维度的理解

注意:通过reshape生成的新数组和原始数组公用一个内存,也就是说,假如更改一个数组的元素,另一个数组也将发生改变。

#!/usr/bin/env python 
# coding=utf-8 
import numpy as np 
 
a=np.array([1, 2, 3, 4, 5, 6, 7, 8]) 
print a 
e=a.reshape((2, 4)) 
print e 
a[1]=100 
print a 
print e

对numpy中轴与维度的理解

Python中reshape函数参数-1的意思

a=np.arange(0, 60, 10)
>>>a
array([0,10,20,30,40,50])
>>>a.reshape(-1,1)
array([[0],
[10],
[20],
[30],
[40],
[50]])

如果写成a.reshape(1,1)就会报错

ValueError:cannot reshape array of size 6 into shape (1,1)

>>> a = np.array([[1,2,3], [4,5,6]])
>>> np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2
array([[1, 2],
    [3, 4],
    [5, 6]])

-1表示我懒得计算该填什么数字,由python通过a和其他的值3推测出来。

# 下面是两张2*3大小的照片(不知道有几张照片用-1代替),如何把所有二维照片给摊平成一维
>>> image = np.array([[[1,2,3], [4,5,6]], [[1,1,1], [1,1,1]]])
>>> image.shape
(2, 2, 3)
>>> image.reshape((-1, 6))
array([[1, 2, 3, 4, 5, 6],
    [1, 1, 1, 1, 1, 1]])

以上这篇对numpy中轴与维度的理解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
在Python的setuptools框架下生成egg的教程
Apr 13 Python
Python编程中的文件操作攻略
Oct 16 Python
浅谈Python生成器generator之next和send的运行流程(详解)
May 08 Python
Python可变参数*args和**kwargs用法实例小结
Apr 27 Python
Python不同目录间进行模块调用的实现方法
Jan 29 Python
django+echart数据动态显示的例子
Aug 12 Python
python3使用GUI统计代码量
Sep 18 Python
详解numpy矩阵的创建与数据类型
Oct 18 Python
python Event事件、进程池与线程池、协程解析
Oct 25 Python
PyTorch实现更新部分网络,其他不更新
Dec 31 Python
使用TensorFlow直接获取处理MNIST数据方式
Feb 10 Python
Python中Yield的基本用法
Oct 18 Python
Python实现购物车购物小程序
Apr 18 #Python
详谈python中冒号与逗号的区别
Apr 18 #Python
python logging日志模块以及多进程日志详解
Apr 18 #Python
详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数
Apr 18 #Python
Python中数组,列表:冒号的灵活用法介绍(np数组,列表倒序)
Apr 18 #Python
浅谈numpy数组中冒号和负号的含义
Apr 18 #Python
对python numpy数组中冒号的使用方法详解
Apr 17 #Python
You might like
php常用Stream函数集介绍
2013/06/24 PHP
PHP.ini安全配置检测工具pcc简单介绍
2015/07/02 PHP
javascript图像处理—仿射变换深度理解
2013/01/16 Javascript
js获取某月的最后一天日期的简单实例
2013/06/22 Javascript
用innerhtml提高页面打开速度的方法
2013/08/02 Javascript
用js代码改变单选框选中状态的简单实例
2013/12/18 Javascript
jquery操作下拉列表、文本框、复选框、单选框集合(收藏)
2014/01/08 Javascript
JS判断两个时间大小的示例代码
2014/01/28 Javascript
jQuery不兼容input的change事件问题解决过程
2014/12/05 Javascript
JavaScript中操作字符串之localeCompare()方法的使用
2015/06/06 Javascript
js实现页面a向页面b传参的方法
2016/05/29 Javascript
全面解析JavaScript中“&&”和“||”操作符(总结篇)
2016/07/18 Javascript
Bootstrap优化站点资源、响应式图片、传送带使用详解3
2016/10/14 Javascript
JavaScript实现翻页功能(附效果图)
2017/02/16 Javascript
利用node.js+mongodb如何搭建一个简单登录注册的功能详解
2017/07/30 Javascript
node 命令方式启动修改端口的方法
2018/05/12 Javascript
Vue3.x源码调试的实现方法
2019/10/13 Javascript
vue-video-player视频播放器使用配置详解
2020/10/23 Javascript
举例讲解Python中装饰器的用法
2015/04/27 Python
Python实现二叉堆
2016/02/03 Python
Python3.2模拟实现webqq登录
2016/02/15 Python
Python实现变量数值交换及判断数组是否含有某个元素的方法
2017/09/18 Python
python 统计数组中元素出现次数并进行排序的实例
2018/07/02 Python
Python爬虫实现验证码登录代码实例
2019/05/10 Python
django表单的Widgets使用详解
2019/07/22 Python
浅谈Python类中的self到底是干啥的
2019/11/11 Python
Python爬虫headers处理及网络超时问题解决方案
2020/06/19 Python
Yahoo-PHP面试题4
2012/05/05 面试题
企业申诉管理制度
2014/01/30 职场文书
员工试用期自我鉴定范文
2014/09/15 职场文书
文明单位汇报材料
2014/12/24 职场文书
音乐教师求职信范文
2015/03/20 职场文书
电力培训学习心得体会
2016/01/11 职场文书
史上最全书信经典范文大全(建议收藏)
2019/07/10 职场文书
Python数据分析之pandas函数详解
2021/04/21 Python
Python数据可视化之绘制柱状图和条形图
2021/05/25 Python