A blog for computer science passionates.

Monday, 25 November 2019

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

Hello Friends!!!
In this article, we are going to learn more methods of NumPy module. At the end of this article, you will be able to work on,
  • ones
  • zeros
So, let's start with these methods, these two methods are powerful as it is generally used to build feature vector with some default values while developing deep learning models.
  • numpy.ones() - method returns an array with default value 1.0 with given shape or we can say newly returned array filled with 1.0 or 1 based on data type and shape.
    • Syntax: numpy.ones(shape, dtype=None, order='C')
    • Here, shape - is the shape of an array that you want to retrieve.
    • dtype - is an optional argument, represents the data type of an array that you want to retrieve, the default data type is float.
    • order{'C','F'} - is an optional argument, represents whether to store multi - dim array in C-style or Fortran style. C-style represents row-major and Fortran-style represents column-major in memory. 
  • It returns ndarray object in the form of an array with the given shape, order and data type.
Let's move to another method.
  • numpy.zeros() - method returns an array with default value 0.0, with given shape or we can say newly returned array filled with 0.0 or 0 based on data type and shape.
    • Syntax: numpy.zeros(shape,dtype=float,order='C') 
    • Here, shape - is the shape of an array that you want to retrieve.
    • dtype - is an optional argument, represents the data type of an array that you want to retrieve, the default data type is float.
    • order{'C','F'} - is an optional argument, represents whether to store multi - dim array in C-style or Fortran style. C-style represents row-major and Fortran-style represents column-major in memory. 
  • It returns ndarray object in the form of an array with the given shape, order and data type.
Let's take an example of the above methods:

#Ones Demo
import numpy as np
ar=np.ones((2,3))
print(ar)
#Output
#[[1. 1. 1.]
# [1. 1. 1.]]
ar=np.ones((2,3),dtype='int8')
print(ar)
#Output
#[[1 1 1]
# [1 1 1]]
ar=np.ones((2),dtype='int16')
print(ar) #Output: [1 1]
#Zeros Demo
ar=np.zeros(5)
print(ar) #Output: [0. 0. 0. 0. 0.]
ar=np.zeros([2,4])
print(ar)
#Output
#[[0. 0. 0. 0.]
# [0. 0. 0. 0.]]
ar=np.zeros([2,4],dtype='int8')
print(ar)
#Output
#[[0 0 0 0]
# [0 0 0 0]]

No comments:

Post a Comment