** Development environment: Jupyter Notebook **
First, read.
import numpy as np
It means "load a library called numpy as np". By declaring this, the method can be referred to as np.
a = np.array([0, 1, 2])
print(a)
#array([0, 1, 2])
a[2]
#2
Create an array with np.array. In the case of a one-dimensional array, it can be created with array ([]). Any value can be called with a [].
a = np.array([[0, 1, 2], [3, 4, 5]])
print(a)
#array([[0, 1, 2],
# [3, 4, 5]])
a[0]
#array([0, 1, 2])
a[1]
#array([3, 4, 5])
a[0][0]
#0
a[1][2]
#5
In the case of a two-dimensional array, it can be created with array ([], []). Here, a [] calls the value for each row. With a [] [], you can specify a row and a column to retrieve a single value.
shape, ndim, dtype, size
a.shape
#(2, 3)
a.ndim
#2
a.dtype
#'int64'
a.size
#6
You can retrieve the number of rows and columns with ** a.shape **. ** a.ndim ** represents the number of dimensions. In this case, 2 is taken out because it is a two-dimensional matrix. ** a.dtype ** represents the number of bits. This time, it is a 2x3 matrix, so if one value is 2 bits, 2 to the 6th power is calculated as int64. ** a.size ** is displayed as 6 from the 2x3 matrix.
arange, linspace
np.arange(6)
#array([0, 1, 2, 3, 4, 5])
np.arange(0, 20, 5)
#array([0, 5, 10, 15])
np.linspace(0, 20, 5)
#array([0., 5., 10., 15., 20.])
** np.arange ** can make arrays with arithmetic progression. In the above example, simply writing 6 will extract 0 to 6 elements. Alternatively, it is represented as an array starting from 0 and increasing by 5 from 0 to less than 20. ** np.linspace ** can be divided into equal parts to create an array. In the above example, the array is created by dividing it into 5 equal parts, starting from 0 and ending with 20.
zeros, ones
np.zeros((2, 3))
#array([[0., 0., 0.],
# [0., 0., 0.]])
np.ones((2, 3))
#array([[1., 1., 1.],
# [1., 1., 1.]])
** np.zeros ** can create an array with 0 as an element. Similarly, ** np.ones ** has 1 as an element.
append, vstack, hstack
a = np.arange(5)
b = np.arange(0, 10, 2)
c = np.arange(0, 100, 20)
a
#array([0, 1, 2, 3, 4])
b
#array([0, 2, 4, 6, 8])
c
#array([0, 20, 40, 60, 80])
np.append(a, b)
#array([0, 1, 2, 3, 4, 0, 2, 4, 6, 8])
np.vstack([a, b, c])
#array([[0, 1, 2, 3, 4],
# [0, 2, 4, 6, 8],
# [0, 20, 40, 60, 80]])
np.hstack([a, b, c])
#array([0, 1, 2, 3, 4, 0, 2, 4, 6, 8, 0, 20, 40, 60, 80])
** np.append ** can be followed by array values. ** np.vstack ** allows you to arrange the array row by row, that is, vertically. In addition, ** np.hstack ** can arrange arrays horizontally (horizontal).
random, sum, min, max, mean
a = np.random.rand((2, 3))
a
#array([[0.3026967 , 0.7533224 , 0.26057491],
# [0.91502592, 0.54166851, 0.60867887]])
a.sum()
# 3.381967315194162
a.min()
# 0.26057491207931693
a.max()
# 0.9150259161906477
a.mean()
# 0.563661219199027
** np.random.rand ** can randomly describe numbers from 0 to less than 1. ** a.sum ** is the sum of all values, ** a.min ** is the minimum value of the array, ** a.max ** is the maximum value of the array, ** a.mean ** is the minimum value of the array Represents the average value.
Recommended Posts