**Introduction to NumPy: Arrays and Basic Operations

In this lesson, you'll be introduced to NumPy, the cornerstone library for numerical computation in Python. You'll learn how to create and manipulate NumPy arrays, understand fundamental array operations, and discover why NumPy is essential for data science tasks.

Learning Objectives

  • Create NumPy arrays from Python lists.
  • Understand the difference between NumPy arrays and Python lists.
  • Perform basic arithmetic operations on NumPy arrays.
  • Access and modify elements within NumPy arrays using indexing and slicing.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to NumPy

NumPy (Numerical Python) is a powerful library for numerical computation in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. Why is this important? Because data science heavily relies on numerical operations, and NumPy is optimized for these tasks, making it much faster and more memory-efficient than using Python lists alone. To use NumPy, you must first import the library using import numpy as np. The as np is a standard convention to use a shorter alias, making the code more readable.

Creating NumPy Arrays

You can create NumPy arrays in several ways, most commonly from Python lists.

Example:

import numpy as np

# Creating a 1D array from a list
my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)
print(my_array)
# Output: [1 2 3 4 5]

# Creating a 2D array (matrix) from a list of lists
my_list_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
my_array_2d = np.array(my_list_2d)
print(my_array_2d)
# Output: 
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]

NumPy automatically infers the data type based on the input data. You can also specify the data type during array creation using the dtype argument, such as np.array([1, 2, 3], dtype=float).

Array Attributes

NumPy arrays have several useful attributes that provide information about the array. Some common ones include:

  • ndim: The number of dimensions (e.g., 1 for a 1D array, 2 for a 2D array).
  • shape: A tuple representing the size of the array in each dimension (e.g., (3, 2) for a 2D array with 3 rows and 2 columns).
  • dtype: The data type of the array's elements (e.g., int64, float64).

Example:

import numpy as np
my_array = np.array([[1, 2, 3], [4, 5, 6]])
print(f"Dimensions: {my_array.ndim}")  # Output: Dimensions: 2
print(f"Shape: {my_array.shape}")  # Output: Shape: (2, 3)
print(f"Data type: {my_array.dtype}")  # Output: Data type: int64

Array Operations

NumPy allows you to perform mathematical operations on arrays element-wise, meaning the operation is applied to each element individually. This is significantly more efficient than using loops with Python lists.

Example:

import numpy as np

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

# Element-wise addition
addition_result = array1 + array2
print(f"Addition: {addition_result}")  # Output: Addition: [5 7 9]

# Element-wise subtraction
subtraction_result = array2 - array1
print(f"Subtraction: {subtraction_result}") # Output: Subtraction: [3 3 3]

# Element-wise multiplication
multiplication_result = array1 * array2
print(f"Multiplication: {multiplication_result}") # Output: Multiplication: [ 4 10 18]

# Element-wise division
division_result = array2 / array1
print(f"Division: {division_result}") # Output: Division: [4.  2.5 2. ]

NumPy also provides mathematical functions like np.sin(), np.cos(), np.sqrt() that can be applied to entire arrays.

Indexing and Slicing

You can access individual elements or portions of NumPy arrays using indexing and slicing, similar to Python lists, but with extensions for multi-dimensional arrays.

Example:

import numpy as np
my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Accessing an element (row 1, column 2 - remember 0-based indexing)
element = my_array[1, 2]
print(f"Element at [1, 2]: {element}")  # Output: Element at [1, 2]: 6

# Slicing a row
row_slice = my_array[1, :]
print(f"Row slice: {row_slice}")  # Output: Row slice: [4 5 6]

# Slicing a column
column_slice = my_array[:, 1]
print(f"Column slice: {column_slice}")  # Output: Column slice: [2 5 8]

# Slicing a sub-array
sub_array = my_array[0:2, 1:3] # rows 0 and 1, columns 1 and 2
print(f"Sub-array: {sub_array}") # Output: Sub-array: [[2 3],[5 6]]
Progress
0%