LoginSignup
2
4

More than 5 years have passed since last update.

NumPy(ナムパイまたはナンパイ)基本

Posted at

Numpy

C/C++ and Fortranで実装されているので速い
http://www.numpy.org/

NumpyのImport

import numpy as np

numpyというライブラリをnpという名前で使えるようにする

numpyの一次配列を作る

arr = np.asarray([1,2,3], dtype=np.int32)
print (arr)
[1 2 3]

np.int32とは32 ビット符号付き整数のこと。

numpyのデータ型を変更する

In [16]:arr.astype(np.float32)
Out[16]:array([ 1.,  2.,  3.], dtype=float32)
In [17]:arr
Out[17]:array([1, 2, 3], dtype=int32)

numpyの多次元配列を作る

In [28]:arr=np.asarray([["a","b","c"],["d","e","f"]])
In [29]:arr
Out[29]:array([['a', 'b', 'c'],
       ['d', 'e', 'f']],
      dtype='<U1')
In [30]:print (arr)
[['a' 'b' 'c']
 ['d' 'e' 'f']]

numpyの全ての配列をスカラー倍する

In [32]:arr=np.asarray([[1,2,3],[4,5,6]])
In [33]:3*arr
Out[33]:array([[ 3,  6,  9],
       [12, 15, 18]])

numpyの関数呼び出し

In [36]:np.sqrt(arr)
Out[36]:array([[ 1.        ,  1.41421356,  1.73205081],
       [ 2.        ,  2.23606798,  2.44948974]])
2
4
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
2
4