numpy1

NumPy Introduction 1

Array

First step: Import numpy

1
import numpy as np

Multi-dimensions array

1
2
3
4
5
np.array([range(i,i+3)for i in[2,4,6]])

Out[2]:array([[2, 3, 4],
[4, 5, 6],
[6, 7, 8]])

Creating repeated array

create an array with length10,and all the values are 0.

1
2
3
np.zeros(10,dtype=int)

Out[3]:array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

create an array which is 3x5,type is float,and all the values are 1.

1
2
3
4
5
np.ones((3,5),dtype=float)

Out[5]:array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])

create an array which is 3x5,and all the values are 3.14.

1
2
3
4
5
np.full((3,5),3.14)

Out[6]:array([[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14]])

create an array with 5 numbers from 0~1 uniformly

1
2
3
np.linspace(0,1,5)

Out[7]:array([0. , 0.25, 0.5 , 0.75, 1. ])

create an array which is 3x3 with random number

1
2
3
4
5
6
np.random.random((3,3))
# 创建一个3×3的、均值为0、方差为1的
# 正态分布的随机数数组
Out[8]:array([[0.47224148, 0.23125945, 0.95002522],
[0.09738343, 0.88182864, 0.38184937],
[0.99395495, 0.42838151, 0.92674988]])

create an array which is 3x3 with random normal number from 0~1

1
2
3
4
5
np.random.normal(0,1,(3,3))

Out[9]:array([[-0.55049464, -0.35293695, 0.75417556],
[-0.63125306, 0.31639634, -2.24209815],
[ 1.44175974, -0.69736733, 0.30666294]])

create an array which is 3x3 with random integer between1and 10

1
2
3
4
5
np.random.randint(0,10,(3,3))

Out[10]:array([[1, 4, 4],
[3, 5, 6],
[6, 3, 3]])

create an array which is 4x4 ,its numbers have only one digit

1
2
3
4
5
6
np.eye(4)

Out[11]:array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])

create an array with 3 random number from the storage

1
2
3
np.empty(3)

Out[12]:array([1., 1., 1.])

The factors of array

ndim(the dimension),shape and size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
np.random.seed(0)
x1=np.random.randint(10,size=6)
x2=np.random.randint(10,size=(3,4))
x3=np.random.randint(10,size=(3,4,5))
x1.ndim

Out[13]:1


print('x3 ndim: ', x3.ndim)
print('x3 shape: ', x3.shape)
print('x3.size: ' , x3.size)

Out[14]:x3 ndim: 3
x3 shape: (3, 4, 5)
x3.size: 60


print('dtype: ',x3.dtype)

Out[15]:dtype: int32

Get one element

1
2
3
x1

Out[16]:array([5, 0, 3, 3, 7, 9])
1
2
3
x1[0]

Out[17]:5

From the end

1
2
3
x1[-1]

Out[18]:9

Use the comma

1
2
3
4
5
x2

Out[19]:array([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7]])
1
2
3
x2[0,0]

Out[20]:3
1
2
3
x2[2,-1]

Out[21]:7

Change element(x1=([5, 0, 3, 3, 7, 9]))

1
2
3
4
x1[0]=3.14
x1

Out[22]:array([3, 0, 3, 3, 7, 9]) #float->integer

slice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
x = np.arange(10)  
x
#Out: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

x[:5] # 前五个元素
#Out:array([0, 1, 2, 3, 4])

x[5:] # 索引五之后的元素
#Out:array([5, 6, 7, 8, 9])

x[4:7] # 中间的子数组
#Out:array([4, 5, 6])

x[::2] # 每隔一个元素
#Out:array([0, 2, 4, 6, 8])

x[1::2] # 每隔一个元素,从索引1开始
#Out:array([1, 3, 5, 7, 9])

multi-dimension:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
x2=array([[12, 5, 2, 4],  
[ 7, 6, 8, 8],
[ 1, 6, 7, 7]])
x2[:2, :3] # 两行,三列
#Out:array([[12, 5, 2],
# [ 7, 6, 8]])

x2[:3, ::2] # 所有行,每隔一列
#Out:array([[12, 2],
# [ 7, 8],
# [ 1, 7]])

x2[::-1, ::-1]
#Out:array([[ 7, 7, 6, 1],
# [ 8, 8, 6, 7],
# [ 4, 2, 5, 12]])

.copy()

1
2
3
4
5
6
7
8
9
10
x2_sub_copy = x2[:2, :2].copy()  
print(x2_sub_copy)
#out:[[99 5]
# [ 7 6]]


x2_sub_copy[0, 0] = 42
print(x2_sub_copy)
#out:[[42 5]
# [ 7 6]]

.reshape()

1
2
3
4
5
6
grid = np.arange(1, 10).reshape((3, 3))  
print(grid)

#out:[[1 2 3]
# [4 5 6]
# [7 8 9]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
x = np.array([1, 2, 3])  # 通过变形获得的行向量 
x.reshape((1, 3))
#Out:array([[1, 2, 3]])

# 通过newaxis获得的行向量
x[np.newaxis, :]
#Out:array([[1, 2, 3]])

# 通过变形获得的列向量
x.reshape((3, 1))
#Out:array([[1],
# [2],
# [3]])

# 通过newaxis获得的列向量
x[:, np.newaxis]
#Out:array([[1],
# [2],
# [3]])

.concatenate() (combine arrays)

1
2
3
4
5
x = np.array([1, 2, 3])  
y = np.array([3, 2, 1])
np.concatenate([x, y])

#Out:array([1, 2, 3, 3, 2, 1])
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
z = [99, 99, 99]  
print(np.concatenate([x, y, z]))
#out:[ 1 2 3 3 2 1 99 99 99]

#two dimension
grid = np.array([[1, 2, 3],
[4, 5, 6]])
# 沿着第一个轴拼接
np.concatenate([grid, grid])
#Out:array([[1, 2, 3],
# [4, 5, 6],
# [1, 2, 3],
# [4, 5, 6]])

# 沿着第二个轴拼接(从0开始索引)
np.concatenate([grid, grid], axis=1)
#out: array([[1, 2, 3, 1, 2, 3],
# [4, 5, 6, 4, 5, 6]])



#np.vstack(垂直栈)和 np.hstack(水平栈)
x = np.array([1, 2, 3])
grid = np.array([[9, 8, 7],
[6, 5, 4]])
# 垂直栈数组
np.vstack([x, grid])
#Out:array([[1, 2, 3],
# [9, 8, 7],
# [6, 5, 4]])

y = np.array([[99],
[99]])
np.hstack([grid, y])
#out: array([[ 9, 8, 7, 99],
# [ 6, 5, 4, 99]])

.split()

1
2
3
4
5
x = [1, 2, 3, 99, 99, 3, 2, 1]  
x1, x2, x3 = np.split(x, [3, 5])
print(x1, x2, x3)

#out:[1 2 3] [99 99] [3 2 1]

np.hsplit & np.vsplit

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
grid = np.arange(16).reshape((4, 4))  
grid
#Out:array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11],
# [12, 13, 14, 15]])

upper, lower = np.vsplit(grid, [2])
print(upper)
print(lower)
#out:[[0 1 2 3]
# [4 5 6 7]]
# [[ 8 9 10 11]
# [12 13 14 15]]

left, right = np.hsplit(grid, [2])
print(left)
print(right)
#out:[[ 0 1]
# [ 4 5]
# [ 8 9]
# [12 13]]
# [[ 2 3]
# [ 6 7]
# [10 11]
# [14 15]]