15  Numpy

15.1 NumPy

NumPy (Numerical Python) is a fundamental package for numerical computing in Python. It provides support for multi-dimensional arrays, mathematical functions, linear algebra operations, and random number generation, making it an essential tool for scientific computing, data analysis, and machine learning.

  • NumPy is a powerful, efficient, and versatile library that serves as the backbone of data analysis, machine learning, and scientific computing in Python. Its ability to perform fast array computations, mathematical operations, and linear algebra functions makes it a must-learn for data science professionals.

15.1.1 Key Features of NumPy

1.  Efficient Array Handling: Supports ndarray, a powerful multi-dimensional array object that is more efficient than Python lists.
2.  Vectorized Operations: Eliminates the need for explicit loops by applying operations element-wise.
3.  Broadcasting: Allows arithmetic operations on arrays of different shapes without explicit looping.
4.  Mathematical Functions: Provides a wide range of functions for algebra, statistics, trigonometry, and more.
5.  Random Number Generation: Generates pseudo-random numbers for simulations and machine learning.
6.  Integration with Other Libraries: Works seamlessly with pandas, matplotlib, scikit-learn, and TensorFlow.

15.1.2 Installing NumPy

To install NumPy, use: pip install numpy Or, if using Anaconda: conda install numpy

15.1.3 Array Creation

  • np.array([1, 2, 3]): Create a NumPy array from a list or tuple.
  • np.arange(10): Create an array with a range of numbers.
Code
import numpy as np
# Create a NumPy array from a list or tuple:
np.array([1, 2, 3])
array([1, 2, 3])
Code
# Create an array with a range of numbers:
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

15.1.4 Array Manipulation

  • np.concatenate((a1, a2), axis=0): Join a sequence of arrays along an existing axis.
  • np.split(array, indices_or_sections): Split an array into multiple sub-arrays.
Code
a1 = np.array([1, 2, 3,])
a2 = np.array([4, 5, 6])
a3 = np.concatenate((a1, a2), axis=0)
a3
array([1, 2, 3, 4, 5, 6])
Code
np.split(a3, [2,4])
[array([1, 2]), array([3, 4]), array([5, 6])]

15.1.5 Mathematical Operations

  • np.add(a, b), np.subtract(a, b), np.multiply(a, b), np.divide(a, b): Perform element-wise addition, subtraction, multiplication, and division.
  • np.sqrt(a): Square root of each element in the array.
  • np.exp(a): Calculate the exponential of all elements in the array.
  • np.log(a): Natural logarithm of each element in the array.
  • np.power(a, b): Elements of a raised to the powers from b, element-wise.
Code
Python
a = np.array([2, 4, 6,])
b = np.array([4, 8, 12])

np.add(a, b)  
array([ 6, 12, 18])
Code
np.subtract(a, b)  
array([-2, -4, -6])
Code
np.multiply(a, b)  
array([ 8, 32, 72])
Code
np.divide(a, b)  
array([0.5, 0.5, 0.5])
Code
np.sqrt(a)
array([1.41421356, 2.        , 2.44948974])
Code
np.exp(a)
array([  7.3890561 ,  54.59815003, 403.42879349])
Code
np.log(a)
array([0.69314718, 1.38629436, 1.79175947])
Code
np.power(a, b)
array([        16,      65536, 2176782336])

15.1.6 Statistical Functions

  • np.mean(a): Compute the arithmetic mean along the specified axis.
  • np.median(a): Compute the median along the specified axis.
  • np.std(a): Compute the standard deviation along the specified axis.
  • np.var(a): Compute the variance along the specified axis.
  • np.min(a), np.max(a): Find the minimum or maximum values.
  • np.argmin(a), np.argmax(a): Find the indices of the minimum or maximum values.
Code
Python
np.mean(a)
4.0
Code
np.median(a)
4.0
Code
np.std(a)
1.632993161855452
Code
np.var(a)
2.6666666666666665
Code
np.min(a)  
2
Code
np.max(a)  
6