0%

NumPy数组之创建、基本属性、改变形状

这里介绍NumPy数组的创建、基本属性,并说明改变数组形状的相关方法

abstract.png

基本属性

NumPy数组的基本属性如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np

array1 = np.arange(15).reshape(3,5)
print(array1)
# 输出:
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]

# 查看numpy数组对象的类型
print(f"type(array1) : {type(array1)}")
# 输出 : type(array1) : <class 'numpy.ndarray'>

# 数组维度的个数,即秩Rank或轴Axis的数量
dim = array1.ndim
print(f"dim: {dim}")
# 输出 : dim: 2

# 数组形状(即数组的维度,表示每个维度中数组的大小)。对于一个n行、m列的矩阵而言,shape是(n,m)
shape = array1.shape
print(f"shape: {shape}")
# 输出 : shape: (3, 5)

# 数组元素的总数
size = array1.size
print(f"size: {size}")
# 输出 : size: 15

# 数组元素的类型
data_type = array1.dtype
print(f"data type: {data_type}")
# 输出 : data type: int64

# 数组元素的字节大小(Unit:字节)
item_size = array1.itemsize
print(f"item size: {item_size}")
# 输出 : item size: 8

# 数组对象的内存布局信息
flag = array1.flags
print(f"flag: {flag}")
# 输出 :
# flag: C_CONTIGUOUS : True
# F_CONTIGUOUS : False
# OWNDATA : False
# WRITEABLE : True
# ALIGNED : True
# WRITEBACKIFCOPY : False

创建数组

从Python列表、元组中创建数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import numpy as np

# 从Python 列表、元组中创建数组
a1 = np.array( (1.1, 2.2, 3.3) )
a2 = np.array( [[2,3,4], [7,8,91]] )

print(a1)
# 输出: [1.1 2.2 3.3]
print(f"a1.dtype: {a1.dtype}")
# 输出:a1.dtype: float64

print(a2)
# 输出:
# [[ 2 3 4]
# [ 7 8 91]]
print(f"a2.dtype: {a2.dtype}")
# 输出: a2.dtype: int64

arange 函数

np.arange(start, stop, step, dtype)函数。创建一个一维数组:元素值从start开始到stop(不包含)、步长为step。其中,start参数:默认为0;step参数:默认为1

1
2
3
4
5
6
7
8
9
import numpy as np

a3 = np.arange(7)
print(a3)
# 输出 : [0 1 2 3 4 5 6]

a4 = np.arange(0,8,2)
print(a4)
# 输出 : [0 2 4 6]

linspace 函数

np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)函数。创建一个一维数组:元素值是一个从start到stop的等差数列、元素数量为num个。其中,endpoint参数值为True时,数列中包含stop值,反之不包含。默认是True。其与arange函数虽然生成的元素值都是等差数列。但区别在于:后者是指定步长,前者是指定元素数量

1
2
3
4
5
6
7
8
9
import numpy as np

a4 = np.linspace(0,6,4, endpoint=True)
print(a4)
# 输出 : [0. 2. 4. 6.]

a5 = np.linspace(0,6,4, endpoint=False)
print(a5)
# 输出 : [0. 1.5 3. 4.5]

empty/empty_like 函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np

# 创建指定形状的数组,元素值随机
a1 = np.empty((1,5))
print(a1)
# 输出: [[ 1. 2.75 6. 10.75 17. ]]

num = np.arange(6).reshape(2,3)
print(num)
# 输出:
# [[0 1 2]
# [3 4 5]]

# 创建 与指定数组相同形状 的数组,元素值随机
a2 = np.empty_like(num)
print(a2)
# 输出:
# [[0 0 0]
# [0 0 0]]

full/full_like 函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np

# 创建指定形状的数组,元素为指定值
a1 = np.full((1,4), 996)
print(a1)
# 输出: [[996 996 996 996]]

num = np.arange(6).reshape(2,3)
print(num)
# 输出:
# [[0 1 2]
# [3 4 5]]

# 创建 与指定数组相同形状 的数组,元素为指定值
a2 = np.full_like(num, 1314)
print(a2)
# 输出:
# [[1314 1314 1314]
# [1314 1314 1314]]

zeros/zeros_like 函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np

# 创建指定形状的数组,元素全为0,元素类型为uint8
a1 = np.zeros((1,4), dtype=np.uint8)
print(a1)
# 输出: [[0 0 0 0]]

num = np.arange(6).reshape(2,3)
print(num)
# 输出:
# [[0 1 2]
# [3 4 5]]

# 创建 与指定数组相同形状 的数组,元素全为0
a2 = np.zeros_like(num)
print(a2)
# 输出:
# [[0 0 0]
# [0 0 0]]

ones/ones_like 函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np

# 创建指定形状的数组,元素全为1,元素类型为float16
a1 = np.ones((1,4), dtype=np.float16)
print(a1)
# 输出: [[1. 1. 1. 1.]]

num = np.arange(6).reshape(2,3)
print(num)
# 输出:
# [[0 1 2]
# [3 4 5]]

# 创建 与指定数组相同形状 的数组,元素全为1
a2 = np.ones_like(num)
print(a2)
# 输出:
# [[1 1 1]
# [1 1 1]]

eye 函数

创建一个 NxN 的单位矩阵(主对角线元素全为1,其余元素均为0)

1
2
3
4
5
6
7
8
9
10
import numpy as np

# 创建NxN的单位矩阵(主对角线元素全为1,其余元素均为0)
m1 = np.eye(4, dtype=np.int32)
print(m1)
# 输出:
# [[1 0 0 0]
# [0 1 0 0]
# [0 0 1 0]
# [0 0 0 1]]

改变形状

reshape 函数

reshape函数将数组改变为指定形状。使用-1时,可自动计算该维度的大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np

num1 = np.arange(12)
print(num1)
# 输出: [ 0 1 2 3 4 5 6 7 8 9 10 11]

num2 = num1.reshape(2,6)
print(num2)
# 输出:
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]]

num3 = num1.reshape(-1, 3)
print(num3)
# 输出:
# [[ 0 1 2]
# [ 3 4 5]
# [ 6 7 8]
# [ 9 10 11]]

ravel/flatten 函数

二者均可将多维数组展平为一维数组:

  • ravel函数:返回的可能是原数组的视图。具体地,如果可能的话则创建视图,否则创建副本。故对返回结果进行修改可能会影响到原数组
  • flatten函数:返回的是原数组的副本。故对返回结果进行修改,不会影响原数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np

num = np.arange(6).reshape(2,3)
num2 = num.ravel()
num3 = num.flatten()

num2[0] = 52996
num3[1] = -7788

print(num)
# 输出:
# [[52996 1 2]
# [ 3 4 5]]

print(num2)
# 输出: [52996 1 2 3 4 5]

print(num3)
# 输出: [ 0 -7788 2 3 4 5]

转置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import numpy as np

num = np.arange(6).reshape(2,3)
print(num)
# 输出:
# [[0 1 2]
# [3 4 5]]

print(f"num shape: {num.shape}")
# 输出: num shape: (2, 3)

num2 = num.T
print(num2)
# 输出:
# [[0 3]
# [1 4]
# [2 5]]

print(f"num2 shape: {num2.shape}")
# 输出: num2 shape: (3, 2)

vstack/hstack函数

  • vstack函数:垂直堆叠。要求拼接的数组列数相同
  • hstack函数:水平堆叠。要求拼接的数组行数相同
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np

num1 = np.array([[1,2,3],[4,5,6]])
num2 = np.array([[-1,-2,-3],[-4,-5,-6]])
print(num1)
# 输出:
# [[1 2 3]
# [4 5 6]]

print(num2)
# 输出:
# [[-1 -2 -3]
# [-4 -5 -6]]

c1 = np.vstack( (num1,num2) )
print(c1)
# 输出:
# [[ 1 2 3]
# [ 4 5 6]
# [-1 -2 -3]
# [-4 -5 -6]]

c2 = np.hstack( (num1, num2) )
print(c2)
# 输出:
# [[ 1 2 3 -1 -2 -3]
# [ 4 5 6 -4 -5 -6]]
请我喝杯咖啡捏~

欢迎关注我的微信公众号:青灯抽丝