23
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BitStarAdvent Calendar 2017

Day 1

NumPy 入門

Last updated at Posted at 2017-11-30

NumPy 入門

Quickstart tutorial

NumPy のメインオブジェクトは homogeneous な配列である. homogeneous な配列とは, ひとつの配列のなかに複数のデータタイプを持たないという意味で均質な配列である, ということである.

NumPy の array は ndarray とよばれる.

ndarray の基本

# import
import numpy as np
arr = [[1, 2], [3, 4]]
# ndarray を作る
a = np.array(arr)
a
array([[1, 2],
       [3, 4]])
# 次元数
a.ndim
2
# 行と列を tuple で返す
a.shape
(2, 2)
# 要素数
a.size
4
# array のデータ型
a.dtype
dtype('int64')
a.data
<read-write buffer for 0x104a04a80, size 32, offset 0 at 0x104a10b30>

numpy.array

numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)

ndarray を作るとき, dtype プロパティにデータ型を指定することができる.
データ型は以下を参照.

Data types

a = np.array([1, 3], dtype=float)
a.dtype
dtype('float64')

numpy.zeros, numpy.ones

numpy.zeros(shape, dtype=float, order='C')

shape を引数とし, すべての要素を 0 (ones の場合は 1) とするような関数が存在する.

np.zeros([2,2], dtype=int)
array([[0, 0],
       [0, 0]])
np.ones([2,2], dtype=int)

array([[1, 1],
       [1, 1]])
np.empty([2,2])
array([[  0.00000000e+000,   0.00000000e+000],
       [  1.48219694e-323,   0.00000000e+000]])

numpy.identity

numpy.identity(n, dtype=None)

単位行列を作る

np.identity(4)
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

numpy.arange

numpy.arange([start, ]stop, [step, ]dtype=None)

連番や等差数列を作る

np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(1, 11)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
np.arange(5, 100, 5)
array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85,
       90, 95])

numpy.linspace

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

等間隔な数列を作る

np.linspace(0, 1)
array([ 0.        ,  0.02040816,  0.04081633,  0.06122449,  0.08163265,
        0.10204082,  0.12244898,  0.14285714,  0.16326531,  0.18367347,
        0.20408163,  0.2244898 ,  0.24489796,  0.26530612,  0.28571429,
        0.30612245,  0.32653061,  0.34693878,  0.36734694,  0.3877551 ,
        0.40816327,  0.42857143,  0.44897959,  0.46938776,  0.48979592,
        0.51020408,  0.53061224,  0.55102041,  0.57142857,  0.59183673,
        0.6122449 ,  0.63265306,  0.65306122,  0.67346939,  0.69387755,
        0.71428571,  0.73469388,  0.75510204,  0.7755102 ,  0.79591837,
        0.81632653,  0.83673469,  0.85714286,  0.87755102,  0.89795918,
        0.91836735,  0.93877551,  0.95918367,  0.97959184,  1.        ])
np.linspace(1, 50)
array([  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.])
np.linspace(10, 50, 5)
array([ 10.,  20.,  30.,  40.,  50.])

numpy.reshape

numpy.reshape(a, newshape, order='C')

array (ndarray) の shape を変換する.

a = np.array([[1, 2], [3, 4], [5, 6]])
print(a.shape)
a
(3, 2)





array([[1, 2],
       [3, 4],
       [5, 6]])
a.reshape(-1,)
array([1, 2, 3, 4, 5, 6])
a.reshape([2, 3])
array([[1, 2, 3],
       [4, 5, 6]])

ndarray の参照

a = np.array([[1, 2, 3], [4, 5, 6]])

print(a[0])
print(a[0][0])
# 行, 列の直接指定
print(a[0, 0])
# スライス
print(a[1:2])
print(a[1:])
print(a[:1])
[1 2 3]
1
1
[[4 5 6]]
[[4 5 6]]
[[1 2 3]]
# True, False による配列の指定 (True である箇所が表示される)
b = a > 3
print(b)
print(a[b])
[[False False False]
 [ True  True  True]]
[4 5 6]

numpy.sort

numpy.sort(a, axis=-1, kind='quicksort', order=None)

配列がソートされる

a = np.random.random(10)
a
array([ 0.30059093,  0.39776416,  0.03569461,  0.32814799,  0.00305144,
        0.33298982,  0.37045668,  0.35102445,  0.7594396 ,  0.35020994])
# 昇順
np.sort(a)
array([ 0.00305144,  0.03569461,  0.30059093,  0.32814799,  0.33298982,
        0.35020994,  0.35102445,  0.37045668,  0.39776416,  0.7594396 ])
# 降順
np.sort(a)[::-1]
array([ 0.7594396 ,  0.39776416,  0.37045668,  0.35102445,  0.35020994,
        0.33298982,  0.32814799,  0.30059093,  0.03569461,  0.00305144])

ndarray の演算

Universal Function とよばれる.

numpy.add

numpy.add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'add'>

行列の足し算

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.add(a, b)
array([5, 7, 9])
a + b
array([5, 7, 9])

numpy.subtract

numpy.subtract(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

行列の引き算

np.subtract(b, a)
array([3, 3, 3])
b - a
array([3, 3, 3])

numpy.multiply

numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

行列の掛け算

np.multiply(a, b)
array([ 4, 10, 18])
a * b
array([ 4, 10, 18])

numpy.divide

numpy.divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

行列の割り算

a = np.array([1, 2, 3])
b = np.array([0.1, 0.2, 0.3])
np.divide(a, b)
array([ 10.,  10.,  10.])
a / b
array([ 10.,  10.,  10.])

その他の Universal Function (ufunc)

Universal functions

ndarray の要約関数

a = np.array([1, 2, 3])
# 合計
a.sum()
6
# 平均
a.mean()
2.0
# 最大値
a.max()
3
# 最小値
a.min()
1
# 分散
a.var()
0.66666666666666663
23
30
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
23
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?