{
"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",
"
$, 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",
"
Explanation in Persian: https://www.instagram.com/words.persian\n",
"
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.
\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
}