NumPy提供两种基本对象:
ndaaray:具有矢量算数运算和复杂广播能力的、快速且节省空间的多维数组
ufunc:提供了对ndarray进行快速运算的标准数学函数
python内置一个array模块,基于NumPy的ndarray用于改善Python内置array模块的不足
创建ndarray对象
可以有函数array、arange、linspace、logspace、zeros、eye、diag、ones等
>>>import numpy as np
>>> print(np.float64(42))
42.0
>>> print(np.int8(42.0))
42
>>> print(np.bool(42.0))
True
>>> print(np.bool(0))
False
>>> print(np.float(True))
1.0
>>> print(np.int8(False))
0
>>> arr1=np.array([1,2,3,4])
>>> print(arr1)
[1 2 3 4]
>>> arr2=np.array([[1,2,3,4],[5,6,7,8],[7,8,9,0]])
>>> print(arr2)
[[1 2 3 4]
[5 6 7 8]
[7 8 9 0]]
>>> print(arr2.ndim) //输出arr2的维数
1
>>> print(arr2.shape) //输出arr2的形状为三行四列
(3, 4)
>>> print(arr2.dtype) //数据类型
int32
>>> print(arr2.size) //元素个数
12
>>> print(arr2.itemsize) //每个元素的大小
4
>>>
使用array创建ndarray不方便,针对特殊ndarray,NumPy提供了其他ndarray创建函数
函数 | 说明 |
---|---|
arange | 等差数列(指定初值,终值和步长) |
linspace | 等差数列(初值,终值和元素个数) |
logspace | 等比数列 |
zeros | 创建值全部为0的矩阵 |
eye | 创建单位矩阵(对角元素为1,其余为0) |
diag | 创建对角矩阵(指定对角元素值) |
ones | 值全部为1 |
>>> print(np.arange(0,1,0.1))
[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
>>> print(np.linspace(0,1,5))
[0. 0.25 0.5 0.75 1. ]
>>> print(np.logspace(1,100,3))
[1.00000000e+001 3.16227766e+050 1.00000000e+100]
>>> print(np.zeros((2,3)))
[[0. 0. 0.]
[0. 0. 0.]]
>>> print(np.eye(3))
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
>>> print(np.diag([1,2,3,4]))
[[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]
>>> print(np.ones((2,3)))
[[1. 1. 1.]
[1. 1. 1.]]
>>>
>>> print(np.random.random(100))
[0.63602081 0.7871554 0.56669805 0.97232689 0.0207896 0.57886676
0.64372612 0.80944545 0.75625861 0.67988537 0.75305242 0.18779097
0.22286867 0.61746534 0.86972547 0.44246135 0.00986978 0.18127593
0.65710968 0.27313108 0.72685097 0.12067366 0.41628046 0.04307971
0.24247995 0.46670822 0.58212109 0.68185902 0.31784222 0.91730552
0.86549622 0.22060213 0.99813447 0.30118742 0.72242015 0.59733132
0.39043248 0.38082654 0.03043517 0.97147862 0.82292743 0.22384881
0.7169306 0.8825181 0.89461956 0.32500068 0.45718019 0.15929286
0.65813812 0.8351554 0.06884279 0.64110422 0.41440121 0.64855575
0.07405247 0.88464219 0.06292621 0.7436007 0.62355673 0.25817082
0.64247213 0.1390323 0.52428072 0.33015445 0.48986753 0.19207131
0.52060488 0.11653308 0.32619628 0.18787004 0.42350872 0.15531873
0.4156147 0.53344594 0.85196222 0.61324832 0.30390031 0.81542646
0.74295816 0.21674356 0.29266157 0.63118271 0.42007533 0.30041424
0.6127812 0.77155011 0.53334965 0.03351164 0.52071247 0.51383995
0.95198997 0.48252852 0.75558122 0.83169528 0.17272343 0.78615273
0.26860977 0.03654327 0.06662281 0.20921301]
>>> print(np.random.rand(4,5))
[[0.59623235 0.94651761 0.48764051 0.26966806 0.23325442]
[0.25353761 0.76190705 0.97678106 0.6760325 0.74904088]
[0.32867547 0.38998648 0.40502748 0.20397448 0.70017271]
[0.90263659 0.59492567 0.13946705 0.71230637 0.62057198]]
>>> print(np.random.randn(4,5))
[[-2.50346608 -1.01591325 -1.16951676 -1.6309351 0.43556252]
[ 1.18340704 0.3305206 -1.5341233 0.55052095 0.92401355]
[ 0.66107624 0.95493295 -0.94287665 -0.02508494 0.10526318]
[-1.10079438 0.06061194 -1.53572543 1.93348636 0.22956255]]
>>> print(np.random.randint(low=2,high=10,size=[2,5]))
[[4 7 6 4 4]
[3 2 7 9 5]]
ndarray的索引与切片
>>> arr=np.arange(10)
>>> print(arr[5])
5
>>> print(arr[3:5]) //使用元素位置切片结果
[3 4]
>>> print(arr[:5]) //省略单个位置切片结果
[0 1 2 3 4]
>>> print(arr[:-1]) //使用元素反向位置切片结果
[0 1 2 3 4 5 6 7 8]
>>> print(arr)
[0 1 2 3 4 5 6 7 8 9]
>>> arr[2:4]=100,101
>>> print(arr)
[ 0 1 100 101 4 5 6 7 8 9]
>>> print(arr[1:-1:2]) //元素位置等差索引
[ 1 101 5 7]
>>> print(arr[5:1:-2]) //元素位置负数步长等差索引
[ 5 101]
>>>