Python リストをNumPy配列ndarrayに変換: numpy.array()

NumPy配列ndarrayとPython標準のリストを相互に変換より
リストをNumPy配列ndarrayに変換: numpy.array()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import numpy as np
 
l_1d = [0, 1, 2]
 
arr_1d = np.array(l_1d)
 
print(arr_1d)
print(arr_1d.dtype)
# [0 1 2]
# int64
 
arr_1d_f = np.array(l_1d, dtype=float)
 
print(arr_1d_f)
print(arr_1d_f.dtype)
# [0. 1. 2.]
# float64

NumPy配列ndarrayをリストに変換: tolist()
1
2
3
4
5
6
7
8
9
10
11
import numpy as np
 
arr_1d = np.arange(3)
 
print(arr_1d)
# [0 1 2]
 
l_1d = arr_1d.tolist()
 
print(l_1d)
# [0, 1, 2]

コメント

このブログの人気の投稿

Python OpenCVとWebカメラでバーコードリーダー

VB.net Dictionaryクラスの初期化