Introduction to NumPy
Understanding NumPy Arrays and
Operations with Examples
What is NumPy?
• - Open-source Python library for numerical
computing.
• - Provides fast mathematical operations.
• - Supports multi-dimensional arrays (ndarray).
• - Efficient and optimized for scientific
computation.
Why Use NumPy?
• - Faster than Python lists for numerical operations.
• - Provides vectorized operations and broadcasting.
• - Optimized memory handling and mathematical functions.
• - Used in Machine Learning, Data Science, and AI.
Numpy
• Python library used for working with arrays.
• Has functions for linear algebra, fourier transform, and matrices.
• Created in 2005 by Travis Oliphant.
• Open source project
• Free to use
• Stands for Numerical Python.
Why Numpy?
• In Python, lists serve the purpose of arrays
• But they are slow to process.
• NumPy provide an array object that is 50x faster.
• Array object in NumPy is called ndarray
• It provides a lot of functions that make working with ndarray very easy.
• Arrays frequently used in data science, where speed and resources are
very important.
Import numpy
import numpy
arr =
numpy.array([1, 2, 3, 4, 5])
print(arr)
Aliasing
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Create a NumPy ndarray Object
• NumPy ndarray object by using the array() function.
• arr = np.array([1, 2, 3, 4, 5])
• arr = np.array((1, 2, 3, 4, 5)) #1D array
• arr = np.array([[1, 2, 3], [4, 5, 6]])#2D array
• arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
#3D array
Creating NumPy Arrays
• ```python
• import numpy as np
• # From Python List
• arr1 = np.array([1, 2, 3])
• # Zeros and Ones
• arr2 = np.zeros((2,3))
• arr3 = np.ones((3,4))
• # Creating Sequences
• arr4 = np.arange(0, 10, 2)
• arr5 = np.linspace(0, 5, 10)
• ```
Accessing and Modifying Arrays
• ```python
• # Indexing and Slicing
• arr = np.array([10, 20, 30, 40, 50])
• print(arr[1]) # Access element
• print(arr[1:4]) # Slice array
• # Modifying elements
• arr[2] = 100
• print(arr)
• ```
NumPy Array Attributes
• ```python
• arr = np.array([[1, 2, 3], [4, 5, 6]])
• print(arr.shape) # Dimensions
• print(arr.ndim) # Number of axes
• print(arr.size) # Total elements
• print(arr.dtype) # Data type
• ```
Mathematical Operations in NumPy
• ```python
• arr1 = np.array([1, 2, 3])
• arr2 = np.array([4, 5, 6])
• print(arr1 + arr2) # Element-wise addition
• print(arr1 * arr2) # Element-wise multiplication
• print(np.dot(arr1, arr2)) # Dot product
• ```
Reshaping and Transposing Arrays
• ```python
• arr = np.arange(6).reshape(2,3)
• print(arr)
• print(arr.T) # Transpose
• ```
Random Number Generation in NumPy
• ```python
• import numpy as np
• np.random.seed(42)
• random_arr = np.random.rand(3, 3) # 3x3
matrix with random values
• print(random_arr)
• ```
Summary and Questions
• - NumPy is efficient for numerical computations.
• - Supports multi-dimensional arrays with vectorized operations.
• - Mathematical functions and random number generation included.
• - Any Questions?