NumPy Scientific Computation in Python

In this part of Learning Python we Cover Numpy Scientific Computation in python
Written by Paayi Tech |24-Apr-2019 | 0 Comments | 264 Views

Introduction:

Before Python, the most scientific computation was done in MATLAB. However, after the python came into mainstream and modules are being developed by open source communities, the community started to think of doing the scientific computation in python. Matplotlib along with SciPy and NumPy are combined to do scientific computation.This computation module helps developer a lot because MATLAB programming language cannot be used as a product or software on a large scale.

So now we know that NumPy is a module for scientific calculation. It makes a lot easier to use and manipulate arrays in a much faster way than usual programming can do. The data type the NumPy used is called ndarray. The ndarray can be of a single dimension, 2 dimensions or multi-dimension.

 

Why NumPy?

So now the question arises why NumPy?

NumPy is fast because it is built in C language which makes it very fast. Moreover, the comprehensive approach behind the functionality makes it very easy. The data type that is used behind functionality is only a floating number, so there is no time consumption of checking data type. There is a range of functionality in NumPy which we will discuss in this section.

 

What NumPy can do and where it is used?

NumPy is used extensively in the field of data science and machine learning. It can be used whether for creating data frames in pandas to manipulate or make changes in the image as in case of OpenCV, subjecting the n-dimensional features to the machine learning algorithm, subjecting the batch of images to the convolutional neural network, etc.

We can perform all the mathematical functions with the help of NumPy like multiplying the matrices, creating the matrices, getting the dot product of any two matrices. It reduces the time complexity when the data is vast like the array of 100000x100. It would take much longer time in a conventional way of doing such operations.

 

Installation:

Installation of NumPy is straightforward. We can do it by using the pip command as follows:

 

Linux or Mac user

pip3 install numpy

Windows User

pip install numpy

 

 

Figure 1

In the above Figure, as you can see, the module requirement is already satisfied. In your case, the installation will start after running the command.

 

Getting started with NumPy array:

So first we will see how to initialize the NumPy array. Unlike the previous ways of initializing the array that we have been taught, by iterating the loop and appending the value, in NumPy to initialize an array there are few ways like

  • arange: So, what the arange function does, it creates a ndarray of element starting from zero to n. The array generated in such response is a single dimensional array. It is similar to the python list range function, but it is fast as compare to range function.
  • Zeros: Zeros method create a ndarray of user-defined rows and column, and all the elements are assigned the value of zero.
  • Ones: Ones method create a ndarray of user-defined rows and columns, and all the elements are given the value of 1. In this method, we can also specify the data type of value like integer, float, etc.
  • Empty: Similar to ones and zero methods empty method initializes the value as used defined the rows and column, but the values are not initialized instead given some arbitrary values in negative.

These are the methods; now we will see how to implement these functions through programming:

import numpy as np
 
print (" Initializing by arange function ")
print (np.arange(21))
 
print (" Initializing by zeros function ")
print (np.zeros((5,5)))
 
print (" Initializing by ones function ")
print (np.ones((5,5)))
 
print (" Initializing by ones function ")
print (np.ones((5,5), dtype=np.int16))
 
print (" Initializing by ones function of multidimensions ")
print (np.ones((3,3,3), dtype=np.int16))
 
print (" Initializing by empty function ")
print (np.empty((3,3)))

 

The output of the above program is as follows:

Output

 

Initializing by arange function

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]

 

Initializing by zeros function

[[0. 0. 0. 0. 0.]

 [0. 0. 0. 0. 0.]

 [0. 0. 0. 0. 0.]

 [0. 0. 0. 0. 0.]

 [0. 0. 0. 0. 0.]]

 

 Initializing by ones function

[[1. 1. 1. 1. 1.]

 [1. 1. 1. 1. 1.]

 [1. 1. 1. 1. 1.]

 [1. 1. 1. 1. 1.]

 [1. 1. 1. 1. 1.]]

 

 Initializing by ones function

[[1 1 1 1 1]

 [1 1 1 1 1]

 [1 1 1 1 1]

 [1 1 1 1 1]

 [1 1 1 1 1]]

 

 Initializing by ones function of multidimensions

[[[1 1 1]

  [1 1 1]

  [1 1 1]]

 

 [[1 1 1]

  [1 1 1]

  [1 1 1]]

 

 [[1 1 1]

  [1 1 1]

  [1 1 1]]]

 

 Initializing by empty function

[[6.90474343e-310 6.90474343e-310 6.90473979e-310]

 [6.90472482e-310 6.90472482e-310 6.90473561e-310]

 [6.90474044e-310 6.90472482e-310 6.90474423e-310]]

The screen shot of the above program is as follows:

Getting started with NumPy

 

Figure 2

 

  1. So in the first output, we initialize the range of value starting from 0 to 21 just like we do in list range functional
  2. In the second output, we initialize the numpy array of size 5x5 with zeros.
  3. In the third output, we print 5x5 size of numpy arrays of one and also change the data type to integer. It is to be noted that NumPy by default use of float value.
  4. In the fourth output, we printed the 3-dimensional NumPy array. We can create n-dimensional arrays easily and fast in NumPy modules.
  5. In the last output, we initialize the 3x3 size of NumPy array with non-values.

So, we can see the main objective of NumPy is the homogenous multidimensional arrays. We can create as many dimensions as we want, in case of NumPy dimension is often referred to as an axis.

So, this is the brief introduction of NumPy. In the next section we will learn more about NumPy and how we can rearrange and make different mathematical operation with the help of NumPy.

 

 





Login/Sign Up

Comments




Related Posts



© Copyright 2019, All Rights Reserved. paayi.com

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.