LoginSignup
0
3

More than 5 years have passed since last update.

NumPyに入門 その1

Last updated at Posted at 2019-03-27

NumPyとは

Python用の行列計算ライブラリです。
大量のデータを効率的に計算できるような機能が充実している。
ディープラーニングをするにはほぼ必須のライブラリです。
Windows / macOS / Linux 同じように使えます。


NumPyの使い方

pip install numpy / pip3 install numpy
python3
>>> import numpy as np

以降 np.* で使用します。


データの初期化

1次元の場合

>>> a = np.array([0, 1, 2])
>>> a
array([0, 1, 2])

2次元の場合

>>> b = np.array(([0, 1, 2] , [3, 4, 5]))
>>> b
array([[0, 1, 2],
       [3, 4, 5]])

データ同士の加減乗除

>>> a = np.array(([5, 3, 8]))
>>> b = np.array(([2, 1, 3]))

>>> a + b
array([ 7,  4, 11])
>>> a - b
array([3, 2, 5])
>>> a * b
array([10,  3, 24])
>>> a / b
array([2.5  , 3.  , 2.66666667])

初期値

標準分布に従う乱数を生成

>>> np.random.randn()
0.6971069597047708
>>> np.random.randn(2, 5)
array([[ 0.06724173,  0.81443565, -1.40231797, -1.03814878,  1.52165979],
       [-0.53434462, -0.08328142,  0.20409356,  2.44858899, -1.92150623]])

スカラへの変換

>>> c = np.random.randn(2, 5)
>>> c
array([[ 1.26717607, -0.53133061,  0.3109047 ,  0.12309104, -0.53160679],
       [ 0.93251521,  0.79879362,  1.80839388,  0.05681128,  0.3855707 ]])
>>> c.sum()
4.620319089229713


動画で説明を作りました。よかったらご覧ください。
https://youtu.be/TTx2E_6MQwE

0
3
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
0
3