{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4d9230e1",
   "metadata": {},
   "source": [
    "# Numpy CheatSheet- By Hadi Hijazi\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5abd9c28",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4978395b",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Creating Matrices with Numpy\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b783307b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Creating Matrix From Nested Lists\n",
    "M = np.array([\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6]\n",
    "  ])\n",
    "\n",
    "# Check dimensions\n",
    "print(M.shape) # (2, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fca5e06e",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{bmatrix}\n",
    "1 & 2 & 3 \\\\\n",
    "4 & 5 & 6\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "### Special Matrices Functions\n",
    "\n",
    "#### Identity Matrix\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44b2e7b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "M = np.identity(3) # 3x3 identity matrix\n",
    "print(M)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "279953c9",
   "metadata": {},
   "source": [
    "\n",
    "$$\n",
    "\\begin{bmatrix}\n",
    "1 & 0 & 0 \\\\\n",
    "0 & 1 & 0 \\\\\n",
    "0 & 0 & 1\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "#### Zeros Matrix\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01e58d12",
   "metadata": {},
   "outputs": [],
   "source": [
    "M = np.zeros((2, 3)) # 2x3 matrix of zeros\n",
    "print(M)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b631390",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{bmatrix}\n",
    "0 & 0 & 0 \\\\\n",
    "0 & 0 & 0\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "#### Ones Matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dade8645",
   "metadata": {},
   "outputs": [],
   "source": [
    "M = np.ones((2, 3)) # 2x3 matrix of ones\n",
    "print(M)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "406b6e32",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{bmatrix}\n",
    "1 & 1 & 1 \\\\\n",
    "1 & 1 & 1\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "#### Random Matrix\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d28cda0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "M = np.random.rand(2, 3) # 2x3 matrix of random numbers\n",
    "print(M)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e9abdcd",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{bmatrix}\n",
    "0.1 & 0.2 & 0.3 \\\\\n",
    "0.4 & 0.5 & 0.6\n",
    "\\end{bmatrix}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "520e8abb",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Matrix Operations\n",
    "\n",
    "Let us create two matrices $A$ and $B$:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40d53165",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([\n",
    "  [1, 2, 3],\n",
    "  [4, 5, 6],\n",
    "  [7, 8, 9]\n",
    "])\n",
    "\n",
    "B = np.array([\n",
    "  [1, 0, 2],\n",
    "  [3, 2, 4],\n",
    "  [5, 6, 0]\n",
    "])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7793c99c",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "### Matrix Addition & Substraction\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8208dd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Adding matrices\n",
    "C = A + B\n",
    "print(\"C = \", C)\n",
    "\n",
    "# Substracting matrices\n",
    "D = A - B\n",
    "print(\"D = \", D)\n",
    "\n",
    "# Broadcasting with scalars\n",
    "E = A + 5 # Adds 5 to reach element\n",
    "print(\"E = \", E)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ee8edda",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "> Matrix addition and substraction are element-wise operations that require the matrices to have the same dimensions.\n",
    "\n",
    "$$\n",
    "\\begin{bmatrix}\n",
    "1 & 2 & 3 \\\\\n",
    "4 & 5 & 6 \\\\\n",
    "7 & 8 & 9\n",
    "\\end{bmatrix}\n",
    "+ \n",
    "\\begin{bmatrix}\n",
    "1 & 0 & 2 \\\\\n",
    "3 & 2 & 4 \\\\\n",
    "5 & 6 & 0\n",
    "\\end{bmatrix}\n",
    "=\n",
    "\\begin{bmatrix}\n",
    "2 & 2 & 5 \\\\\n",
    "7 & 7 & 10 \\\\\n",
    "12 & 14 & 9\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "### Scalar & Matrix Multiplication\n",
    "\n",
    "#### Scalar Multiplication\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88481ec3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Multiplying every element by 2\n",
    "result = 2 * A\n",
    "print(\"result = \", result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1221741",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "$$\n",
    "\\begin{bmatrix}\n",
    "2 & 4 & 6 \\\\\n",
    "8 & 10 & 12\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "> Useful for scaling features or applying learning rates in ML algorithms.\n",
    "\n",
    "#### Matrix Multiplication\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41f32149",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Matrix multiplication\n",
    "result = np.dot(A, B) # Classic method\n",
    "result = A @ B # Modern syntax (Python 3.5+)\n",
    "print(\"result = \", result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efaba9fc",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "> Matrix multiplication is **not commutative**: $A@B \\neq B@A$. The inner dimensions must match: $(m * n) * (n * p) = (m * p)$.\n",
    "\n",
    "### Transpose & Inverse Operations\n",
    "\n",
    "#### Matrix Transpose\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c80a142",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Matrix Transpose\n",
    "A_transosed = A.T # or np.transpose(A)\n",
    "print(\"A_transosed = \", A_transosed)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82dd6f51",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{bmatrix}\n",
    "1 & 0 & 2 \\\\\n",
    "3 & 2 & 4 \\\\\n",
    "5 & 6 & 7\n",
    "\\end{bmatrix}\n",
    "\\rightarrow\n",
    "\\begin{bmatrix}\n",
    "1 & 4 & 77 \\\\\n",
    "2 & 5 & 8 \\\\\n",
    "3 & 6 & 9\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "Tranposing flips a matrix along its diagonal, converting rows into columns and columns into rows.\n",
    "In Machine Learning, transposes are frequently used in backpropagation when calculating the gradient of the loss function with respect to the weights of a neural network.\n",
    "\n",
    "#### Matrix Inverse\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87c79545",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compute Inverse of A (if possible)\n",
    "B_inv = np.linalg.inv(B)\n",
    "print(B_inv)\n",
    "\n",
    "# Verify A x A^(-1) = I (Identity Matrix)\n",
    "result = B @ B_inv\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f36461bf",
   "metadata": {},
   "source": [
    "A matrix is invertible only if its determinant is non-zero.\n",
    "The inverse is used for solving linear systems and certain optimization problems."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cec3be2",
   "metadata": {},
   "source": [
    "$$\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "370d5c1c",
   "metadata": {},
   "source": [
    "### Determinant & Rank\n",
    "\n",
    "#### Determinant\n",
    "\n",
    "The determinant of a square matrix is a scalar value that can be computed from the elements of a square matrix and encodes certain properties of the matrix. It is denoted as det(A) or |A|. A zero determinant indicates that the matrix is singular (non-invertible) and cannot be inverted.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "582f0265",
   "metadata": {},
   "outputs": [],
   "source": [
    "det_A = np.linalg.det(A)\n",
    "print(det_A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac958667",
   "metadata": {},
   "source": [
    "#### Matrix Rank\n",
    "\n",
    "The rank of a matrix is the maximum number of linearly independent rows or columns. It is denoted as rank(A).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa101785",
   "metadata": {},
   "outputs": [],
   "source": [
    "rank_A = np.linalg.matrix_rank(A)\n",
    "print(rank_A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1e80718",
   "metadata": {},
   "source": [
    "### Eigen Values & Eigen Vectors\n",
    "\n",
    "$$\n",
    "A \\mathbf{v} = \\lambda \\mathbf{v}\n",
    "$$\n",
    "\n",
    "Eigenvectoes ($\\mathbf{v}$) are special vectors that when transformed by matrix A, only change is scale (not direction), by a factor of the eigenvalue $(\\lambda)$.\n",
    "\n",
    "In machine learning, eigenvalues and eigenvectors are used in Principal Component Analysis (PCA) for dimensionality reduction, where eigenvectors correspond to the directions of maximum variance in the data, and eigenvalues indicate the amount of variance captured by each principal component.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b181f1d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "eigenvalues, eigenvectors = np.linalg.eig(A)\n",
    "print(\"Eigenvalues:\", eigenvalues)\n",
    "print(\"Eigenvectors:\", eigenvectors)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
