【NumPy】1. n维数组,dtype,切片,索引

NumPy是python的一个第三方库,全称"Numeric Python"。他可以执行数组的算数和逻辑运算、线性代数等多方面操作,如何安装这个库,这里就不说了,之前数学建模比赛的时候也看过这个库,但是学的都比较粗糙,这次比较系统的了解下这个包,先说说NumPy中最重要的一个对象——n维数组

本文所有numpy包导入统一命名为np

import numpy as np

1. Ndarray 对象

全称为 N-dimensional array,译为n维数组,它是相同类型的元素集合

ndarray类的实例可以通过许多不同的数组创建来构造,基本的ndarray是使用NumPy中的数组函数创建的,他的构造方法如下

np.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
  • 参数
参数 特征属性 特征描述
object array_like 一个数组、任何暴露数组接口方法的对象、对象的__array__()方法返回数组的对象、任何(嵌套)序列【最后这个我没看懂啥意思】
dtype data-type, optional 数组的数据属性,如果没有给出,那么数据类型(dtype)将会根据数组序列对象所需的最小空间进行自动转换
copy bool, optional 如果为True(默认),那么这个对象是已被拷贝的(??)。这个没弄明白,大佬知道的可以在评论区里踢我一脚
order {‘K’, ‘A’, ‘C’, ‘F’}, optional 用于声明数组内存的排列规则(即怎么存储元素),C:按行排列(C Order) F:按列排列(Fortan Order)
A:任意(由输入的数据排列规则定), K(不懂)
subok bool, optional 默认情况下(False)返回数组会被强制转为基类数组,如果为True返回子类
ndmin int, optional 指定返回数组的最小维数
like array_like 该参数可以让数组创建出来的对象不一定是NumPy arrays,让返回的对象为like关键字指定的类似数组的(array_like)对象.

上面是我自己的见解,想要更好的了解,下面这是源码(笑)

array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0,like=None)'''Create an array.Parameters----------object : array_likeAn array, any object exposing the array interface, an object whose__array__ method returns an array, or any (nested) sequence.dtype : data-type, optionalThe desired data-type for the array.  If not given, then the type willbe determined as the minimum type required to hold the objects in thesequence.copy : bool, optionalIf true (default), then the object is copied.  Otherwise, a copy willonly be made if __array__ returns a copy, if obj is a nested sequence,or if a copy is needed to satisfy any of the other requirements(`dtype`, `order`, etc.).order : {'K', 'A', 'C', 'F'}, optionalSpecify the memory layout of the array. If object is not an array, thenewly created array will be in C order (row major) unless 'F' isspecified, in which case it will be in Fortran order (column major).If object is an array the following holds.===== ========= ===================================================order  no copy                     copy=True===== ========= ==================================================='K'   unchanged F & C order preserved, otherwise most similar order'A'   unchanged F order if input is F and not C, otherwise C order'C'   C order   C order'F'   F order   F order===== ========= ===================================================When ``copy=False`` and a copy is made for other reasons, the result isthe same as if ``copy=True``, with some exceptions for 'A', see theNotes section. The default order is 'K'.subok : bool, optionalIf True, then sub-classes will be passed-through, otherwisethe returned array will be forced to be a base-class array (default).ndmin : int, optionalSpecifies the minimum number of dimensions that the resultingarray should have.  Ones will be pre-pended to the shape asneeded to meet this requirement.like : array_likeReference object to allow the creation of arrays which are notNumPy arrays. If an array-like passed in as ``like`` supportsthe ``__array_function__`` protocol, the result will be definedby it. In this case, it ensures the creation of an array objectcompatible with that passed in via this argument.
'''
  • example
x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]], dtype=np.intc)
print(x)
'''
[[ 0  1  2][ 3  4  5][ 6  7  8][ 9 10 11]]'''x1 = np.array([1, 0, True, False, np.True_, np.False_], dtype=np.bool_)
print(x1)
'''
[ True False  True False  True False]
'''x2 = np.array([1, 2, 3, 4], dtype=np.float_)
print(x2)
'''
[1. 2. 3. 4.]
'''x3 = np.array([1, 2, 3, 4], dtype=np.complex_)
print(x3)
'''
[1.+0.j 2.+0.j 3.+0.j 4.+0.j]
'''

这个试验我们也可以发现,numpy库不仅有许多的数据类型,也可以兼容python内置类型

ndarray数组可以通过很多方法去进行构造,比较常用的有linspace()(line space,线性空间),logspace()(指数空间),eye()(单位矩阵),zero()(零矩阵)还有许多的方法都可以构造出ndarray对象

2. 数据类型-Dtype

刚刚的例子里面就用了许多NumPy的标量数据类型

数据类型 描述
bool_ 存储一个字节的布尔值
int_ 默认整数,相当于C的long,通常为int32或int64
intc 相当于C的int,通常为int32或int64
intp 用于索引的整数,相当于C的size_t,通常为int32或int64
int8 8位整数(-128~127)
int16 16位整数(-32768~32767)
int32 32位整数(-2147483648~2147483647)
uint8 8位无符号整数(0~255)
uint16 16位无符号整数(0 ~ 65535)
uint32 32位无符号整数(0 ~ 4294967295)
uint64 64位无符号整数(0 ~ 18446744073709551615)
float_ float64的简写
float16 半精度浮点:符号位,5位指数,10位尾数
float32 单精度浮点:符号位,8位指数,23位尾数
float64 双精度浮点:符号位,11位指数,52位尾数
complex_ complex128的简写
complex64 复数,由两个64位浮点表示(实部和虚部)
complex128 复数,由两个64位浮点表示(实部和虚部)

这些数据类型都是dtype实例对象的唯一特征

# 使用数组标量类型
import numpy as np
dt = np.dtype(np.int32)
print(dt)
'''
int32
'''

3. 切片与索引

3.1 基础切片介绍

ndarray对象的内容可以通过索引或者切片的方式去进行修改,这也是我之前接触python觉得特别魔法的一个特点之一,不过知道它是什么个原理之后,觉得也不是特别神奇了,不过确实方便。

ndarray对象中的元素遵循基于零的索引。

有三种可用的索引方法型:字段访问,基本切片和高级索引

3.2 切片格式

先讲讲python中的切片

对于一个python内置容器(如str类),用切片的方式访问它内部序列元素,可以遵循下面这个格式

str1[start:end:step]

start:开始元素下标,若缺省则默认为容器第一个元素

end:结束元素下表,若缺省则默认为容器最后一个元素

step:步长值(就是每隔几个元素就取出来,在a1、a2、a3、a4的序列中,a1与a2的步长值是1,a1与a3的步长值 为3),若缺省则默认为1

注:start不一定要比end小,step也不一定为正

如若第一个start参数不写,但要写第二个end参数或step参数,必须要写出":"符号用以识别参数类型,详细演示可以看例子,我嘴笨

两个数之间必须要有":"间隔

不能同时写三个":"

a[::]        正确
a[:::]       错误
a[:5]        正确
a[:5:]       正确
a[::2]       正确
a[2::1]      正确
a[9:2:2]     正确
a[9:2:-1]    正确
  • example
import numpy as np
a = np.array(range(0, 10))print(a[::])
"""[0 1 2 3 4 5 6 7 8 9]"""
print(a[5:])
"""[5 6 7 8 9]"""
print(a[5:6])
"""[5]"""
print(a[:6])
"""[0 1 2 3 4 5]"""
print(a[::2])
"""[0 2 4 6 8]"""
print(a[2:7:2])
"""[2 4 6]"""
print(a[8:3:-1])
"""[8 7 6 5 4]"""

上面的描述也可以适用于多维的ndarray,下面是二维数组

import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print(a)
# 对始于索引的元素进行切片
print('现在我们从索引 a[1:] 开始对数组切片')
print(a[1:])
[[1 2 3][3 4 5][4 5 6]]
现在我们从索引 a[1:] 开始对数组切片
[[3 4 5][4 5 6]]

切片还可以包括省略号(…),用于切片表示该维度的所有元素,如果在行位置使用"…",那么则表示所有行的元素都被选中了,比如说一个3*3的矩阵 […, 1]意味着选中了第二列的所有元素(行和列都可以切片表示哦 如[0:2, 1:3])

import numpy as npa = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
print('我们的数组是:')
print(a)
# 这会返回第二列元素的数组:
print('第二列的元素是:')
print(a[..., 1])
# 现在我们从第二行切片所有元素:
print('第二行的元素是:')
print(a[1, ...])
# 现在我们从第二列向后切片所有元素:
print('第二列及其剩余元素是:')
print(a[..., 1:])
我们的数组是:
[[1 2 3][3 4 5][4 5 6]]
第二列的元素是:
[2 4 5]
第二行的元素是:
[3 4 5]
第二列及其剩余元素是:
[[2 3][4 5][5 6]]

4. 高级索引

如果一个ndarray是非元组序列,数据类型为整数或布尔值的ndarray,或者至少一个元素为序列对象的元组,我们就能够用它来索引ndarray。高级索引始终返回数据的副本。 与此相反,切片只提供了一个视图。

有两种类型的高级索引:整数和布尔值。

4.1 整数索引

这种机制有助于基于 N 维索引来获取数组中任意元素。 每个整数数组表示该维度的下标值。 当索引的元素个数就是目标ndarray的维度时,会变得相当直接。

以下示例获取了ndarray对象中每一行指定列的一个元素。 因此,行索引包含所有行号,列索引指定要选择的元素。

import numpy as np x = np.array([[1,  2],  [3,  4],  [5,  6]])
y = x[[0,1,2],  [0,1,0]]
print(y)
"""
输出了数组中(0,0), (1,1), (2,0)位置处的元素
[1 4 5]
"""

4.2 布尔索引

这个比较容易理解,直接上例子

import numpy as npa = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(a[a > 5])
"""[6 7 8 9]"""

注:那是个布尔表达式,你直接写个True False都行,这意味着你在里面写一些filter()的布尔函数去过筛选你想要的信息都是可以的

【Numpy】1. n维数组,dtype,切片,索引相关推荐

  1. Numpy 笔记: 多维数组的切片(slicing)和索引(indexing)【转】

    目录 切片(slicing)操作 索引(indexing) 操作 最简单的情况 获取多个元素 切片和索引的同异 切片(slicing)操作 Numpy 中多维数组的切片操作与 Python 中 lis ...

  2. python运算学习之Numpy ------ 数组的切片索引与循环遍历、条件和布尔数组、形状变化...

    数组的切片索引: 数组的切片索引和列表非常类似,下面用代码简单说明 1 a = np.random.rand(16).reshape(4, 4) 2 print("数组a:\n", ...

  3. 图解数据分析(9) | Numpy - 与1维数组操作(数据科学家入门·完结)

    作者:韩信子@ShowMeAI 教程地址:https://www.showmeai.tech/tutorials/33 本文地址:https://www.showmeai.tech/article-d ...

  4. 图解数据分析(10) | Numpy - 与2维数组操作(数据科学家入门·完结)

    作者:韩信子@ShowMeAI 教程地址:https://www.showmeai.tech/tutorials/33 本文地址:https://www.showmeai.tech/article-d ...

  5. python一维数组和二维数组_Python numpy实现二维数组和一维数组拼接的方法

    Python numpy实现二维数组和一维数组拼接的方法 撰写时间:2017.5.23 一维数组 1.numpy初始化一维数组 a = np.array([1,2,3]); print a.shape ...

  6. Numpy之N维数组-ndarray

    Numpy之N维数组-ndarray 1 ndarray的属性 数组属性反映了数组本身固有的信息. 属性名字 属性解释 ndarray.shape 数组维度的元组    [假如是8行5列的数组,将会返 ...

  7. NumPy 创建多维数组

    NumPy 创建多维数组 arange 函数创建的数组作为列表元素,把这个列表作为参数传给 array 函数,从而创建了一个 2 × 2 的数组,而且没有出现任何报错信息. Example 1 #!/ ...

  8. numpy获取二维数组某一行、某一列

    numpy获取二维数组某一行.某一列 1.需求 有一个二维数组: a = [[1, 2, 3, 4, 5, 6],[7, 8, 9, 10, 11, 12],[13.2, 14.8, 15.9, 16 ...

  9. Python numpy实现二维数组和一维数组拼接

    撰写时间:2017.5.23 一维数组 1.numpy初始化一维数组 a = np.array([1,2,3]); print a.shape 输出的值应该为(3,) 二维数组 2.numpy初始化二 ...

  10. python一维数组和二维数组,Python numpy实现二维数组和一维数组拼接的方法

    撰写时间:2017.5.23 一维数组 1.numpy初始化一维数组 a = np.array([1,2,3]); print a.shape 输出的值应该为(3,) 二维数组 2.numpy初始化二 ...

最新文章

  1. 为了不复制粘贴,我被逼着学会了JAVA爬虫
  2. 23LC1024四线访问数据
  3. uvm 形式验证_一种基于UVM的总线验证方法与流程
  4. JVM—堆栈 堆 方法区 静态区 final static 内存分配
  5. 【前端自动化构建】之 自动化部署
  6. 关于SWT中的Combo类和List类
  7. 拿完年终奖换工作?频繁跳槽职场人工资低于同龄人平均水平
  8. addActionError addFieldErrot addActionMessage 的区别
  9. ubuntu LVS+keepalived 笔记
  10. Audio Precision SYS-2722音频分析仪
  11. android代码禁用软键盘,Android 禁用软键盘
  12. YOLO3+GPU(gtx1060)
  13. day2——泰波那契数列
  14. 小白记录第一个Android APP,VS2019,Xamarin,C#
  15. redis.set方法详解
  16. java服务和net服务_艾伟_转载:Java和.NET互操作:我们应该放弃Web Service吗?
  17. 一维数组与对象深拷贝的几种方法(指数组、对象中均无嵌套)
  18. linux常用关机命令shutdown、halt、poweroff、init用法
  19. Windows Store协议(ms-windows-store)解析和使用
  20. 【CS224n】(lecture9)Transformer的变体

热门文章

  1. Python培训得多少钱
  2. 开启win10防火墙开启某端口
  3. Linux基础-用户管理与组管理
  4. 【SmartSvn】分支合并问题
  5. 在线PS编辑器使用教程(Photoshop)
  6. Angular快速上手
  7. 七年之后的《深入理解计算机系统》CSAPP
  8. 今日头条内推码URQVEYM社招,校招,实习应有尽有
  9. base64图片上传解析不了问题
  10. 如何申请微信公众平台帐号