File size: 4,216 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": "a5847596",
"metadata": {},
"source": [
"### Machine Learning (Background): Gram-Schmidt process\n",
"$\\mathbf{u_k}\\leftarrow \\mathbf{v_k}-\\sum_{j=1}^{k-1} proj_{\\mathbf{u}_j}(\\mathbf{v_k})$\n",
"<br>$, proj_{\\mathbf{u}_j}(\\mathbf{v_k})=\\frac{< \\mathbf{v}_k,\\mathbf{u}_j>}\n",
"{< \\mathbf{u}_j,\\mathbf{u}_j>}\\mathbf{u}_j$\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": 51,
"id": "7546b91d",
"metadata": {},
"outputs": [],
"source": [
"# importing the required module\n",
"# درونبَری سنجانه نیازداشته\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "3d6ffd39",
"metadata": {},
"outputs": [],
"source": [
"# this modified method can have smaller rounding errors\n",
"# این روش سنجیدهسازی شده، میتواند دارای ایرَنگهای گِرد کردن کوچکتری باشد\n",
"def modified_gram_schmidt(A,normalize=False): # row vectors\n",
" A=A.astype('float32')\n",
" n=A.shape[0] # no. of rows\n",
" for j in range(n):\n",
" for k in range(j):\n",
" A[j]-=np.dot(A[j],A[k])/np.dot(A[k],A[k])*A[k]\n",
" if normalize:\n",
" for j in range(n):\n",
" A[j]/=np.linalg.norm(A[j])\n",
" return A"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "78a08b83",
"metadata": {},
"outputs": [],
"source": [
"# The classical method which may have large rounding errors\n",
"# روش سَررَدهای که میتواند دارای ایرَنگهای گِرد کردن بزرگ باشد\n",
"def gram_schmidt(A,normalize=False):# row vectors\n",
" A=A.astype('float32')\n",
" n=A.shape[0] # no. of rows\n",
" temprow=np.zeros(A.shape[1])\n",
" for j in range(n):\n",
" temprow.fill(0)\n",
" for k in range(j):\n",
" temprow+=np.dot(A[j],A[k])/np.dot(A[k],A[k])*A[k]\n",
" A[j]-=temprow\n",
" if normalize:\n",
" for j in range(n):\n",
" A[j]/=np.linalg.norm(A[j])\n",
" return A"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "c5f5d909",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Classical method:\n",
" [[ 1. 0. 1. 0.]\n",
" [ 0. 1. 0. 1.]\n",
" [-1. 0. 1. 0.]]\n",
"Modified method:\n",
" [[ 1. 0. 1. 0.]\n",
" [ 0. 1. 0. 1.]\n",
" [-1. 0. 1. 0.]]\n"
]
}
],
"source": [
"# example, rows of matrix are vectors\n",
"# نمونه، رجهای ماتکدان، بُردارها هستند\n",
"A=np.array([[1,0,1,0],[1,1,1,1],[0,1,2,1]]) \n",
"print('Classical method:\\n',gram_schmidt(A))\n",
"print('Modified method:\\n',modified_gram_schmidt(A))"
]
},
{
"cell_type": "markdown",
"id": "111a3338",
"metadata": {},
"source": [
"Hint: You can check that the processed vectors are orthogonal.<br>\n",
"نکته: میتوانید چک کنید که بُردارهای پردازش شده، فرکنجی هستند"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55608cb7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|