This document details my learnings on how to use the NumPy library for numerical computing in Python. It covers array creation, manipulation, and various mathematical operations. The document provides detailed explanations and code examples to illustrate the concepts and techniques learned. (”numpy is for mathematical purposes ”)
To use NumPy, import the library using the alias np.
import numpy as np
Convert a Python list to a NumPy array using np.array()
.
my_list = [1, 2, 3]
np.array(my_list)
Convert a list of lists to a 2D array (matrix).
my_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
x = np.array(my_matrix)
Use .shape
to get the dimensions of an array.
x.shape # returns (3, 3) for a 3x3 matrix