Linear Algebra Basics

Python · NumPy Library

Theory

NumPy handles vectors and matrices directly. The dot product np.dot(a, b) multiplies matching elements and adds them up: prices [10, 20] and quantities [3, 2] give 70. The @ operator does matrix multiplication: a (2, 3) matrix @ a (3, 4) matrix gives (2, 4), and the inner sizes must match. Note that * is element-wise and is not matrix multiplication.

np.linalg holds the classic tools: np.linalg.inv(A) is the inverse, np.linalg.det(A) the determinant and np.linalg.solve(A, b) solves the system A x = b. For example, if 2 pens and 3 notebooks cost 85 rupees and 4 pens and 1 notebook cost 65, solve gives a pen at 11 and a notebook at 21. solve is faster and more accurate than inv(A) @ b, and raises LinAlgError for a singular matrix.

Why we use it

Linear regression, neural networks, PCA and recommendation systems are all matrix maths underneath. Knowing dot products and matrix multiplication helps you read model code and compute weighted totals in one step.

When to use it

Use @ or np.dot for weighted sums and matrix products, np.linalg.solve for systems of equations and inv only when you really need the inverse. For element-by-element products use * instead.

How it works

  1. Vectors or matrices
  2. Element-by-element product wanted? Yes: Use a * b. No: Use a @ b or np.dot.
  3. Inner dimensions match? Yes: Result has shape (rows of A, cols of B). No: ValueError: shapes not aligned.
  4. Solving A x = b? Yes: np.linalg.solve(A, b). No: Use the product directly.
  5. Numeric result

Syntax

np.dot(a, b)   a @ b
np.linalg.inv(A)  np.linalg.det(A)
np.linalg.solve(A, b)
A.T

Example

A = np.array([[2, 3], [4, 1]])
b = np.array([85, 65])
print(np.linalg.solve(A, b))   # [11. 21.]

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic