base | Base object, if memory is from other object |
dtype | type of data. Integer, float, Python object, etc |
data | buffer object showing start of the data |
flags | Details about Memory Layout |
itemsize | Size in bytes |
ndim | Number of dimensions or axis |
nbytes | Total bytes used by all the elements |
shape | rows and columns ,tuple , length indicates numbrer of axes, product of elements is same as size |
size | Total number of elements |
strides | Returns a tuple with bytes consumed by each dimension |
import numpy as my_np
my_array = my_np.array([0,1,2])
print(my_array)
Output
[0 1 2]
Let us try above attributes to know details of the array.
import numpy as my_np
my_array = my_np.array([ 0, 1, 2])
print(my_array.base) # None
print(my_array.dtype) # int32
print(my_array.data) # <memory at 0x0000021772F6F108>
print(my_array.flags) # C_CONTIGUOUS : True
# F_CONTIGUOUS : False
# OWNDATA : True
# WRITEABLE : True
# ALIGNED : True
# WRITEBACKIFCOPY : False
# UPDATEIFCOPY : False
print(my_array.itemsize) # 4
print(my_array.ndim) # 1
print(my_array.nbytes) # 12
print(my_array.shape) # (3,)
print(my_array.size) # 3
print(my_array.strides) # (4,)
import numpy as my_np
my_array = my_np.array([[0, 1, 2],[3, 4, 5],[6, 7, 8]])
print(my_array[1][2]) # output is 5
import numpy as my_np
my_array = my_np.array([[0, 1, 2],[3, 4, 5],[6, 7, 8]])
print(my_array.base) # None
print(my_array.dtype) # int32
print(my_array.data) # <memory at 0x0000021772EC28B8>
print(my_array.flags) # C_CONTIGUOUS : True
# F_CONTIGUOUS : True
# OWNDATA : True
# WRITEABLE : True
# ALIGNED : True
# WRITEBACKIFCOPY : False
# UPDATEIFCOPY : False
print(my_array.itemsize) # 4
print(my_array.ndim) # 2
print(my_array.nbytes) # 36
print(my_array.shape) # (3,3)
print(my_array.size) # 9
print(my_array.strides) # (12,4)
import numpy as my_np
#x=complex(98,97)
my_array = my_np.array([[[ 0, 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]]])
print(my_array[2][2][1]) # 25
Let us try above attributes to know details of the array.
import numpy as my_np
my_array = my_np.array([[[ 0, 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]]])
print(my_array.base) # None
print(my_array.dtype) # int32
print(my_array.data) # <memory at 0x00000217728F4E58>
print(my_array.flags) # C_CONTIGUOUS : True
# F_CONTIGUOUS : False
# OWNDATA : True
# WRITEABLE : True
# ALIGNED : True
# WRITEBACKIFCOPY : False
# UPDATEIFCOPY : False
print(my_array.itemsize) # 4
print(my_array.ndim) # 3
print(my_array.nbytes) # 108
print(my_array.shape) # (3,3,3)
print(my_array.size) # 27
print(my_array.strides) # (36,12,4)
Read more on shapeimport numpy as np
npr = np.array([ 0, 1, 2],ndmin=3) # adding dimension to array
print(npr) # [[[0 1 2]]] # print array
print(npr.ndim) # 3 ( display dimension of the array )
Numpy
random.randint , Random Integers 26-02-2024 | |
Very well written, visuals have really helped. Great job, thanks! |