NumPy Operations For Beginners 1

Introduction

The Python programming language was not clearly developed for technical computing, but it gradually got the attention of the scientific and engineering societies. As a result, the Python programming language became a hub for mathematical and logical computation, and a high computational library was needed. Prior to the implementation of NumPy, the Python List was used to create a collection of variables that can be manipulated for analysis, but it did not provide an incredible variety of quick and efficient ways of generating arrays and manipulating numerical data within them. That's because only one Python list can consist of different data types, making mathematical computations on an array with homogeneous data types difficult.

NumPy is an abbreviation for Numerical Python. It was developed by Travis Oliphant in the year, 2005. NumPy is a Python library designed to make logical computation easy for python users and can be used in solving problems related to linear algebra, Fourier transform, and matrices. It is therefore referred to as the "Core Library for Scientific Computation" in the python programming language.

image.png

Though the python library, NumPy is written partially in the python programming language; C++ and C is the main programing language used in NumPy development and that is why it executes very fast. NumPy arrays are significantly quicker and smaller than Python lists. An array uses only little memory and is easier to use. NumPy stores data in far less memory and includes a mechanism for specifying data types. This allows for even more optimization of the code.

This article is centered on the NumPy array, operations, and application in data manipulation and analysis. Without further delay, let's get started.

Numpy Array

A tuple of positive integers, booleans, another array, can be used to index an array. The number of dimensions is modeled by the array's rank. The array's shape is a tuple of real numbers indicating the array's size across each dimension.

image.png

The first step in every Numpy operation is the Import Numpy as np command. The import NumPy line of code instructs Python to import the NumPy library into your coding environment. The environment may be Jupyter Notebook or other numerous Virtual environments. The "as" np section of the code instructs Python to ascribe NumPy to the keyword "np". This enables you to use NumPy functions by typing np. function instead of NumPy.function.

import numpy as np

This command practically imports NumPy to the environment else the Numpy package was not originally installed in your environment. If so, run the following code on your environment:

! pip install numpy

That is a great start. Now, let's get started with the different NumPy operations, from simple to more complex ones.

np.array([])

This command creates a NumPy array in python. The array can either be simple or nested for two or more higher dimensional data

arr = np.array([2,4,5,6,7,]) 
print(arr)

or

arr = ([[2, 4 ,5 6], [0, 8, 4, 0], [7, 6, 3, 2], [1, 4, 0, 3]])
print(arr)

np.zeros()

This command output a Numpy array of zeros.

 np.zeros(4)

array([0., 0., 0., 0.]

The "4" simply means: to output a numpy array of four zeros

np.ones()

This command is similar to np.zeros but on the contrary, outputs an array of ones

np.ones(4)

array ([1., 1., 1., 1.])

np.empty()

You have the ability to create an empty array! The function empty generates an array whose initial content is unexpected and dependent on the memory's state. Because it is faster, the empty function can be used instead of zero. After that, simply fill in every element!

np.empty(4)

This command returns a new array with four random values of the given shape and type.

np.arange()

np.arange() is a numerical range-based array structure routine. It creates an ndarray instance with regularly spaced values and appends a reference to it. The range function is quite more complicated than the four previous functions. Take a look at the code below:

np.arange(4)

array([0, 1, 2, 3])

The code above instructs python to output the numbers from zero to four. This function removes the last digit from the array. Look at another example.

np.arange(10)

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Also, this function can be given a command to output a number between a start value (not necessarily zero), and a stop.

np.arange(2, 10)

array([2, 3, 4, 5, 6, 7, 8, 9])

In addition to these, the arange function can be given a command to output a number between a start value (not necessarily zero), and a stop value with the necessary step side value.

np.arange(2, 10, 2)

array([2, 4, 6, 8])

The code above instructs python to an array of numbers between 2 and 10 with a step side of 2 ## np.linspace() This function generates an array with variables spaced linearly in a specified interval. When using this function, you must first specify a starting and ending point for an interval, preceded by the total number of breakpoints you want within a certain interval (including the start and end points).

np.linspace(0, 10, num=5)

array([ 0. ,  2.5,  5. ,  7.5, 10. ])

np.sort([])

image.png

The np.sort function is used to sort array elements ascendingly. so if you have a scattered array of elements, the sort function returns values arranged from the least to the highest.

np.sort([4,6,7,2,9,0,1,7,5,8,])

array([0, 1, 2, 4, 5, 6, 7, 7, 8, 9])

The np.sort function can not only be used to sort a single array but can be employed in complicated operations. For example, it can be used to sort z arrays:

a = np.array([[5, 0],[4,1]])
np.sort(a)                # sort along the last axis

array([[0, 5],
       [1, 4]])

It can also be used to sort along an axis.

np.sort(a, axis=0)        # this code sort along the first axis(0)

array([[5, 4],
       [0, 1]])

Conclusion

Numpy as you have seen is the seat of mathematical operations and can be used to make once python list complex data manipulation easy. With NumPy, you can perform lots of computations in a limited time. In part 2 of this article, we will dive deep into complex data manipulation and get our hands busy with exercises.