对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 相关文章推荐
简单的编程0基础下Python入门指引
Apr 01 Python
在Linux中通过Python脚本访问mdb数据库的方法
May 06 Python
改进Django中的表单的简单方法
Jul 17 Python
全面了解Python环境配置及项目建立
Jun 30 Python
python 遍历字符串(含汉字)实例详解
Apr 04 Python
Python Requests模拟登录实现图书馆座位自动预约
Apr 27 Python
Django框架使用富文本编辑器Uedit的方法分析
Jul 31 Python
详解Python 正则表达式模块
Nov 05 Python
Python函数装饰器实现方法详解
Dec 22 Python
python RC4加密操作示例【测试可用】
Sep 26 Python
Django 如何使用日期时间选择器规范用户的时间输入示例代码详解
May 22 Python
Pytorch损失函数nn.NLLLoss2d()用法说明
Jul 07 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 快速生成 Flash 动画的方法
2007/03/06 PHP
Ext.data.PagingMemoryProxy分页一次性读取数据的实现代码
2010/04/07 PHP
php字符串截取函数用法分析
2014/11/25 PHP
谈谈你对Zend SAPIs(Zend SAPI Internals)的理解
2015/11/10 PHP
PHP链表操作简单示例
2016/10/15 PHP
[原创]PHP获取数组表示的路径方法分析【数组转字符串】
2017/09/01 PHP
PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
2019/12/20 PHP
Javascript 解疑
2009/11/11 Javascript
创建公共调用 jQuery Ajax 带返回值
2012/08/01 Javascript
jQuery制作仿Mac Lion OS滚动条效果
2015/02/10 Javascript
js实现的简单radio背景颜色选择器代码
2015/08/18 Javascript
javascript实现根据函数名称字符串动态执行函数的方法示例
2016/12/28 Javascript
angular中使用Socket.io实例代码
2017/06/03 Javascript
Layui table 组件的使用之初始化加载数据、数据刷新表格、传参数
2017/09/11 Javascript
NodeJS爬虫实例之糗事百科
2017/12/14 NodeJs
关于ligerui子页面关闭后,父页面刷新,重新加载的方法
2019/09/27 Javascript
JS中数组实现代码(倒序遍历数组,数组连接字符串)
2019/12/29 Javascript
详解搭建一个vue-cli的移动端H5开发模板
2020/01/17 Javascript
Python 时间操作例子和时间格式化参数小结
2014/04/24 Python
Python极简代码实现杨辉三角示例代码
2016/11/15 Python
python 读取excel文件生成sql文件实例详解
2017/05/12 Python
python中列表和元组的区别
2017/12/18 Python
在cmd中运行.py文件: python的操作步骤
2018/05/12 Python
Myprotein芬兰官网:欧洲第一运动营养品牌
2019/05/05 全球购物
GAZMAN官网:澳大利亚领先的男装品牌
2019/12/19 全球购物
信用社员工先进事迹材料
2014/02/04 职场文书
教师节促销方案
2014/03/22 职场文书
《生命 生命》教学反思
2014/04/19 职场文书
学校先进集体事迹材料
2014/05/31 职场文书
2014年学校后勤工作总结
2014/12/06 职场文书
2014年高中教师工作总结
2014/12/19 职场文书
孕妇病假条怎么写
2015/08/17 职场文书
病假条格式范文
2015/08/17 职场文书
三下乡活动心得体会
2016/01/23 职场文书
解决Pytorch dataloader时报错每个tensor维度不一样的问题
2021/05/28 Python
Spark SQL 2.4.8 操作 Dataframe的两种方式
2021/10/16 SQL Server