Numpy の ndarray の使い方

モジュールのインポート

import numpy as np

Numpy の多次元配列 ndarray

a = np.array([3,5,7,3]) # 1次元配列をnumpyのアレーに変換
a
array([3, 5, 7, 3])
np.zeros(5) # 0の1次元配列
array([0., 0., 0., 0., 0.])
np.ones(5) # 1の1次元配列
array([1., 1., 1., 1., 1.])
a>4
array([False,  True,  True, False])
上のような配列をマスクといい,マスクを使うと次のように非常に便利である.
a[a>=5]
array([5, 7])

ndarray のスライシング(部分配列)ほか

a1 = np.array([2,4,6,8,10]) # リストをnumpyの配列(ndarray)に変換
a1
array([ 2,  4,  6,  8, 10])
a1[1]
4
a1[1:]
array([ 4,  6,  8, 10])
a1[:-1]
array([2, 4, 6, 8])
a1[1:]+a1[:-1]
array([ 6, 10, 14, 18])
a1>5
array([False, False,  True,  True,  True])
a1[a1>5]
array([ 6,  8, 10])
a1%4
array([2, 0, 2, 0, 2])
a1%4==0
array([False,  True, False,  True, False])

関数の例:np.sum

n=101
a = np.arange(0,n,1)
a
array([  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
        13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
        26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
        39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,
        52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
        65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,
        78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
        91,  92,  93,  94,  95,  96,  97,  98,  99, 100])
np.sum(a)
5050
n*(n-1)//2
5050
a[(a>50) & (a<60)]
array([51, 52, 53, 54, 55, 56, 57, 58, 59])
np.sum(a[(a>50) & (a<60)])
495

乱数,平均,分散など

np.random.random(6) # 一様乱数
array([0.32815296, 0.8492352 , 0.45114558, 0.04260362, 0.61720102,
       0.52884076])
np.random.randn(100) # 正規分布(平均=0,rms=1)
array([ 0.77084079, -0.73228818,  0.84784692, -0.41812462,  0.55224526,
       -0.86892605,  1.43365659, -0.52159852,  0.39236404, -0.35963961,
       -0.23363778, -0.4576865 ,  0.34601616, -0.62347543,  0.00839344,
        0.2428087 ,  1.13459758, -1.32369591, -0.96430401,  1.32534369,
        0.35079732,  1.01842708,  0.06337092, -0.92765291,  1.51062173,
       -0.46039522, -0.37753899,  0.15917892,  1.35162604, -0.772902  ,
       -0.43990742, -0.84403826, -0.26273828,  1.46878074, -0.30946205,
       -0.23111594,  0.44609049, -0.2590333 ,  0.68570139,  1.09809373,
        0.69187471,  0.68690459, -1.43540188,  0.19544236, -0.62716717,
        1.50992199,  0.4526521 , -1.52252587, -1.05895923,  0.99289562,
       -0.62722245, -0.24461926, -1.19521911, -0.38332472,  0.09104275,
        0.5512664 ,  1.09970072,  0.69132974,  0.87380598,  1.41638599,
       -1.04116653,  0.51492855,  0.83088157,  1.14043296,  0.10982346,
        0.85882435,  1.09687318, -0.10746052,  1.12711473, -0.62029973,
       -0.48379085, -1.46939708, -2.61344797, -0.57381966,  0.38639922,
        0.43450337, -0.8095239 ,  0.57061461, -1.33395683,  1.33284553,
       -0.26036775,  0.92835557, -0.35372333,  1.1996495 ,  0.33054811,
       -0.28747841,  0.38739494, -0.51953496, -0.66733731, -0.26193475,
        0.03057587,  1.71527574,  0.20733853, -1.5418961 ,  0.02898901,
       -0.79900814,  0.75427377,  0.33021807, -0.72160684, -1.08151834])
a6 = np.random.randn(20)*2 + 10
a6
array([10.65055351, 10.55369232,  8.9736806 ,  7.49063768, 11.8002802 ,
       12.24012598, 12.62607301,  9.76911224, 12.13178051, 10.78527221,
        8.75613188,  9.31907998, 11.0219058 ,  5.17098625, 12.14126596,
       11.53360589,  7.11516453,  9.38947374, 11.61800258,  8.53644932])
np.mean(a6) # 平均
10.081163709832001
np.std(a6) # 標準偏差
1.93758329398173