0%

Numpy笔记(紧急ver.)

Basic properties of Arrays

数组:

​ 1.任意维度,包括0(a scalar)

​ 2.类型: np.unit8, np.int64, np.float32, np.float64

​ 3.密集的,元素的类型相同

​ 4.不同shape无法combine

1
2
a = np.array([1,2,3],[4,5,6],dtype = np.float32)
print(a.ndim, a,shape, a.dtype)
1
2 (2,3) float32

The creation of array

  • np.zeros/np.ones/_like
1
2
3
4
5
np.zeros((6,2), dtype = np.int8)
np.ones((3,5), dtype = np.float32)

a = np.ones((2,2,3))
b = np.zeros_like(a)
  • np.arange
1
np.arange(133,1338)
  • np.concatenate
1
2
3
4
5
6
7
A = np.ones((2,3))
B = np.zeros((3,3))
np.concatenate([A,B], axis = 0)#axis=0, 竖着加

A = np.ones((4,1))
B = np.zeros((4,2))
np.concatenate([A,B], axis = 0)#axis=0, 横着加
  • np.random.random
1
np.random.random((10,3))

Shaping

元素总数不能变

1
2
3
a = np.array([1,2,3,4,5,6])
b = a.reshape(3,2)
c = a.reshape(2,-1)

Transposition

1
2
a = np.array([1.,2.],[3.,4.])
b = a.T

Array sorting

1
2
3
4
array4 = np.array([1,0,2,-3,6,8,4,7])
array4.sort()

#二维也能排,[i][j],i不变,排j

Statistical operations

算术操作,element-wise

1
2
3
a = np.array([1,2,3])
b = np.array([4,4,10])
a*b

逻辑操作,返回bool array

1
2
a = np.random.random((5,3))#5行3列浮点数(0-1)
c = a > 0.5

计算操作

1
2
3
4
5
6
7
a = np.array([1,4],[9,16],[25,36])

b = np.sqrt(a)
a.max()
a.min()
a.mean()
a.std()

Array spliting

1
2
a = np.array([1,4],[9,16],[25,36],[40,50])
first, second = np.split(a, 2, axis=0)

Array Indexing

arr[0:6:2] => range(0,6,2)

Array Slicing

1
2
3
4
5
6
7
8
9
10
11
12
13
[2,3,1,5,6]

arr[1:4]
[3,1,5]

arr[:4]
[2,3,1,5]

arr[1:]
[3,1,5,6]

arr[:]
[2,3,1,5,6]

Lab5 Numpy

create an array of the integers from 20 to 50

1
2
3
import numpy as np
array = np.arange(20,51)
print(array)
1
[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 49 50]

create an array of the integers from 0 to 50 with evenly spacing of 10

1
2
3
import numpy as np

array = np.linspace(0,50,6)
1
[ 0. 10. 20. 30. 40. 50.]

show different properties of the numpy array

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

array = np.arange(20)
print(array)

array = array.reshape(4,5)
print(array)
print(type(array))
print(array.ndim)
print(array.shape)
print(array.dtype)
print(array.itemsize)
print(array.size)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]

<class 'numpy.ndarray'>

2

(4, 5)

int32

4

20

create an array of thre integers from 9 to 31 and print all values except the first and the last

1
2
3
import numpy as np
array = arange(9,32)
print(array[1:-1])
1
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30]

create an array of 5 zeros, 5 ones, 5 fives

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
print('An array of 5 zeros:')
array = np.zeros(5)
print(array)

print('An array of 5 ones:')
array = np.ones(5)
print(array1)

print('An array of 5 fives:')
array = np.ones(5) * 5
print(2)
1
2
3
4
5
6
An array of 5 zeros:
[0. 0. 0. 0. 0.]
An array of 5 ones:
[1. 1. 1. 1. 1.]
An array of 5 fives:
[5. 5. 5. 5. 5.]

create 5x5 zero matrix with elements with the diagonal to 5,4,3,2,1

1
2
3
import numpy as np
array = np.diag([5,4,3,2,1])
print(array)
1
2
3
4
5
[[5 0 0 0 0]
[0 4 0 0 0]
[0 0 3 0 0]
[0 0 0 2 0]
[0 0 0 0 1]]

find missing item in a given array

1
2
3
4
5
6
7
import numpy as np

array = np.array([[1,1,np.nan,1],
[np.nan,1,1,1],
[1,np.nan,1,1]])
print('\nFind the missing data of the said array:')
print(np.isnan(array))
1
2
3
4
Find the missing data of the said array:
[[False False True False]
[ True False False False]
[False True False False]]

indexing row and col

1
2
3
4
5
6
7
import numpy as np
array = np.array(([5,10,15],[20,25,30],[35,40,45]))
array[1]
array[1][0]
print(array)
print('----')
print(array[:2,1:])
1
2
3
4
5
6
7
8
array([20, 25, 30])
20
[[ 5 10 15]
[20 25 30]
[35 40 45]]
----
[[10 15]
[25 30]]