LoginSignup
16
25

More than 5 years have passed since last update.

Numpyチートシート

Last updated at Posted at 2018-05-19

無料で読めるデータサイエンス本の第二章が Numpy に充てられている。学習メモ。

Python Data Science Handbook by Jake VanderPlas
2. Introduction to NumPy

無から配列を作る

  • python のリストから生成 np.array([1, 4, 2, 5, 3]),
    • dtypeを指定 np.array([1, 2, 3, 4], dtype='float32')
  • ゼロ埋め、一埋め、任意の数値で埋め
    • np.zeros(10, dtype=int), np.ones((3, 5), dtype=float), np.full((3, 5), 3.14)
  • レンジ np.arange(0, 20, 2)
    • レンジ+補間 np.linspace(0, 1, 5)
  • ランダム
    • [0,1)な乱数 np.random.random((3, 3))
    • 整数 np.random.randint(0, 10, (3, 3))
    • 正規分布 np.random.normal(0, 1, (3, 3))
  • 行列
    • 単位行列 = eye np.eye(3)
  • 相関のある組
mean = [0, 0]
cov = [[1, 2],
       [2, 5]]
rand.multivariate_normal(mean, cov, 100)

配列アクセス

  • 要素の指定 x[0], x[1, 2]
  • スライス
    • 先頭 N個 x[:5], N個めから x[5:], N個とばし x[::2]
  • リバース x[::-1]
    • 先頭からN個目までをリバース x[3::-1]
  • 行列関連
    • N行目 x[3,:], N列目 x[:, 3]
    • 最初のN行 x[:5]
  • 配列に配列番号の配列でアクセス
    • 該当要素のみ何かする
x = np.arange(10)
i = np.array([2, 1, 8, 4])
x[i] -= 10

その他操作

  • コピー x.copy()
  • reshape, x.reshape((3, 3))
  • 結合 np.concatenate([x, y, z])
    • 上から行列をくっつける np.vstack([x, M])
    • 横から行列をくっつける np.hstack([M, y])
  • 分離
    • 場所を指定 np.split(x, [3, 5])
    • np.hsplit, np.vsplitという類似関数

基本的な計算

  • 四則演算 x + 5, x -5,x * 2, x / 2,x // 2, x ** 2, x % 4
    • 例: -(0.5*x + 1) ** 2
    • 正式名称の方がいいことも、np.add, np.multiply, ...
  • 三角関数 np.sin(x), np.cos(x), np.tan(x)
    • 定数 np.pi も使えるよ
  • 指数対数 np.log(x), np.exp(x), np.power(2, x)
    • こんなのも np.log2(x), np.log10(x), np.exp2(x)
  • 統計関数を自作する前に scipy.special を見よう
    • ガンマ、ベータ、エラーバーなどいろいろある
  • オペレータで遊ぶ
    • 変数に出力 np.multiply(x, 10, out=y)
    • reducer 化
      • np.add.reduce(x)
      • とはいえ、np.sum, np.prod があるのを忘れずに

集約関数系

  • np.min, np.max
    • 行列に対しても使える
    • 列ごとに実施 M.min(axis=0), 行ごとに実施 M.max(axis=1)
  • NaN-safe な版も覚えておくとよい
    • np.nansum, np.nanmean, np.nanstd などなど
  • カウント
    • np.count_nonzero(x < 6)

比較演算子とマスキング

  • < などの演算子は(True/Falseでできた)配列を生む x > 2
    • こんな感じでも行ける (2 * x) == (x ** 2)
  • それを配列要素に入れるとフィルタリングの動作になる
    • 3より小さい要素のみを抽出 x[x < 3]
  • 比較演算を利用した集約演算(重要)
    • カウント: np.count_nonzero(x < 6)
    • 合計 np.sum(x < 6), np.sum(x < 6, axis=1)
    • 存在確認 np.any(x > 8)
    • 全件確認 np.all(x == 6)
  • bitwise な計算(and or ...)
    • np.sum((inches > 0.5) & (inches < 1))

ソート関連

  • インプレースなソートnp.sort
  • インデックスをソートしてくれる np.argsort
  • 最初のN要素のみソートしてくれる np.argpartition

データタイプ一覧

bool_       Boolean (True or False) stored as a byte
int_        Default integer type (same as C long; normally either int64 or int32)
intc        Identical to C int (normally int32 or int64)
intp        Integer used for indexing (same as C ssize_t; normally either int32 or int64)
int8        Byte (-128 to 127)
int16       Integer (-32768 to 32767)
int32       Integer (-2147483648 to 2147483647)
int64       Integer (-9223372036854775808 to 9223372036854775807)
uint8       Unsigned integer (0 to 255)
uint16      Unsigned integer (0 to 65535)
uint32      Unsigned integer (0 to 4294967295)
uint64      Unsigned integer (0 to 18446744073709551615)
float_      Shorthand for float64.
float16     Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32     Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64     Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_    Shorthand for complex128.
complex64   Complex number, represented by two 32-bit floats
complex128  Complex number, represented by two 64-bit floats

よく使う集約関数一覧

NaN-safe 版も併記

np.sum          np.nansum        Compute sum of elements
np.prod         np.nanprod       Compute product of elements
np.mean         np.nanmean       Compute mean of elements
np.std          np.nanstd        Compute standard deviation
np.var          np.nanvar        Compute variance
np.min          np.nanmin        Find minimum value
np.max          np.nanmax        Find maximum value
np.argmin       np.nanargmin     Find index of minimum value
np.argmax       np.nanargmax     Find index of maximum value
np.median       np.nanmedian     Compute median of elements
np.percentile   np.nanpercentile Compute rank-based statistics of elements
np.any          N/A              Evaluate whether any elements are true
np.all          N/A              Evaluate whether all elements are true
16
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
25