본문 바로가기

Python

Python_N-004. 10 basic attributes in array

N-004_10_basic_attribute_in_array.py
0.00MB

 

no. Attribute Description example code
1 ndim The number of dimensions
of the array.
import numpy as np
arr = np.array(  [ [1, 2, 3], [4, 5, 6] ]  )   
print(arr.ndim)  # Output: 2
2 shape The shape
of the array (dimensions).
print(arr.shape)  # Output: (2, 3)
3 size The total number of elements
in the array.
print(arr.size)  # Output: 6
4 dtype The data type of the array’s elements. print(arr.dtype)  # Output: int64
5 itemsize The size (in bytes) of each element
in the array.
print(arr.itemsize)  # Output: 8
6 nbytes The total number of bytes consumed
by the elements of the array.
print(arr.nbytes)  # Output: 48
7 T The transposed array. print(arr.T)  # Output: array([[1, 4], [2, 5], [3, 6]])
print( transpose(arr) )  # same
8 flat A flat iterator over the array. for item in arr.flat:
    print(item)  # Output: 1 2 3 4 5 6
9 real The real part of the array elements
(useful for complex numbers).
arr_complex = np.array([1+2j, 3+4j])
print(arr_complex.real)  # Output: [1. 3.]
10 imag The imaginary part of the array elements
(useful for complex numbers)
print(arr_complex.imag)  # Output: [2. 4.]