目次
Step1 インプットデータ設定
1. 基本操作
1-1. 簡単定義方法
1次元配列
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d: ", sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5]
2次元配列
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d: ", sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5]
1-2. 要素アクセス [要素数:1]
1次元配列
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d[0]: ", sample_ndarray_1d[0])
print("sample_ndarray_1d[1]: ", sample_ndarray_1d[1])
print("sample_ndarray_1d[2]: ", sample_ndarray_1d[2])
print("sample_ndarray_1d[3]: ", sample_ndarray_1d[3])
print("sample_ndarray_1d[4]: ", sample_ndarray_1d[4])
print("sample_ndarray_1d[-1]: ", sample_ndarray_1d[-1])
sample_ndarray_1d[0]: 1
sample_ndarray_1d[1]: 2
sample_ndarray_1d[2]: 3
sample_ndarray_1d[3]: 4
sample_ndarray_1d[4]: 5
sample_ndarray_1d[-1]: 5
2次元配列
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d[0,0]: ", sample_ndarray_2d[0,0])
print("sample_ndarray_2d[0,1]: ", sample_ndarray_2d[0,1])
print("sample_ndarray_2d[0,2]: ", sample_ndarray_2d[0,2])
print("sample_ndarray_2d[0,3]: ", sample_ndarray_2d[0,3])
print("sample_ndarray_2d[0,4]: ", sample_ndarray_2d[0,4])
print("sample_ndarray_2d[1,0]: ", sample_ndarray_2d[1,0])
print("sample_ndarray_2d[1,1]: ", sample_ndarray_2d[1,1])
print("sample_ndarray_2d[1,2]: ", sample_ndarray_2d[1,2])
print("sample_ndarray_2d[1,3]: ", sample_ndarray_2d[1,3])
print("sample_ndarray_2d[1,4]: ", sample_ndarray_2d[1,4])
sample_ndarray_2d[0,0]: 1
sample_ndarray_2d[0,1]: 2
sample_ndarray_2d[0,2]: 3
sample_ndarray_2d[0,3]: 4
sample_ndarray_2d[0,4]: 5
sample_ndarray_2d[1,0]: 6
sample_ndarray_2d[1,1]: 7
sample_ndarray_2d[1,2]: 8
sample_ndarray_2d[1,3]: 9
sample_ndarray_2d[1,4]: 10
1-3. 要素アクセス [要素数:複数]
1次元配列
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d: ", sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5]
start_id = 1
end_id = 3
print("項目番号「1」から「3」:",sample_ndarray_1d[start_id:end_id+1])
print("項目番号「最初」から「3」:",sample_ndarray_1d[:end_id+1])
print("項目番号「1」から「最後」:",sample_ndarray_1d[start_id:])
print("全要素:",sample_ndarray_1d[:])
項目番号「1」から「3」: [2 3 4]
項目番号「最初」から「3」: [1 2 3 4]
項目番号「1」から「最後」: [2 3 4 5]
全要素: [1 2 3 4 5]
1-4. 配列長さ (Array Length)
1次元配列
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("配列長さ:",len(sample_ndarray_1d))
配列長さ: 5
2次元配列
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("親リスト長さ:",len(sample_ndarray_2d)) ### 親リスト
print("第一子リスト長さ:",len(sample_ndarray_2d[0])) ### 第一子リスト
親リスト長さ: 2
第一子リスト長さ: 5
1-5. 配列サイズ (Array Size)
1次元配列
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("配列サイズ:",sample_ndarray_1d.shape)
配列サイズ: (5,)
2次元配列
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("配列サイズ:",sample_ndarray_2d.shape)
配列サイズ: (2, 5)
1-6. 次元数 (Dimension Number)
1次元配列
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("次元数:",sample_ndarray_1d.ndim)
次元数: 1
2次元配列
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("次元数:",sample_ndarray_2d.ndim)
次元数: 2
1-7. 要素数 (Element Number)
1次元配列
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("要素数:",sample_ndarray_1d.size)
要素数: 5
2次元配列
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("要素数:",sample_ndarray_2d.size)
要素数: 10
1-8. 格納型 (Data Type)
1次元配列
import numpy as np
sample_ndarray = np.array([1,2,3,4,5])
print("格納型:",type(sample_ndarray))
格納型: <class 'numpy.ndarray'>
1-9. 要素データ型 (Element Data Type)
1次元配列
import numpy as np
sample_ndarray = np.array([1,2,3,4,5])
print("要素データ型:",sample_ndarray.dtype)
要素データ型: int32
1-10. 軸削除 [軸項目番号,軸方向] (Axis Delete)
1次元配列
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d: ",sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5]
deleted_ndarray_1d = np.delete(sample_ndarray_1d,2,0)
print("deleted_ndarray_1d: ",deleted_ndarray_1d)
deleted_ndarray_1d: [1 2 4 5]
2次元配列[削除:行]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d: ", sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
deleted_ndarray_2d = np.delete(sample_ndarray_2d,0,0)
print("deleted_ndarray_2d: ",deleted_ndarray_2d)
deleted_ndarray_2d: [[ 6 7 8 9 10]]
2次元配列[削除:列]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d: ", sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
deleted_ndarray_2d = np.delete(sample_ndarray_2d,2,1)
print("deleted_ndarray_2d: ",deleted_ndarray_2d)
deleted_ndarray_2d: [[ 1 2 4 5]
[ 6 7 9 10]]
1-11. 配列連結 [手法1]
1次元配列
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.concatenate((A,B),axis=0)
print("joint ndarray (Horizontal): ", C)
joint ndarray (Horizontal): [1 2 3 4 5 6]
2次元配列
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.concatenate((A,B),axis=0)
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D = np.concatenate((A,B),axis=1)
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
1-12. 配列連結 [手法2]
1次元配列
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.block([[A,B]])
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[1 2 3]
[4 5 6]]
D = np.block([[A],[B]])
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [1 2 3 4 5 6]
2次元配列
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.block([[A],[B]])
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D = np.block([[A,B]])
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
1-13. 配列連結 [手法3]
1次元配列
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.vstack((A,B))
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[1 2 3]
[4 5 6]]
D = np.hstack((A,B))
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [1 2 3 4 5 6]
2次元配列
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.vstack((A,B))
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D = np.hstack((A,B))
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
1-14. 配列連結 [要素連結による配列生成]
2次元配列
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.dstack((A,B))
print("stack ndarray: ", C)
stack ndarray: [[[ 1 7]
[ 2 8]
[ 3 9]]
[[ 4 10]
[ 5 11]
[ 6 12]]]
print("配列サイズ:",C.shape)
print("次元数:",C.ndim)
配列サイズ: (2, 3, 2)
次元数: 3
1-15. 配列スタック
2次元配列
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.stack((A,B))
print("stack ndarray: ", C)
stack ndarray: [[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
print("配列サイズ:",C.shape)
print("次元数:",C.ndim)
配列サイズ: (2, 2, 3)
次元数: 3
1-16. 配列分割 [手法1]
1次元配列
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5,6])
print("sample_ndarray_1d: ",sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5 6]
split_ndarray_1d = np.split(sample_ndarray_1d,2)
print("split_ndarray_1d[0]: ",split_ndarray_1d[0])
print("split_ndarray_1d[1]: ",split_ndarray_1d[1])
split_ndarray_1d[0]: [1 2 3]
split_ndarray_1d[1]: [4 5 6]
2次元配列[分割:行]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.split(sample_ndarray_2d,2,axis=0)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3 4 5 6]]
split_ndarray_2d[1]: [[ 7 8 9 10 11 12]]
2次元配列[分割:列]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.split(sample_ndarray_2d,2,axis=1)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3]
[7 8 9]]
split_ndarray_2d[1]: [[ 4 5 6]
[10 11 12]]
1-16. 配列分割 [手法2]
2次元配列[分割:行]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.vsplit(sample_ndarray_2d,2)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3 4 5 6]]
split_ndarray_2d[1]: [[ 7 8 9 10 11 12]]
2次元配列[分割:列]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.hsplit(sample_ndarray_2d,2)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3]
[7 8 9]]
split_ndarray_2d[1]: [[ 4 5 6]
[10 11 12]]
1-17. 配列分割 [要素分割による配列生成]
3次元配列
import numpy as np
sample_ndarray_3d = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
print("sample_ndarray_3d: ",sample_ndarray_3d)
sample_ndarray_3d: [[[1 2]
[3 4]]
[[5 6]
[7 8]]]
split_ndarray_3d = np.dsplit(sample_ndarray_3d,2)
print("split_ndarray_3d[0]: ",split_ndarray_3d[0])
print("split_ndarray_3d[1]: ",split_ndarray_3d[1])
split_ndarray_3d[0]: [[[1]
[3]]
[[5]
[7]]]
split_ndarray_3d[1]: [[[2]
[4]]
[[6]
[8]]]
2. データ定義
2-1. 零配列 (Zero Array)
1次元配列
import numpy as np
zero_ndarray_1d = np.zeros(5)
print("zero_ndarray_1d: ",zero_ndarray_1d)
zero_ndarray_1d: [0. 0. 0. 0. 0.]
2次元配列
import numpy as np
zero_ndarray_2d = np.zeros((2,3))
print("zero_ndarray_2d: ",zero_ndarray_2d)
zero_ndarray_2d: [[0. 0. 0.]
[0. 0. 0.]]
配列サイズコピー
import numpy as np
sample_ndarray = np.array([[1,2,3,4,5],[6,7,8,9,10]])
zero_ndarray = np.zeros_like(sample_ndarray)
print("zero ndarray: ",zero_ndarray)
zero ndarray: [[0 0 0 0 0]
[0 0 0 0 0]]
2-2. 単位配列 [手法1] (Identity Array)
単位配列[オフセット:無]
import numpy as np
identity_ndarray = np.eye(3)
print("identity ndarray: ",identity_ndarray)
identity ndarray: [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
単位配列[オフセット:有]
import numpy as np
offset = 1
identity_ndarray = np.eye(3,k=offset)
print("identity ndarray: ",identity_ndarray)
identity ndarray: [[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
import numpy as np
offset = -1
identity_ndarray = np.eye(3,k=offset)
print("identity ndarray: ",identity_ndarray)
identity ndarray: [[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]]
2-3. 単位配列 [手法2] (Identity Array)
単位配列[オフセット:無]
import numpy as np
identity_ndarray = np.identity(3)
print("identity ndarray: ",identity_ndarray)
2-3. 空白配列 (Empty Array)
1次元配列
import numpy as np
empty_ndarray_1d = np.empty(5)
print("empty_ndarray_1d: ",empty_ndarray_1d)
empty_ndarray_1d: [4.94065646e-324 4.94065646e-324 0.00000000e+000 0.00000000e+000
1.32373351e+079]
2次元配列
import numpy as np
empty_ndarray_2d = np.empty((2,3))
print("empty_ndarray_2d: ",empty_ndarray_2d)
empty_ndarray_2d: [[0. 0. 0.]
[0. 0. 0.]]
2-2. 特定値配列 (Specified Value Array)
1次元配列
import numpy as np
specified_value = 10
array_size = 5
specified_value_ndarray = np.empty(array_size)
specified_value_ndarray.fill(specified_value)
print("specified_value_ndarray: ",specified_value_ndarray)
specified_value_ndarray: [10. 10. 10. 10. 10.]
2次元配列
import numpy as np
specified_value = 3
array_size = (2,3)
specified_value_ndarray = np.empty(array_size)
specified_value_ndarray.fill(specified_value)
print("specified_value_ndarray: ",specified_value_ndarray)
specified_value_ndarray: [[3. 3. 3.]
[3. 3. 3.]]
2-4. 等差数列リスト (Arithmetic Sequence List)
1次元配列
start_num = 1
end_num = 10
difference = 1
arithmetic_sequence_ndarray = np.array(range(start_num, end_num + 1, difference))
print("arithmetic sequence ndarray: ",arithmetic_sequence_ndarray)