このページは、nAGライブラリのJupyterノートブックExampleの日本語翻訳版です。オリジナルのノートブックはインタラクティブに操作することができます。
nAG Pythonライブラリをテストするためのノートブック
ランダムな相関行列の生成 (g05py)
import numpy as np
import naginterfaces.library.rand as nirand
=10
n54321)
np.random.seed(= np.random.rand(n)
d = np.zeros((n,n)) c
# dの合計が要素の数と同じになるようにする
= d*n/d.sum()
d sum() d.
10.000000000000002
= [1]
seed = nirand.init_repeat(1,seed) statecomm
= nirand.matrix_corr(d,statecomm) c
print (np.matrix(c))
[[ 1. 0.12564145 -0.00764626 -0.16539236 -0.0057206 0.33249834
-0.30321203 -0.21418952 0.00680034 -0.05270024]
[ 0.12564145 1. -0.22944284 -0.30689584 0.01636082 0.15489516
0.09513636 -0.11511604 0.02053952 -0.02445098]
[-0.00764626 -0.22944284 1. -0.05145711 -0.0028276 -0.22348789
0.11902808 0.22075883 -0.07590371 0.1018837 ]
[-0.16539236 -0.30689584 -0.05145711 1. 0.10975949 0.16784829
0.29459949 -0.03324149 -0.16856802 0.28284157]
[-0.0057206 0.01636082 -0.0028276 0.10975949 1. 0.45541538
-0.00205822 -0.18870226 0.07874764 0.0127316 ]
[ 0.33249834 0.15489516 -0.22348789 0.16784829 0.45541538 1.
0.00766639 0.20634005 -0.02867739 -0.09530013]
[-0.30321203 0.09513636 0.11902808 0.29459949 -0.00205822 0.00766639
1. -0.15300155 0.20595416 -0.32157421]
[-0.21418952 -0.11511604 0.22075883 -0.03324149 -0.18870226 0.20634005
-0.15300155 1. 0.10960632 0.23585012]
[ 0.00680034 0.02053952 -0.07590371 -0.16856802 0.07874764 -0.02867739
0.20595416 0.10960632 1. 0.07595573]
[-0.05270024 -0.02445098 0.1018837 0.28284157 0.0127316 -0.09530013
-0.32157421 0.23585012 0.07595573 1. ]]
同じことをpandasのデータフレームで試してみてください
import pandas as pd
=10
n= pd.DataFrame(np.random.rand(n)) df
df.head()
0 | |
---|---|
0 | 0.538427 |
1 | 0.041469 |
2 | 0.808094 |
3 | 0.995254 |
4 | 0.638710 |
= df*n/df.sum() df
sum() df.
0 10.0
dtype: float64
0] df[
0 0.844449
1 0.065039
2 1.267385
3 1.560919
4 1.001729
5 1.483451
6 1.155444
7 1.289283
8 0.179535
9 1.152767
Name: 0, dtype: float64
= [1]
seed_pd = nirand.init_repeat(1,seed_pd) statecomm_pd
= pd.DataFrame(np.random.rand(n,n))
c_df c_df.head()
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 0.821482 | 0.073884 | 0.207861 | 0.805677 | 0.914479 | 0.148888 | 0.958101 | 0.371736 | 0.718589 | 0.103931 |
1 | 0.876924 | 0.583506 | 0.681451 | 0.886152 | 0.178985 | 0.196202 | 0.460703 | 0.816532 | 0.632180 | 0.645103 |
2 | 0.455040 | 0.562814 | 0.670125 | 0.645266 | 0.046710 | 0.261600 | 0.008384 | 0.857218 | 0.030585 | 0.843959 |
3 | 0.491000 | 0.808222 | 0.102852 | 0.874662 | 0.424479 | 0.800346 | 0.233507 | 0.413949 | 0.062330 | 0.927309 |
4 | 0.693016 | 0.810947 | 0.485409 | 0.034001 | 0.104252 | 0.130955 | 0.872597 | 0.593396 | 0.978339 | 0.372679 |
= nirand.matrix_corr(df[0],statecomm_pd) c_df
c_df
array([[ 1. , -0.29729941, -0.21300892, 0.03478882, -0.09595445,
-0.1224626 , -0.1393347 , 0.1912117 , -0.10030998, 0.15494009],
[-0.29729941, 1. , -0.08760441, 0.00529587, -0.10077574,
-0.31210086, -0.26633473, 0.13655237, 0.07305169, -0.17211715],
[-0.21300892, -0.08760441, 1. , 0.10080064, 0.19363926,
-0.12299742, 0.03189871, 0.38336042, -0.07689659, 0.19927168],
[ 0.03478882, 0.00529587, 0.10080064, 1. , -0.04295422,
-0.03200566, 0.00818092, -0.1439598 , -0.02215293, -0.09256416],
[-0.09595445, -0.10077574, 0.19363926, -0.04295422, 1. ,
-0.08986962, -0.26003982, -0.0011058 , 0.07202205, -0.31143519],
[-0.1224626 , -0.31210086, -0.12299742, -0.03200566, -0.08986962,
1. , -0.15524479, 0.21512623, 0.05717375, -0.0764278 ],
[-0.1393347 , -0.26633473, 0.03189871, 0.00818092, -0.26003982,
-0.15524479, 1. , -0.05409596, 0.15665846, -0.14315115],
[ 0.1912117 , 0.13655237, 0.38336042, -0.1439598 , -0.0011058 ,
0.21512623, -0.05409596, 1. , -0.17834947, -0.14585691],
[-0.10030998, 0.07305169, -0.07689659, -0.02215293, 0.07202205,
0.05717375, 0.15665846, -0.17834947, 1. , 0.15725037],
[ 0.15494009, -0.17211715, 0.19927168, -0.09256416, -0.31143519,
-0.0764278 , -0.14315115, -0.14585691, 0.15725037, 1. ]])
= pd.DataFrame(c_df) gdf
gdf.head()
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1.000000 | -0.297299 | -0.213009 | 0.034789 | -0.095954 | -0.122463 | -0.139335 | 0.191212 | -0.100310 | 0.154940 |
1 | -0.297299 | 1.000000 | -0.087604 | 0.005296 | -0.100776 | -0.312101 | -0.266335 | 0.136552 | 0.073052 | -0.172117 |
2 | -0.213009 | -0.087604 | 1.000000 | 0.100801 | 0.193639 | -0.122997 | 0.031899 | 0.383360 | -0.076897 | 0.199272 |
3 | 0.034789 | 0.005296 | 0.100801 | 1.000000 | -0.042954 | -0.032006 | 0.008181 | -0.143960 | -0.022153 | -0.092564 |
4 | -0.095954 | -0.100776 | 0.193639 | -0.042954 | 1.000000 | -0.089870 | -0.260040 | -0.001106 | 0.072022 | -0.311435 |