Thuta Learning
AdvancedData & Databasesintermediate

Linear Algebra

Relax. We'll talk through this in plain words — no textbook voice.

NumPy gives you np.dot(), @, and np.linalg for linear algebra calculations. Matrix multiplication, inverse, determinant, solving equations, and more are all easy to compute in Python. This is foundational stuff for data science, machine learning, computer graphics, and engineering calculations.

python
import numpy as np

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

B = np.array([
    [5, 6],
    [7, 8]
])

matrix_product = A @ B
determinant = np.linalg.det(A)

print("Matrix multiplication:
", matrix_product)
print("Determinant:", determinant)

A @ B performs matrix multiplication, not element-wise multiplication. np.linalg.det(A) computes the determinant of matrix A. For matrix multiplication, A's column count has to match B's row count.

You should see
Matrix multiplication: [[19 22] [43 50]] Determinant: -2.0000000000000004

Info

If you want element-wise multiplication, use A * B. If you want matrix multiplication, use A @ B or np.dot(A, B). Don't mix the two up.

Easy traps

  • Because of floating-point arithmetic, a determinant might print as -2.0000000000000004 instead of -2.0. That's just how computers handle decimal calculations. Use round() if you need a cleaner number.

Exercise

Instead of solving a system of equations by hand, you can use np.linalg.solve() to solve it straight from matrix form.

python
import numpy as np

# Solve equations:
# 2x + y = 8
# x + 3y = 13
A = np.array([[2, 1], [1, 3]])
b = np.array([8, 13])

solution = np.linalg.solve(A, b)
print("x and y:", solution)

You'll know it worked when: x and y: [2.2 3.6]

Linear Algebra | Thuta Learning