5. Numpy#
5.1. Introduction#
The numpy
package is essential for handling arrays and matrices in Python, making it invaluable for processing and visualizing scientific data. It offers a wide range of functions for various operations, including mathematics, logic, linear algebra, Fourier transforms, and more. In this section, you will explore commonly used numpy functions to work with multidimensional arrays.
Here is how you can import the numpy
package in python:
import numpy as np
Here is a table of what you should know after this topic:
Function |
Definition |
Example |
Array Type |
---|---|---|---|
|
Creates an array from a given sequence or iterable object. |
|
One-Dimensional |
|
Creates an array filled with zeros. |
|
Two-Dimensional |
|
Converts a given input to an array. |
|
One-Dimensional |
|
Returns the shape of an array (number of rows and columns). |
|
- |
|
Returns the minimum value in an array. |
|
- |
|
Returns the maximum value in an array. |
|
- |
|
Computes the mean (average) value of an array. |
|
- |
|
Sorts the elements of an array in ascending order. |
|
- |
|
Returns evenly spaced numbers over a specified interval. |
|
One-Dimensional |
|
Returns evenly spaced values within a given interval. |
|
One-Dimensional |
|
Returns the indices of the maximum values along a specified axis. |
|
- |
|
Returns the indices of the minimum values along a specified axis. |
|
- |
|
Returns the indices where a given condition is true. |
|
- |
|
Changes the data type of an array. |
|
- |
|
Computes the dot product of two arrays. |
|
- |
|
Transposes the rows and columns of an array. |
|
- |
|
Loads data from a text file into an array. |
|
Two-Dimensional |
|
Computes the sum of array elements. |
|
- |
|
Computes the cosine of each element in the array. |
|
- |
|
Computes the sine of each element in the array. |
|
- |
|
Computes the square root of each element in the array. |
|
- |
Conditions within arrays#
You can also use conditions to check for values within an array. For example:
arr = np.array([-1, 0, 1, 2, 3])
filtered_arr = arr[arr > 0]
print("Original Array:", arr)
print("Filtered Array (Positive Elements):", filtered_arr)
Original Array: [-1 0 1 2 3]
Filtered Array (Positive Elements): [1 2 3]
Numpy
provides a variety of functions for linear algebra operations. These functions are used to perform operations on matrices and solve systems of linear equations. Here are a few commonly used functions from np.linalg
:
Function |
Description |
---|---|
|
Computes the inverse of a square matrix. |
|
Calculates the determinant of a square matrix. |
|
Computes the eigenvalues and eigenvectors of a square matrix. |
|
Solves a system of linear equations. |
Let’s focus on the np.linalg.solve
function, which is used to solve a system of linear equations. It takes two arguments: the coefficient matrix A and the dependent variable vector b. The function solves the equation Ax = b and returns the solution vector x. To illustrate this, here is an example:
import numpy as np
# Coefficient matrix
A = np.array([[2, 3], [4, 1]])
# Dependent variable vector
b = np.array([8, 10])
# Solve the equation Ax = b
x = np.linalg.solve(A, b)
print(x)
[2.2 1.2]
Note
A: the created coefficient matrix b: the dependent variable vector x: the unknown we try to solve for from matrix equation of Ax = b
The @
operator in Python is used for matrix multiplication when working with Numpy arrays
. Here’s an example that demonstrates the usage of the @
operator for matrix multiplication:
import numpy as np
matrix1 = np.array([[1, 2],
[3, 4]])
matrix2 = np.array([[5, 6],
[7, 8]])
result = matrix1 @ matrix2
print("Matrix 1:")
print(matrix1)
print("\nMatrix 2:")
print(matrix2)
print("\nResult of Matrix Multiplication:")
print(result)
Matrix 1:
[[1 2]
[3 4]]
Matrix 2:
[[5 6]
[7 8]]
Result of Matrix Multiplication:
[[19 22]
[43 50]]