What is more, thanks to NumPy, numerical calculations in Python can be done much quicker and with better use of memory than what is done using native Python lists. It allows the programming of complex equations without an unreasonable amount of pain to implement the equation manually.
What is NumPy?
The NumPy® stands for Numerical Python that can support fruition of an open-source library that allows the development of multi dimensional arrays along with matrices. But the most interesting fact lies in the fact that a vast range of operations can be performed using mathematical functions on the made arrays. Furthermore, it is interesting to note that, while Python's normal lists can be described as multi-purpose data containing a variety of types, a NumPy array is a single type array. This feature gives NumPy an edge on enhanced performance and reduced memory usage, this now makes it possible for scientific computation and statistical to assist in complex calculations and better prediction models.Arrays are the core object of NumPy which provides the base and foundation of the library; A good comparison of this is to use Python and its list type but now imagine a list that is much larger where stored lists can perform high-speed operations. Data manipulation also became easier, whereas before, constructs and slices were not allowed for ordinary lists. Every NumPy array can be indexed, sliced and each section can be reshaped with ease.
Creating NumPy Arrays
In order to use NumPy for the first time, it is necessary to prepend the library. After that, there are different modes in which you can create arrays. One of the most common ones is transforming any given list into any given NumPy array with the help of the np.array() function call.Let's see how to use NumPy by importing it and creating a basic array.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Creating arrays with specific values is also possible in NumPy using their many built in functions. For instance, there are configurations such as: an array of zeros, an array of ones or even an array of a certain range of numbers.
zeros_array = np.zeros((3, 4)) 3x4 Array with zeroes
ones_array = np.ones((2, 2)) 2x2 Array with ones
range_array = np.arange(0, 10, 2) Output: [0 2 4 6 8]
Array Operations
One of the reasons why NumPy has a wide usability is the fact that it allows for vectorized operations. Nowadays, most computers have a large amount of memory which allows for vectorization. In layman's terms, it is when you are able to operate on whole arrays, without needing to iterate through every element.You can, for example, carry out arithmetic directly with NumPy arrays as such:
arr1=np.array([1,2,3])
arr2=np.array([4,5,6])
sum_array = arr1 + arr2 Array adding the two arrays element wise
print(sum_array)
product_array = arr1 * arr2 Multiplication of two arrays
print(product_array)
sin_array = np.sin(arr1) Each element has a sine function applied to it
log_array = np.log(arr1) Apply the natural logarithm
Multi-dimensional Arrays
The claim that Python is a one-dimensional language is without merit as one of its salient features is the existence of multi-dimensional arrays. Such arrays have found a useful application in the analytics of data, machine learning, and the computation of scientific calculations.A 2D array can be considered to be a matrix which consists of rows and columns. You can create a 2D array as follows:
arr2D = np.array([[1, 2, 3],[4, 5, 6]])
print(arr2D)
element = arr2D[1, 2] Selecting the 2nd row, 3rd column element
print(element)
subarray = arr2D[0:1, 1:3] Copies elements of first row, 2nd to 3rd column
print(subarray)
Changing the Shape of an Array
Similarly, in NumPy, it is possible to change the shape of an array without in any way altering its data, such as its content and its order. This means that you can transform a 1D array into a 2D array and the converse as well. For example: arr1D = np.array([1, 2, 3, 4, 5, 6])
arr2D = arr1D.reshape((2, 3)) Two dimensional array of two rows and three columns
print(arr2D)
Other Significant Features of Numpy
There is a powerful feature called broadcasting in NumPy. As a result, NumPy has an inherent advantage because an operation can be done on two arrays of different sizes without the need to change the size of one of the two dimensions.Suppose we are given a 2D array and a single number, and we wish to add that single number to all elements of that array. Broadcasting lets us do this – there is no need to loop through the array.
arr2D = np.array([[1, 2], [3, 4]])
result = arr2D + 5 Add 5 to every element in the array
print(result)