We have summarized ** 2D array operations (initialization, reference, extraction, calculation, transpose) ** that are inevitable when learning Python.
(* You need to install the numpy module. For mac, you can install it with pip3 install numpy
. For windows, refer to here, Install Python3, numpy, pandas, matplotlib, etc. on Windows)
Basically, write the code and output result with the following code.
ex.py
code = 'code'
#Output result
As an example, a two-dimensional array of 2 rows and 3 columns [[0, 0, 0], [0, 0, 0]]
is created by the following two methods.
ex1.py
a = [[0 for j in range(3)] for i in range(2)]
print(a)
# [[0, 0, 0], [0, 0, 0]]
a[0][0] = 1
print(a)
# [[1, 0, 0], [0, 0, 0]]
ex2.py
import numpy as np
a = np.zeros((2, 3))
print(a)
# [[0, 0, 0],
# [0, 0, 0]]
print(a)
a[0][0] = 1
# [[1, 0, 0],
# [0, 0, 0]]
Example: When substituting numbers from 0 to 5 into a 2-by-3 2-dimensional array ** (* -1 is used to automatically calculate the size of that dimension from other dimensions.) **
ex.py
import numpy as np
#0~Automatically generate arrays up to 5
a = np.arange(6)
print(a)
# [0 1 2 3 4 5]
#Convert one-dimensional array a to two-dimensional array
print(a.reshape(2, 3))
# [[0 1 2]
# [3 4 5]]
print(a.reshape(-1, 3))
# [[0 1 2]
# [3 4 5]]
print(a.reshape(2, -1))
# [[0 1 2]
# [3 4 5]]
To access by specifying the range, specify as X [start: end: step]
.
Note that end is not included at this time. (End is included in Matlab.)
If start is omitted, it will be from the beginning, if end is omitted, it will be the end, and if step is omitted, step will be 1.
In the case of multiple dimensions, specify each by separating them with commas (,). The method of specification is the same as for one-dimensional.
ex.py
import numpy as np
a = np.arange(12).reshape(3, 4)
print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
#a[Line range,Column range]
print(a[:3, 1:3])
# [[1 2]
# [5 6]
# [9, 10]
print(a[:, 1])
# [[1]
# [5]
# [9]]
ex.py
import numpy as np
a = np.arange(12).reshape(3, 4)
print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
#The part that satisfies the condition is True,A numpy array is returned where the unsatisfied part is False.
print(a > 5)
# [[False False False False]
# [False False True True]
# [ True True True True]]
#The value of the part that satisfies the condition is returned.
print( a[a > 5] )
# [ 6 7 8 9 10 11]
If the second argument ʻaxis is specified, the index with the maximum value is returned along each axis. For example, if ʻaxis = 0
, the row number of the maximum value for each column is returned. The maximum value of each column itself is obtained by np.max ()
and ʻaxis = 0`. (If axis = 1, the maximum column number for each row is returned.)
ex.py
import numpy as np
a = np.array([[20, 50, 30], [60, 40, 10]])
print(a)
# [[20 50 30]
# [60 40 10]]
#axis=If 1, the maximum column number for each column is returned.
print(np.argmax(a, axis=0))
# [1 0 0]
#Maximum value per column
print(np.max(a, axis=0))
# [60 50 30]
#Similarly axis=If 1, the maximum column number for each row is returned.
print(np.argmax(a, axis=1))
# [1 0]
#Maximum value per row
print(np.max(a, axis=1))
# [50 60]
Similarly, the following applications are possible.
・ Numpy.sum ()
: Sum
· Numpy.mean ()
: Mean
· Numpy.min ()
: Minimum / numpy.max ()
: Maximum
・ Others (numpy.std ()
: standard deviation / numpy.var ()
: variance, etc.)
You can get the transposed matrix of the original two-dimensional array (matrix) with the T
attribute.
ex1.py
import numpy as np
import numpy as np
a = np.arange(6).reshape(2, 3)
print(a)
# [[0 1 2]
# [3 4 5]]
a_T = a.T
print(a_T)
# [[0 3]
# [1 4]
# [2 5]]
-The T attribute returns a view (reference) of the original array, and changing one element changes the other.
-You can check if two ndarray
s refer to data in the same memory (one is the other view) withnp.shares_memory ()
.
ex2.py
print(np.shares_memory(a, a_T))
# True
a_T[0, 1] = 100
print(a_T)
# [[ 0 100]
# [ 1 4]
# [ 2 5]]
print(a)
# [[ 0 1 2]
# [100 4 5]]
If you want to process it as separate data, make a copy with copy ().
ex3.py
a_T_copy = a.T.copy()
print(a_T_copy)
# [[0 3]
# [1 4]
# [2 5]]
print(np.shares_memory(a, a_T_copy))
# False
a_T_copy[0, 1] = 100
print(a_T_copy)
# [[ 0 100]
# [ 1 4]
# [ 2 5]]
print(a)
# [[0 1 2]
# [3 4 5]]
I would be grateful if you could tell me any other good solutions or useful information to know.