NumPy生成全0行列,全1行列,ランダム行列,平均数,分散を求める方法

5448 ワード

In [19]: Z = np.zeros(10)                                                       

In [20]: Z                                                                      
Out[20]: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

In [21]: Z = np.zeros((10, 10))                                                 

In [22]: Z                                                                      
Out[22]: 
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])

In [23]: O = np.ones((10, 10))                                                  

In [24]: O                                                                      
Out[24]: 
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])

In [25]: R = np.random.random((10, 10))                                         

In [26]: R                                                                      
Out[26]: 
array([[0.19030367, 0.11061756, 0.66043274, 0.82270992, 0.86288627,
        0.73516432, 0.13385655, 0.00503817, 0.56665816, 0.25389674],
       [0.2897241 , 0.63861602, 0.72253788, 0.93986596, 0.15625199,
        0.63017126, 0.5003463 , 0.15863999, 0.58883923, 0.52337149],
       [0.72983719, 0.65553847, 0.6840876 , 0.287123  , 0.60315891,
        0.0209261 , 0.08419834, 0.74994483, 0.51497606, 0.38176057],
       [0.85862599, 0.87270897, 0.89383138, 0.48394818, 0.37811392,
        0.52072555, 0.63172092, 0.77801888, 0.60513243, 0.2459972 ],
       [0.83363106, 0.78122808, 0.59937547, 0.32271631, 0.2186535 ,
        0.68846675, 0.64467556, 0.66150354, 0.77511819, 0.30240223],
       [0.27676871, 0.41084595, 0.00794113, 0.45920282, 0.80515498,
        0.57526071, 0.56819186, 0.06612172, 0.92467369, 0.36471206],
       [0.41946954, 0.16159165, 0.68769474, 0.22532472, 0.52197982,
        0.49693437, 0.37108051, 0.60618145, 0.93917826, 0.80515922],
       [0.76189594, 0.06783411, 0.68737973, 0.52845467, 0.54538306,
        0.923922  , 0.40114351, 0.53929616, 0.2268697 , 0.90800343],
       [0.8385178 , 0.44538915, 0.84338426, 0.50872464, 0.36665953,
        0.05338008, 0.19676065, 0.24176836, 0.33197874, 0.82874875],
       [0.11035295, 0.14753642, 0.32537747, 0.89676955, 0.49697756,
        0.47156815, 0.49980517, 0.74236363, 0.37142998, 0.720032  ]])

In [27]: G = np.random.rand((10, 10))                                           
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1 G = np.random.rand((10, 10))

mtrand.pyx in mtrand.RandomState.rand()

mtrand.pyx in mtrand.RandomState.random_sample()

mtrand.pyx in mtrand.cont0_array()

TypeError: 'tuple' object cannot be interpreted as an integer

In [28]: G = np.random.rand(10, 10)                                             

In [29]: G                                                                      
Out[29]: 
array([[0.56403257, 0.83244469, 0.40786028, 0.87510904, 0.28979795,
        0.85373717, 0.22971783, 0.91748492, 0.59644171, 0.22596483],
       [0.5356771 , 0.32860374, 0.52780662, 0.45942596, 0.711057  ,
        0.88122348, 0.28165273, 0.80198265, 0.79417296, 0.34449948],
       [0.79948364, 0.48259782, 0.24138731, 0.93903261, 0.85768324,
        0.67484601, 0.34147604, 0.61237275, 0.78041496, 0.79874341],
       [0.59309698, 0.96080025, 0.60859   , 0.16338954, 0.11260613,
        0.75901486, 0.93156206, 0.85028601, 0.42449479, 0.21489974],
       [0.30057841, 0.44063444, 0.97201988, 0.82144723, 0.98394706,
        0.56034609, 0.52291201, 0.19475874, 0.07134214, 0.04036237],
       [0.47856247, 0.6961275 , 0.47105025, 0.52691155, 0.70072232,
        0.16515405, 0.92592033, 0.56134931, 0.61291941, 0.28965213],
       [0.24156968, 0.25037489, 0.42165716, 0.67268851, 0.86370549,
        0.17427679, 0.65278806, 0.68228767, 0.19584982, 0.55713113],
       [0.04489099, 0.68107002, 0.42957282, 0.80947637, 0.10821918,
        0.92451679, 0.06331635, 0.6509719 , 0.83978771, 0.33431554],
       [0.39827019, 0.50991561, 0.96772609, 0.92826535, 0.4593551 ,
        0.90264111, 0.97470487, 0.31576824, 0.0953885 , 0.44522264],
       [0.41188263, 0.21477884, 0.16530341, 0.15481517, 0.88389246,
        0.34319296, 0.47860245, 0.91240803, 0.99871849, 0.70139331]])

In [30]: G.mean()                                                               
Out[30]: 0.5480290092345271

In [31]: G.var()                                                                
Out[31]: 0.07751730818313896