A blog for computer science passionates.

Tuesday, 22 October 2019

How to start ML & DL? NumPy Module Part - 2.2

Hello Friends!!!
In this article, we are going to discuss NumPy objects. In the previous article, we learned about NumPy and how to install NumPy? Now, Let's start with the NumPy module.

The main objective of the NumPy module is a homogeneous multidimensional array. All elements are of the same type, array indexed by a tuple of non - negative integer. In NumPy dimensions are called axes.

There is a class ndarray in NumPy. It works as an alias array.

The NumPy array is not the same as Python array, as python array only works with one - dimensional array.

Let's start with some attributes of ndarray object.

ndarray.ndim - it represents the number of axes of an array (here, axes is the dimension of array).
ndarray.shape - it also represents the dimension of array, but it shows tuple of integers which indicate size of different dimensions. For example (rows, cols) here, rows mean the first dimension of array and cols mean the second dimension of the array. But shape and ndim both are different attribute shape represents tuple and the length of tuple is represented by axes i.e. ndim.
ndarray.size - it represents the total number of elements of array.
ndarray.dtype - it represents the data type of elements in the array.
ndarray.itemsize - it represents the size in bytes of each element of an array.
ndarray.data - it represents the buffer containing actual elements of an array. we need not to use this attribute.

Example:

import numpy as np
# Define the one dimensional array by using array()
ar=np.array([1,2,3,4,5,6,7,8,9])
print(ar.ndim) # 1
print(ar.shape) # (9,)
print(ar.size) # 9
print(ar.dtype) # 'int32'
print(ar.dtype.name) # 'int32'
print(ar.itemsize) # 4
print(ar.data) # <memory at 0x00000238214AD288>
print(type(ar)) # <class 'numpy.ndarray'>
print(ar.data[1]) # 2 in this output we define an index 1 so it shows 2 on index 1.
view raw NumPy_Attrib.py hosted with ❤ by GitHub

So, These are some important attributes in NumPy.

In the next article, we will learn about different methods of NumPy. All the best....👍

No comments:

Post a Comment