<目次>
(1) Pythonでランダムな座標データを生成する方法
(1-1) 使う構文
(1-2) サンプルプログラム
(1-3) 補足:データの座標を全体的にシフトしたい場合
(1) Pythonでランダムな座標データを生成する方法
(1-1) 使う構文
●構文①:numpy.random.RandomState
rng = np.random.RandomState(seed)
●構文②:numpy.random.RandomState.randn(d0,d1,…,dn)
x1 = rng.randn(d0,d1,…,dn)
●補足:randnは数学的にはどんなデータなの?
↓
(1-2) サンプルプログラム
import numpy as np def main(): rng = np.random.RandomState(123) # 2次元=d0,d1を指定 x1 = rng.randn(5,2) print(x1) if __name__ == "__main__": main()
[[-1.0856306 0.99734545] [ 0.2829785 -1.50629471] [-0.57860025 1.65143654] [-2.42667924 -0.42891263] [ 1.26593626 -0.8667404 ] [-0.67888615 -0.09470897] [ 1.49138963 -0.638902 ] [-0.44398196 -0.43435128] [ 2.20593008 2.18678609] [ 1.0040539 0.3861864 ]]
import numpy as np def main(): rng = np.random.RandomState(123) # 3次元=d0,d1,d2を指定 x1 = rng.randn(10,2,2) print(x1) if __name__ == "__main__": main()
[[[-1.0856306 0.99734545] [ 0.2829785 -1.50629471]] [[-0.57860025 1.65143654] [-2.42667924 -0.42891263]] [[ 1.26593626 -0.8667404 ] [-0.67888615 -0.09470897]] [[ 1.49138963 -0.638902 ] [-0.44398196 -0.43435128]] [[ 2.20593008 2.18678609] [ 1.0040539 0.3861864 ]] [[ 0.73736858 1.49073203] [-0.93583387 1.17582904]] [[-1.25388067 -0.6377515 ] [ 0.9071052 -1.4286807 ]] [[-0.14006872 -0.8617549 ] [-0.25561937 -2.79858911]] [[-1.7715331 -0.69987723] [ 0.92746243 -0.17363568]] [[ 0.00284592 0.68822271] [-0.87953634 0.28362732]]]
(図122)
(1-3) 補足:データの座標を全体的にシフトしたい場合
import numpy as np def main(): rng = np.random.RandomState(123) x1 = rng.randn(10,2) + np.array([5,5]) print(x1) if __name__ == "__main__": main()
[[3.9143694 5.99734545] [5.2829785 3.49370529] [4.42139975 6.65143654] [2.57332076 4.57108737] [6.26593626 4.1332596 ] [4.32111385 4.90529103] [6.49138963 4.361098 ] [4.55601804 4.56564872] [7.20593008 7.18678609] [6.0040539 5.3861864 ]]