This article is a record of learning for python beginners. I mainly write what I didn't understand. So the order is messy. For reference ,,,
NumPy np.array
# -*- coding: utf-8 -*-
import numpy as np
#Declaration / initialization of 2D array
A = np.array([[1, 2],
[3, 4],
[5, 6]])
#Matrix size
print("Matrix A size:", A.shape)
print("Number of rows in matrix A:", A.shape[0])
print("Number of columns in matrix A:", A.shape[1])
"""
Matrix A size:(3, 2)
Number of rows in matrix A:3
Number of columns in matrix A:2
"""
np.reshape shape [0] displays ** number of rows **, shape [1] displays ** number of columns **.
import numpy as np
a = np.arange(24)
print(a)
# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
print(a.shape)
# (24,)
print(a.ndim)
# 1
a_4_6 = a.reshape([4, 6])
print(a_4_6)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
print(a_4_6.shape)
# (4, 6)
print(a_4_6.ndim)
# 2
In np.ndim, you can easily find the number of elements. Reshape is as in the above code.
[:, 0] All 0th column. If it is a 2x2 array, if it is written without omission, it will be a [0: 2, 0]. A [a: b, c: d] means to extract columns from c to d (not including b and d) in the rows from a to b.
import numpy as np
a = np.arange(9).reshape((3, 3))
print(a)
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
print(np.where(a < 4, -1, 100))
# [[ -1 -1 -1]
# [ -1 100 100]
# [100 100 100]]
print(np.where(a < 4, True, False))
# [[ True True True]
# [ True False False]
# [False False False]]
print(a < 4)
# [[ True True True]
# [ True False False]
# [False False False]]
If the following is only a condition, true or false will appear.
Consolidation of arrays with the same number of columns. For rows, np.vstack.
In the sequence in (), extract without fog.
import numpy as np
a = np.array([0, 0, 30, 10, 10, 20])
print(a)
# [ 0 0 30 10 10 20]
print(np.unique(a))
# [ 0 10 20 30]
print(type(np.unique(a)))
# <class 'numpy.ndarray'>
Recommended Posts