numpy.ndarray attribute element | Acquisition contents | Example 1 np.array([[1,0,0],[0,1,2]]) |
---|---|---|
ndim | Number of dimensions | 2 |
shape | Array shape | (2,3) |
size | Number of array elements | 6 |
dtype | Array element data type | int32 |
T | Transpose array | np.array([[1 0] [0 1] [0 2]]) |
flags | Memory layout | |
flat | One-dimensional (flattened) array generation Array definition example: np.array(Array variables.flat) |
|
imag | Imaginary value array of array elements | |
real | Real part array of array elements | |
itemsize | Array element size (bytes) Example: int32-> 32/8 =4 bytes |
4 |
nbytes | Array size(Part-Time Job) | 32 |
strides | Misalignment of adjacent array elements (bytes) | (12,4) Vertical: 12 bytes ->4 bytes3 elements(Lateral direction) Lateral direction:4バイト ->4 bytes1 element |
ctypes | Used in ctypes module | |
base | Referenced array | None |
Example_1.py
### Library definition
import numpy as np
### Function definition
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 definition
array = np.array( [[1,0,0],[0,1,2]])
### numpy.ndarray element definition
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 element acquisition example
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
"""
Recommended Posts