对numpy中轴与维度的理解
作者:liuhmmjj 发布时间:2022-03-20 11:58:33
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
三维数组
#!/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
形状变化的原则是数组元素不能发生改变,比如这样写就是错误的,因为数组元素发生了变化。
#!/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
注意:通过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
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]])
来源:https://blog.csdn.net/u014082714/article/details/75946302
猜你喜欢
- 认知心理学家Donald A. Norman将人类的行动分解成七个阶段:1. 确定目标2. 确定意图3. 明确行动内容4. 执行5. 感知外
- python开发者向普通windows用户分享程序,要给程序加图形化的界面(传送门:这可能是最好玩的python GUI入门实例! http
- 什么是瀑布图?瀑布图用表达两个数值之间的变化过程,过程值为正的时候,向上加,过程值为负的时候向下减[1]。今天分享在Python中绘制瀑布图
- 上个星期,大佬分享了一个验证身份证号合法性的库:id_validator,没空去试着用一下看看,今天有点时间,来试着用下这个库。1、首先,要
- 修改 index.js 内容,写一些 ES6 的语法:const arr = [ new Promise(() => {}
- 前言:在爬取并下载网页上的视频的时候,我们需要实时进度条,这可以帮助我们更直观的看到视频的下载进度。一、全部代码展示from context
- 一、系统简介实现一个学生信息的管理系统:主要功能有:添加学生信息删除学生信息修改学生信息查询学生信息显示学生信息退出当前系统二、步骤分析显示
- 修改python plot折线图的坐标轴刻度,这里修改为整数:代码如下:from matplotlib import pyplot as p
- 在添加新记录时,有时候我们需要将部分动态内容自动添加到指定的文本域,例如上传一个文件后,将文件名自动添加到一个独立的记录框内,以
- 你甚至可以在一行内将多个值赋值给多个变量>>> a , b = 45, 54>>> a45>>
- 1. 介绍灰度切割:增强特定范围的对比度,突出图像 * 定范围的亮度(灰度级分层也叫灰度切割)实现灰度切割的方法有很多种,但基本的方法就两种,
- 本文以一个简单的实例讲述了python实现斐波那契数列数列递归函数的方法,代码精简易懂。分享给大家供大家参考之用。主要函数代码如下:def
- 本文实例讲述了Python面向对象类继承和组合。分享给大家供大家参考,具体如下:在python3中所有类默认继承object,凡是继承了ob
- 组合框 Combobox 简介Combobox 可以翻译为组合框,这是tkinter.ttk 的 Widget控件,它的特性与OptionM
- 折纸是日本著名的折叠纸张的艺术。折纸艺术只是使用一些不同的折叠方式,却能被用各种各样的方式组合成错综复杂的设计。而受折纸启发的logo设计则
- 解决方案1.安装django-cors-headerspip install django-cors-headers2.配置settings
- xml.etree.ElementTree可以通过支持的有限的XPath表达式来定位元素。语法ElementTree支持的语法如下:语法说明
- 1.导言现今的公司需要易访问的和可用性好的商业数据,以便他们可以在全球市场中获得一席之地。与易访问数据的这个需求相呼应的,关系数据库和分析数
- 以下工具类代码来自开源项目pyslam。Timerimport cv2 class Colors(object): &
- 使用del和drop方法删除DataFrame中的列,使用drop方法一次删除多列数据准备:import pandas as pd