Skip to content

Commit 3c542d0

Browse files
committed
inner products
1 parent a8bf99a commit 3c542d0

File tree

2 files changed

+145
-1
lines changed

2 files changed

+145
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
1) Naive Bayes classifier for categorical data from scratch in Python
33
2) Naive Bayes classifier for continuous data from scratch in Python
44
3) Data Visualization: Showing Iris dataset with Blender API
5-
4) Norms in vector space: A review of norms, and reminding p-norms are included. Finally, we compare some special p-norms.
5+
4) Norms in vector space: A review of norms, and reminding p-norms are included. Finally, we compare some special p-norms.
6+
5) Inner products in vector space: Reminding dot product and Frobenius inner product, and then canonical norms based on them. There are examples with module *numpy*.

inner product-ML- background.ipynb

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "58525ac6",
6+
"metadata": {},
7+
"source": [
8+
"### Machine Learning (Background): Inner products\n",
9+
"$\\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",
10+
"$<A,B>_F=tr(A^HB) \\, \\rightarrow\\lVert A \\rVert_F=\\sqrt{<A,A>_F}$\n",
11+
"###### by Hamed Shah-Hosseini\n",
12+
"Explanation at: https://www.pinterest.com/HamedShahHosseini/Machine-Learning/Background-Knowledge\n",
13+
"<br>Explanation in Persian: https://www.instagram.com/words.persian\n",
14+
"<br>Code that: https://github.com/ostad-ai/Machine-Learning"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 98,
20+
"id": "ddd68127",
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"# importing the required module\n",
25+
"# درون‌بَری سنجانه نیازداشته\n",
26+
"import numpy as np"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 99,
32+
"id": "b12e168b",
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"name": "stdout",
37+
"output_type": "stream",
38+
"text": [
39+
"vector 1: [2 1 1 3]\n",
40+
"vector 2: [1 2 4 3]\n",
41+
"dot product: 17\n",
42+
"1-norm of vector 1: 7.0\n",
43+
"1-norm of vector 2 10.0\n",
44+
"2-norm of vector 1: 3.872983346207417\n",
45+
"2-norm of vector 2 5.477225575051661\n",
46+
"canonical norm of vector 1: 3.872983346207417\n",
47+
"canonical norm of vector 2 5.477225575051661\n"
48+
]
49+
}
50+
],
51+
"source": [
52+
"# dot product and p-norms, example\n",
53+
"# فرآورد خجک و پ-هنجارها: نمونه\n",
54+
"vec1=np.random.randint(1,5,4)\n",
55+
"vec2=np.random.randint(1,5,4)\n",
56+
"print('vector 1:',vec1)\n",
57+
"print('vector 2:',vec2)\n",
58+
"print('dot product: ',np.dot(vec1,vec2))\n",
59+
"print('1-norm of vector 1:',np.linalg.norm(vec1,ord=1))\n",
60+
"print('1-norm of vector 2',np.linalg.norm(vec2,ord=1))\n",
61+
"print('2-norm of vector 1:',np.linalg.norm(vec1,ord=2))\n",
62+
"print('2-norm of vector 2',np.linalg.norm(vec2,ord=2))\n",
63+
"print('canonical norm of vector 1:',np.sqrt(np.dot(vec1,vec1)))\n",
64+
"print('canonical norm of vector 2',np.sqrt(np.dot(vec2,vec2)))"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 102,
70+
"id": "1a0b718b",
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"name": "stdout",
75+
"output_type": "stream",
76+
"text": [
77+
"The matrix:\n",
78+
" [[3 2 2]\n",
79+
" [3 1 4]\n",
80+
" [1 3 2]]\n",
81+
"---------------------\n",
82+
"Eigenvalues of matrix: [ 6.92434399 1.21506597 -2.13940996]\n",
83+
"Trace of matrix: 6\n",
84+
"sum of eigenvalues: 5.999999999999998\n",
85+
"-------------\n",
86+
"Determinant of matrix: -18.000000000000004\n",
87+
"Product of eigenvalues: -18.0\n",
88+
"------------------\n",
89+
"Frobenius norm of matrix: 7.54983443527075\n",
90+
"Norm using Frobenius inner product: 7.54983443527075\n"
91+
]
92+
}
93+
],
94+
"source": [
95+
"# Example for trace, determinant, and norm of a matrix\n",
96+
"# نمونه برای رآس، بَریهنده، و هنجار یک ماتکدان\n",
97+
"M=np.random.randint(1,5,(3,3))\n",
98+
"eigs=np.linalg.eigvals(M)\n",
99+
"detM=np.linalg.det(M)\n",
100+
"print('The matrix:\\n',M)\n",
101+
"print('---------------------')\n",
102+
"print('Eigenvalues of matrix:',eigs)\n",
103+
"print('Trace of matrix:',np.trace(M))\n",
104+
"print('sum of eigenvalues:',np.sum(eigs))\n",
105+
"print('-------------')\n",
106+
"print('Determinant of matrix: ',detM)\n",
107+
"print('Product of eigenvalues: ',np.prod(eigs))\n",
108+
"print('------------------')\n",
109+
"print('Frobenius norm of matrix:',np.linalg.norm(M,ord='fro'))\n",
110+
"print('Norm using Frobenius inner product:',np.sqrt(np.trace(M.T@M)))"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": null,
116+
"id": "0df1d5e9",
117+
"metadata": {},
118+
"outputs": [],
119+
"source": []
120+
}
121+
],
122+
"metadata": {
123+
"kernelspec": {
124+
"display_name": "Python 3",
125+
"language": "python",
126+
"name": "python3"
127+
},
128+
"language_info": {
129+
"codemirror_mode": {
130+
"name": "ipython",
131+
"version": 3
132+
},
133+
"file_extension": ".py",
134+
"mimetype": "text/x-python",
135+
"name": "python",
136+
"nbconvert_exporter": "python",
137+
"pygments_lexer": "ipython3",
138+
"version": "3.8.15"
139+
}
140+
},
141+
"nbformat": 4,
142+
"nbformat_minor": 5
143+
}

0 commit comments

Comments
 (0)