A Comprehensive Numpy Tutorial for Python Enthusiasts
Numpy, short for Numerical Python, is a fundamental library for scientific computing in Python. It provides support for large, multi-dimensional array
Why Numpy?
NumPy is short for "Numerical Python," and it is a powerful library in Python used for numerical and mathematical operations. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these elements. NumPy is widely used in scientific computing, data analysis, and machine learning due to its efficiency and ease of use.
In Python, lists are a versatile and commonly used data structure for storing ordered collections of items. However, when it comes to storage and manipulation efficiency, particularly for numerical operations, the built-in list type may not be the most optimal choice. This is where arrays come into play
Numpy Arrays:
Numpy arrays are homogeneous, meaning all elements must be of the same data type. This homogeneity allows for more efficient storage and computation.
Numpy primarily deals with multi-dimensional arrays, the
ndarray
objects. These arrays can have any number of dimensions, providing a versatile structure for representing data.
Array Axes Explained
In a 2-dimensional NumPy array, the axes are the directions along the rows and columns.
Creating Arrays:
You can create Numpy arrays using various methods, such as np.array()
, np.zeros()
, np.ones()
, and np.arange()
. Understand the syntax and usage of each function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
zeros_arr = np.zeros((3, 3))
ones_arr = np.ones((2, 4))
range_arr = np.arange(0, 10, 2)
Array Operations:
- Indexing and Slicing
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Accessing element "2"
element = arr[0, 1]
# Slicing rows and columns "[4 5 6] [3 6]"
row = arr[1, :]
col = arr[:, 2]
Array Shape and Reshaping:
arr = np.array([[1, 2, 3], [4, 5, 6]]) # Get array shape --> (2,3) shape = arr.shape # Reshape array #[[1 2] # [3 4] # [5 6]] reshaped_arr = arr.reshape((3, 2))
Array Reshaping:
np.reshape(arrayname, newshape, order='C')
Where arrayname is the name of the array that is to be reshaped,newshape is the intended shape of the given array by making use of NumPy reshape and order is the index order using which the elements of the array can be read and placed into the reshaped array represented by new shape.
Statistical and Mathematical Operations:
Numpy Mean
NumPy mean calculates the mean of the values within a NumPy array (or an array-like object).(Refer axis from shape of array section)
arr = np.array([1, 2, 3, 4, 5])
# Mean, median, and standard deviation
mean_val = np.mean(arr)
median_val = np.median(arr)
std_dev = np.std(arr)
# Trigonometric functions
sin_arr = np.sin(arr)
Numpy Median Function:
The NumPy median function computes the median of the values in a NumPy array. Note that the NumPy median function will also operate on “array-like objects” like Python lists.
np.median function can get a little more complicated. It can operate on 2-dimensional or multi-dimensional array objects. It can also calculate the median value of each row or column.
import numpy as np
np_array_1d = np.array([0,20,40,60,80,100])
print(np.median(np_array_1d))
#for 2 dimensional array
np_array_2x3 = np.array([[0,2,4],[1,3,5]])
print(np.median(np_array_2x3))
Numpy Sum:
Essentially, the NumPy sum function sums up the elements of an array. It just takes the elements within a NumPy array (an ndarray
object) and adds them together.
It’s possible to also add up the rows or add up the columns of an array. This will produce a new array object (instead of producing a scalar sum of the elements).
import numpy as np
np_array_1d = np.array([0,2,4,6,8,10])
print(np.sum(np_array_1d))
#output --> 30
#sum of 2x3 array
np_array_2x3 = np.array([[0,2,4],[1,3,5]])
print(np.sum(np_array_2x3))
Array sum in 2x3 array (Sum down the rows)
import numpy as np
np_array_2x3 = np.array([[0,2,4],[1,3,5]])
print(np.sum(np_array_2x3, axis = 0))
Array Sum (Across the columns)
import numpy as np
np_array_2x3 = np.array([[0,2,4],[1,3,5]])
print(np.sum(np_array_2x3, axis = 1))
Numpy Dot:
import numpy as np
#If both input are 1 dimensional array
print(np.dot(2,3))
#if both are 2d arrays
np_array_2x3_1 = np.array([[0,2,4],[1,3,5]])
np_array_2x3_2 = np.array([[0,2,4],[1,3,5]])
print(np.dot(A_array_2d, B_array_2d))
Numpy Zeros:
The NumPy zeros function enables you to create NumPy arrays that contain only zeros.
Importantly, this function enables you to specify the exact dimensions of the array. It also enables you to specify the exact data type.
import numpy as np
#datatype by default is float
print(np.zeros(3, dtype = int)) #array of integers of data type integers
#creating arrays of zeros of specific shape
print(np.zeros(shape = (2, 3)))
Similary, Array of ones and Fives can also be made:
import numpy as np
print(np.ones(5))
#array of fives
print(np.full(5,6)) #fill 5 - 6 times
Numpy Max:
The numpy.max()
function computes the maximum value of the numeric values contained in a NumPy array. It can also compute the maximum value of the rows, columns, or other axes.
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print(np.max(np_array_2d))
# CALCULATE COLUMN MAXIMA
print(np.max(arr, axis = 0)
#output array([8, 3, 6])
#calculate ROW MAXIMA
print(np.max(np_array_2d, axis = 1))
These are some fundamental aspects of NumPy arrays. As you delve deeper, you'll discover more advanced features and capabilities that make NumPy a powerful tool for scientific computing and data manipulation in Python.