File size: 4,397 Bytes
bcf16ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
{
"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}}$ <br>\n",
"$<A,B>_F=tr(A^HB) \\, \\rightarrow\\lVert A \\rVert_F=\\sqrt{<A,A>_F}$\n",
"###### by Hamed Shah-Hosseini\n",
"Explanation at: https://www.pinterest.com/HamedShahHosseini/Machine-Learning/Background-Knowledge\n",
"<br>Explanation in Persian: https://www.instagram.com/words.persian\n",
"<br>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
}
|