NumPy (Numerical Python) is a powerful library for numerical computing in Python. It provides support for arrays, matrices, and many mathematical functions to operate on these data structures. This module will cover the basics of NumPy, including array creation, operations, and common functions.

Table of Contents

  1. Introduction to NumPy
  2. Installing NumPy
  3. Creating Arrays
  4. Array Operations
  5. Indexing and Slicing
  6. Array Manipulation
  7. Mathematical Functions
  8. Practical Exercises

  1. Introduction to NumPy

NumPy is the fundamental package for scientific computing with Python. It contains among other things:

  • A powerful N-dimensional array object
  • Sophisticated (broadcasting) functions
  • Tools for integrating C/C++ and Fortran code
  • Useful linear algebra, Fourier transform, and random number capabilities

  1. Installing NumPy

To install NumPy, you can use pip:

pip install numpy

  1. Creating Arrays

NumPy arrays are the main way to store data in NumPy. They are similar to Python lists but more efficient.

Example: Creating Arrays

import numpy as np

# Creating a 1D array
array_1d = np.array([1, 2, 3, 4, 5])
print("1D Array:", array_1d)

# Creating a 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
print("2D Array:\n", array_2d)

# Creating an array with zeros
zeros_array = np.zeros((3, 3))
print("Zeros Array:\n", zeros_array)

# Creating an array with ones
ones_array = np.ones((2, 4))
print("Ones Array:\n", ones_array)

# Creating an array with a range of values
range_array = np.arange(0, 10, 2)
print("Range Array:", range_array)

# Creating an array with random values
random_array = np.random.random((2, 2))
print("Random Array:\n", random_array)

Explanation:

  • np.array([1, 2, 3, 4, 5]): Creates a 1D array from a list.
  • np.array([[1, 2, 3], [4, 5, 6]]): Creates a 2D array from a list of lists.
  • np.zeros((3, 3)): Creates a 3x3 array filled with zeros.
  • np.ones((2, 4)): Creates a 2x4 array filled with ones.
  • np.arange(0, 10, 2): Creates an array with values from 0 to 10 with a step of 2.
  • np.random.random((2, 2)): Creates a 2x2 array with random values between 0 and 1.

  1. Array Operations

NumPy supports element-wise operations, matrix operations, and broadcasting.

Example: Array Operations

import numpy as np

array_a = np.array([1, 2, 3])
array_b = np.array([4, 5, 6])

# Element-wise addition
sum_array = array_a + array_b
print("Sum:", sum_array)

# Element-wise multiplication
product_array = array_a * array_b
print("Product:", product_array)

# Dot product
dot_product = np.dot(array_a, array_b)
print("Dot Product:", dot_product)

# Broadcasting
broadcast_array = array_a + 10
print("Broadcasting:", broadcast_array)

Explanation:

  • array_a + array_b: Adds corresponding elements of array_a and array_b.
  • array_a * array_b: Multiplies corresponding elements of array_a and array_b.
  • np.dot(array_a, array_b): Computes the dot product of array_a and array_b.
  • array_a + 10: Adds 10 to each element of array_a (broadcasting).

  1. Indexing and Slicing

NumPy arrays can be indexed and sliced similarly to Python lists.

Example: Indexing and Slicing

import numpy as np

array = np.array([1, 2, 3, 4, 5])

# Indexing
print("Element at index 2:", array[2])

# Slicing
print("Elements from index 1 to 3:", array[1:4])

# 2D array slicing
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("2D Array:\n", array_2d)
print("Element at row 1, column 2:", array_2d[1, 2])
print("First two rows:\n", array_2d[:2, :])

Explanation:

  • array[2]: Accesses the element at index 2.
  • array[1:4]: Slices the array from index 1 to 3 (4 is not included).
  • array_2d[1, 2]: Accesses the element at row 1, column 2 of the 2D array.
  • array_2d[:2, :]: Slices the first two rows of the 2D array.

  1. Array Manipulation

NumPy provides functions to reshape, flatten, and concatenate arrays.

Example: Array Manipulation

import numpy as np

array = np.array([[1, 2, 3], [4, 5, 6]])

# Reshape
reshaped_array = array.reshape((3, 2))
print("Reshaped Array:\n", reshaped_array)

# Flatten
flattened_array = array.flatten()
print("Flattened Array:", flattened_array)

# Concatenate
array_a = np.array([1, 2, 3])
array_b = np.array([4, 5, 6])
concatenated_array = np.concatenate((array_a, array_b))
print("Concatenated Array:", concatenated_array)

Explanation:

  • array.reshape((3, 2)): Reshapes the array to 3 rows and 2 columns.
  • array.flatten(): Flattens the 2D array into a 1D array.
  • np.concatenate((array_a, array_b)): Concatenates array_a and array_b.

  1. Mathematical Functions

NumPy provides a wide range of mathematical functions to operate on arrays.

Example: Mathematical Functions

import numpy as np

array = np.array([1, 2, 3, 4, 5])

# Sum
sum_array = np.sum(array)
print("Sum:", sum_array)

# Mean
mean_array = np.mean(array)
print("Mean:", mean_array)

# Standard Deviation
std_array = np.std(array)
print("Standard Deviation:", std_array)

# Trigonometric functions
sin_array = np.sin(array)
print("Sine:", sin_array)

Explanation:

  • np.sum(array): Computes the sum of all elements in the array.
  • np.mean(array): Computes the mean of the array.
  • np.std(array): Computes the standard deviation of the array.
  • np.sin(array): Computes the sine of each element in the array.

  1. Practical Exercises

Exercise 1: Create and Manipulate Arrays

  1. Create a 3x3 array with values ranging from 0 to 8.
  2. Reshape the array to 1x9.
  3. Compute the sum of all elements in the array.
  4. Compute the mean of the array.

Solution:

import numpy as np

# Step 1: Create a 3x3 array with values ranging from 0 to 8
array = np.arange(9).reshape((3, 3))
print("3x3 Array:\n", array)

# Step 2: Reshape the array to 1x9
reshaped_array = array.reshape((1, 9))
print("Reshaped Array:", reshaped_array)

# Step 3: Compute the sum of all elements in the array
sum_array = np.sum(reshaped_array)
print("Sum:", sum_array)

# Step 4: Compute the mean of the array
mean_array = np.mean(reshaped_array)
print("Mean:", mean_array)

Exercise 2: Array Operations

  1. Create two 1D arrays with values [1, 2, 3] and [4, 5, 6].
  2. Compute the element-wise addition and multiplication of the arrays.
  3. Compute the dot product of the arrays.

Solution:

import numpy as np

# Step 1: Create two 1D arrays
array_a = np.array([1, 2, 3])
array_b = np.array([4, 5, 6])

# Step 2: Compute the element-wise addition and multiplication
sum_array = array_a + array_b
print("Sum:", sum_array)

product_array = array_a * array_b
print("Product:", product_array)

# Step 3: Compute the dot product
dot_product = np.dot(array_a, array_b)
print("Dot Product:", dot_product)

Conclusion

In this module, we covered the basics of NumPy, including array creation, operations, indexing, slicing, manipulation, and mathematical functions. NumPy is a powerful tool for numerical computing and is widely used in data science, machine learning, and scientific computing. In the next module, we will explore Pandas for data manipulation.

Python Programming Course

Module 1: Introduction to Python

Module 2: Control Structures

Module 3: Functions and Modules

Module 4: Data Structures

Module 5: Object-Oriented Programming

Module 6: File Handling

Module 7: Error Handling and Exceptions

Module 8: Advanced Topics

Module 9: Testing and Debugging

Module 10: Web Development with Python

Module 11: Data Science with Python

Module 12: Final Project

© Copyright 2024. All rights reserved