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

NumPy配列ndarrayとPython標準のリストを相互に変換より
リストをNumPy配列ndarrayに変換: numpy.array()
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()
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カメラでバーコードリーダー

OpenCV 画像の足し算

OpenCV3とPython3で形状のある物体の輪郭と方向を認識する(主成分分析:PCA、固有ベクトル)