{ "cells": [ { "cell_type": "markdown", "id": "58525ac6", "metadata": {}, "source": [ "### Machine Learning (Background): Inner products\n", "$\\mathbf{u}\\cdot\\mathbf{v}=\\sum_{i=1}^n u_i v_i \\,\\rightarrow \\lVert \\mathbf{u} \\rVert_{canonical}=\\sqrt{\\mathbf{u}\\cdot\\mathbf{u}}$
\n", "$_F=tr(A^HB) \\, \\rightarrow\\lVert A \\rVert_F=\\sqrt{_F}$\n", "###### by Hamed Shah-Hosseini\n", "Explanation at: https://www.pinterest.com/HamedShahHosseini/Machine-Learning/Background-Knowledge\n", "
Explanation in Persian: https://www.instagram.com/words.persian\n", "
Code that: https://github.com/ostad-ai/Machine-Learning" ] }, { "cell_type": "code", "execution_count": 98, "id": "ddd68127", "metadata": {}, "outputs": [], "source": [ "# importing the required module\n", "# درون‌بَری سنجانه نیازداشته\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 99, "id": "b12e168b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "vector 1: [2 1 1 3]\n", "vector 2: [1 2 4 3]\n", "dot product: 17\n", "1-norm of vector 1: 7.0\n", "1-norm of vector 2 10.0\n", "2-norm of vector 1: 3.872983346207417\n", "2-norm of vector 2 5.477225575051661\n", "canonical norm of vector 1: 3.872983346207417\n", "canonical norm of vector 2 5.477225575051661\n" ] } ], "source": [ "# dot product and p-norms, example\n", "# فرآورد خجک و پ-هنجارها: نمونه\n", "vec1=np.random.randint(1,5,4)\n", "vec2=np.random.randint(1,5,4)\n", "print('vector 1:',vec1)\n", "print('vector 2:',vec2)\n", "print('dot product: ',np.dot(vec1,vec2))\n", "print('1-norm of vector 1:',np.linalg.norm(vec1,ord=1))\n", "print('1-norm of vector 2',np.linalg.norm(vec2,ord=1))\n", "print('2-norm of vector 1:',np.linalg.norm(vec1,ord=2))\n", "print('2-norm of vector 2',np.linalg.norm(vec2,ord=2))\n", "print('canonical norm of vector 1:',np.sqrt(np.dot(vec1,vec1)))\n", "print('canonical norm of vector 2',np.sqrt(np.dot(vec2,vec2)))" ] }, { "cell_type": "code", "execution_count": 102, "id": "1a0b718b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The matrix:\n", " [[3 2 2]\n", " [3 1 4]\n", " [1 3 2]]\n", "---------------------\n", "Eigenvalues of matrix: [ 6.92434399 1.21506597 -2.13940996]\n", "Trace of matrix: 6\n", "sum of eigenvalues: 5.999999999999998\n", "-------------\n", "Determinant of matrix: -18.000000000000004\n", "Product of eigenvalues: -18.0\n", "------------------\n", "Frobenius norm of matrix: 7.54983443527075\n", "Norm using Frobenius inner product: 7.54983443527075\n" ] } ], "source": [ "# Example for trace, determinant, and norm of a matrix\n", "# نمونه برای رآس، بَریهنده، و هنجار یک ماتکدان\n", "M=np.random.randint(1,5,(3,3))\n", "eigs=np.linalg.eigvals(M)\n", "detM=np.linalg.det(M)\n", "print('The matrix:\\n',M)\n", "print('---------------------')\n", "print('Eigenvalues of matrix:',eigs)\n", "print('Trace of matrix:',np.trace(M))\n", "print('sum of eigenvalues:',np.sum(eigs))\n", "print('-------------')\n", "print('Determinant of matrix: ',detM)\n", "print('Product of eigenvalues: ',np.prod(eigs))\n", "print('------------------')\n", "print('Frobenius norm of matrix:',np.linalg.norm(M,ord='fro'))\n", "print('Norm using Frobenius inner product:',np.sqrt(np.trace(M.T@M)))" ] }, { "cell_type": "code", "execution_count": null, "id": "0df1d5e9", "metadata": {}, "outputs": [], "source": [] } ], "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.8.15" } }, "nbformat": 4, "nbformat_minor": 5 }