6
10

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 3 years have passed since last update.

Numpy入門

Last updated at Posted at 2020-02-15

1. Numpy 基礎

1.1. numpy.ndarray 基礎

1.1.1. numpy.ndarray 属性要素

numpy.ndarray 属性要素 取得内容 Example 1
np.array([[1,0,0],[0,1,2]])
ndim 次元数 2
shape 配列の形 (2,3)
size 配列要素の数 6
dtype 配列要素のデータ型 int32
T 転置配列 np.array([[1 0]
[0 1]
[0 2]])
flags メモリーレイアウト
flat 一次元化(平坦化)した配列生成
配列定義例:np.array(配列変数.flat)
imag 配列要素の虚部値配列
real 配列要素の実部値配列
itemsize 配列要素の大きさ(バイト)
例:int32 -> 32/8 = 4バイト
4
nbytes 配列の大きさ(バイト) 32
strides 隣する配列要素のずれ(バイト) (12,4)
縦方向:12バイト
-> 4バイト * 3要素(横方向)
横方向:4バイト
-> 4バイト * 1要素
ctypes ctypesモジュールで使用
base 参照先の配列 None
Example_1.py
### ライブラリ定義
import numpy as np

### 関数定義
def print_attribute(input):
    print("")
    for key, value in input.items():
        print(">>> " + str(key))
        print("IN: print("+str(key)+")")
        print("OUT: "+str(value))
        print("")

### 配列定義
array = np.array( [[1,0,0],[0,1,2]])

### numpy.ndarray要素定義

attribute ={}
attribute['array.ndim'] = array.ndim
attribute['array.shape'] = array.shape
attribute['array.size'] = array.size
attribute['array.dtype'] = array.dtype
attribute['array.T'] = array.T
attribute['array.flags'] = array.flags
attribute['array.flat'] = array.flat
attribute['np.array(array.flat)'] = np.array(array.flat)
attribute['array.imag'] = array.imag
attribute['array.real'] = array.real
attribute['array.itemsize'] = array.itemsize
attribute['array.nbytes'] = array.nbytes
attribute['array.strides'] = array.strides
attribute['array.ctypes'] = array.ctypes
attribute['array.base'] = array.base

### numpy.ndarray要素取得例
print_attribute(attribute)
"""

>>> array.ndim
IN: print(array.ndim)
OUT: 2

>>> array.shape
IN: print(array.shape)
OUT: (2, 3)

>>> array.size
IN: print(array.size)
OUT: 6

>>> array.dtype
IN: print(array.dtype)
OUT: int32

>>> array.T
IN: print(array.T)
OUT: [[1 0]
 [0 1]
 [0 2]]

>>> array.flags
IN: print(array.flags)
OUT:   C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False


>>> array.flat
IN: print(array.flat)
OUT: <numpy.flatiter object at 0x000001E00DB70A00>

>>> np.array(array.flat)
IN: print(np.array(array.flat))
OUT: [1 0 0 0 1 2]

>>> array.imag
IN: print(array.imag)
OUT: [[0 0 0]
 [0 0 0]]

>>> array.real
IN: print(array.real)
OUT: [[1 0 0]
 [0 1 2]]

>>> array.itemsize
IN: print(array.itemsize)
OUT: 4

>>> array.nbytes
IN: print(array.nbytes)
OUT: 24

>>> array.strides
IN: print(array.strides)
OUT: (12, 4)

>>> array.ctypes
IN: print(array.ctypes)
OUT: <numpy.core._internal._ctypes object at 0x000001E00D84BC50>

>>> array.base
IN: print(array.base)
OUT: None

"""

参考文献

  1. Numpy公式ユーザーガイド(バージョン:1.18)
  2. Numpy公式レファレンス(numpy.ndarray)
6
10
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
6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?