1+ {
2+ "cells" :[
3+ {
4+ "cell_type" :" markdown" ,
5+ "source" :[
6+ " **In this notebook we will vector operations including sum, multiplication and dot product**"
7+ ],
8+ "attachments" :{
9+
10+ },
11+ "metadata" :{
12+ "datalore" :{
13+ "node_id" :" dgS96Y5ceG7IwdE10v42in" ,
14+ "type" :" MD" ,
15+ "hide_input_from_viewers" :true ,
16+ "hide_output_from_viewers" :true
17+ }
18+ }
19+ },
20+ {
21+ "cell_type" :" code" ,
22+ "source" :[
23+ " # let's start by importing the required libraries\n " ,
24+ " \n " ,
25+ " import numpy as np\n " ,
26+ " import time"
27+ ],
28+ "execution_count" :18 ,
29+ "outputs" :[
30+
31+ ],
32+ "metadata" :{
33+ "datalore" :{
34+ "node_id" :" ucOqR0H3WJYR57LgKqZ9zi" ,
35+ "type" :" CODE" ,
36+ "hide_input_from_viewers" :true ,
37+ "hide_output_from_viewers" :true
38+ }
39+ }
40+ },
41+ {
42+ "cell_type" :" code" ,
43+ "source" :[
44+ " # let's take an example of a vector\n " ,
45+ " \n " ,
46+ " v = np.array([[1],[3]])\n " ,
47+ " \n " ,
48+ " v_size = v.shape[0]\n " ,
49+ " magnitude = 0 \n " ,
50+ " for i in range(v_size):\n " ,
51+ " magnitude += np.square(v[i])\n " ,
52+ " \n " ,
53+ " magnitude = np.sqrt(magnitude)\n " ,
54+ " \n " ,
55+ " print(f\" For vector {v}, magnitude is: {magnitude}\" )"
56+ ],
57+ "execution_count" :11 ,
58+ "outputs" :[
59+ {
60+ "name" :" stdout" ,
61+ "text" :[
62+ " For vector [[1]\n " ,
63+ " [3]], magnitude is: [3.16227766]\n "
64+ ],
65+ "output_type" :" stream"
66+ }
67+ ],
68+ "metadata" :{
69+ "datalore" :{
70+ "node_id" :" JhzwFPkW2YJa13VDjz28Jk" ,
71+ "type" :" CODE" ,
72+ "hide_input_from_viewers" :true ,
73+ "hide_output_from_viewers" :true
74+ }
75+ }
76+ },
77+ {
78+ "cell_type" :" code" ,
79+ "source" :[
80+ " # now let's multiply our vector with a scalar value\n " ,
81+ " \n " ,
82+ " scalar_val = 50\n " ,
83+ " magnitude_mult = 0\n " ,
84+ " v_1 = v * scalar_val\n " ,
85+ " for i in range(v_size):\n " ,
86+ " magnitude_mult += np.square(v_1[i])\n " ,
87+ " \n " ,
88+ " magnitude_mult = np.sqrt(magnitude_mult)\n " ,
89+ " \n " ,
90+ " print(f\" For vector {v_1}, magnitude is: {magnitude_mult}\" )\n " ,
91+ " \n " ,
92+ " # let's also try to multiply magnitude directly by the scalar\n " ,
93+ " \n " ,
94+ " magnitude_mult_1 = magnitude * scalar_val\n " ,
95+ " \n " ,
96+ " print(f\" For vector {v_1}, magnitude is: {magnitude_mult_1}\" )\n "
97+ ],
98+ "execution_count" :12 ,
99+ "outputs" :[
100+ {
101+ "name" :" stdout" ,
102+ "text" :[
103+ " For vector [[ 50]\n " ,
104+ " [150]], magnitude is: [158.11388301]\n " ,
105+ " For vector [[ 50]\n " ,
106+ " [150]], magnitude is: [158.11388301]\n "
107+ ],
108+ "output_type" :" stream"
109+ }
110+ ],
111+ "metadata" :{
112+ "datalore" :{
113+ "node_id" :" GuaEiUgZA6dxajmOMcDDE3" ,
114+ "type" :" CODE" ,
115+ "hide_input_from_viewers" :true ,
116+ "hide_output_from_viewers" :true
117+ }
118+ }
119+ },
120+ {
121+ "cell_type" :" code" ,
122+ "source" :[
123+ " # now let's compute sum of two vectors\n " ,
124+ " \n " ,
125+ " v = np.array([[1],[3]])\n " ,
126+ " w = np.array([[4],[-1]])\n " ,
127+ " \n " ,
128+ " sum_vec = v + w\n " ,
129+ " \n " ,
130+ " print(f\" Sum of vector {v} and vector {w} is: {sum_vec}\" )"
131+ ],
132+ "execution_count" :14 ,
133+ "outputs" :[
134+ {
135+ "name" :" stdout" ,
136+ "text" :[
137+ " Sum of vector [[1]\n " ,
138+ " [3]] and vector [[ 4]\n " ,
139+ " [-1]] is: [[5]\n " ,
140+ " [2]]\n "
141+ ],
142+ "output_type" :" stream"
143+ }
144+ ],
145+ "metadata" :{
146+ "datalore" :{
147+ "node_id" :" 1MJX1i41Jknz65VP8aTbfk" ,
148+ "type" :" CODE" ,
149+ "hide_input_from_viewers" :true ,
150+ "hide_output_from_viewers" :true
151+ }
152+ }
153+ },
154+ {
155+ "cell_type" :" code" ,
156+ "source" :[
157+ " # now let's compute the norm of the vector which is used interchangibly with magnitude of the vector\n " ,
158+ " \n " ,
159+ " norm_v = np.linalg.norm(v)\n " ,
160+ " \n " ,
161+ " print(f\" Norm of the vector {v} is: {norm_v}\" )"
162+ ],
163+ "execution_count" :15 ,
164+ "outputs" :[
165+ {
166+ "name" :" stdout" ,
167+ "text" :[
168+ " Norm of the vector [[1]\n " ,
169+ " [3]] is: 3.1622776601683795\n "
170+ ],
171+ "output_type" :" stream"
172+ }
173+ ],
174+ "metadata" :{
175+ "datalore" :{
176+ "node_id" :" H6V7yhIL2uCE1KPojSSzVK" ,
177+ "type" :" CODE" ,
178+ "hide_input_from_viewers" :true ,
179+ "hide_output_from_viewers" :true
180+ }
181+ }
182+ },
183+ {
184+ "cell_type" :" code" ,
185+ "source" :[
186+ " # now let's calculate the dot product of two vectors\n " ,
187+ " # dot product is a scalar computation of two vectors to determine projection of one vector on another\n " ,
188+ " A = np.array([[1, 2], [3, 4]])\n " ,
189+ " B = np.array([[5, 6], [7, 8]])\n " ,
190+ " # Matrix multiplication\n " ,
191+ " result = np.dot(A, B)\n " ,
192+ " #dot_prod = np.dot(v.reshape(1,2),w)\n " ,
193+ " \n " ,
194+ " print(f\" Dot product of vector {v} and vector {w} is: {result}\" )\n "
195+ ],
196+ "execution_count" :19 ,
197+ "outputs" :[
198+ {
199+ "name" :" stdout" ,
200+ "text" :[
201+ " Dot product of vector [[1]\n " ,
202+ " [3]] and vector [[ 4]\n " ,
203+ " [-1]] is: [[19 22]\n " ,
204+ " [43 50]]\n "
205+ ],
206+ "output_type" :" stream"
207+ }
208+ ],
209+ "metadata" :{
210+ "datalore" :{
211+ "node_id" :" 9s7AC3oL8F7ger2VRhPinZ" ,
212+ "type" :" CODE" ,
213+ "hide_input_from_viewers" :true ,
214+ "hide_output_from_viewers" :true
215+ }
216+ }
217+ },
218+ {
219+ "cell_type" :" code" ,
220+ "source" :[
221+ " # let's calculate dot product of two vectors using np dot function and @ operator\n " ,
222+ " \n " ,
223+ " a = np.random.rand(1000000)\n " ,
224+ " b = np.random.rand(1000000)\n " ,
225+ " \n " ,
226+ " time_start = time.time()\n " ,
227+ " dot_prod = np.dot(a,b)\n " ,
228+ " time_stop = time.time()\n " ,
229+ " \n " ,
230+ " print(f\" Time taken to calculate dot_product {dot_prod} is : {time_stop-time_start}\" )"
231+ ],
232+ "execution_count" :20 ,
233+ "outputs" :[
234+ {
235+ "name" :" stdout" ,
236+ "text" :[
237+ " Time taken to calculate dot_product 250203.84421967238 is : 0.004907131195068359\n "
238+ ],
239+ "output_type" :" stream"
240+ }
241+ ],
242+ "metadata" :{
243+ "datalore" :{
244+ "node_id" :" ls1tyiR92aV3qRo57qjwu6" ,
245+ "type" :" CODE" ,
246+ "hide_input_from_viewers" :true ,
247+ "hide_output_from_viewers" :true
248+ }
249+ }
250+ },
251+ {
252+ "cell_type" :" code" ,
253+ "source" :[
254+ " # let's calculate dot product of two vectors using np dot function and @ operator\n " ,
255+ " \n " ,
256+ " a = np.random.rand(1000000)\n " ,
257+ " b = np.random.rand(1000000)\n " ,
258+ " \n " ,
259+ " time_start = time.time()\n " ,
260+ " dot_prod = a @ b\n " ,
261+ " time_stop = time.time()\n " ,
262+ " \n " ,
263+ " print(f\" Time taken to calculate dot_product {dot_prod} is : {time_stop-time_start}\" )"
264+ ],
265+ "execution_count" :22 ,
266+ "outputs" :[
267+ {
268+ "name" :" stdout" ,
269+ "text" :[
270+ " Time taken to calculate dot_product 249861.64146470415 is : 0.0008220672607421875\n "
271+ ],
272+ "output_type" :" stream"
273+ }
274+ ],
275+ "metadata" :{
276+ "datalore" :{
277+ "node_id" :" PIlt0vE8K227CHzZSC9bmu" ,
278+ "type" :" CODE" ,
279+ "hide_input_from_viewers" :true ,
280+ "hide_output_from_viewers" :true
281+ }
282+ }
283+ },
284+ {
285+ "cell_type" :" code" ,
286+ "source" :[
287+ " # Calculating vector similarity is one of the fundamental concepts of NLP \n " ,
288+ " \n " ,
289+ " A = np.array([[1, 2], [3, 4]])\n " ,
290+ " B = np.array([[5, 6], [7, 8]])\n " ,
291+ " \n " ,
292+ " dot_prod = np.dot(A, B)\n " ,
293+ " \n " ,
294+ " vect_sim = dot_prod\/ (np.linalg.norm(A)*np.linalg.norm(B))\n " ,
295+ " \n " ,
296+ " print(f\" Vector similarity between A & B is: {vect_sim}\" )"
297+ ],
298+ "execution_count" :24 ,
299+ "outputs" :[
300+ {
301+ "name" :" stdout" ,
302+ "text" :[
303+ " Vector similarity between A & B is: [[0.26297735 0.30450009]\n " ,
304+ " [0.59515927 0.69204567]]\n "
305+ ],
306+ "output_type" :" stream"
307+ }
308+ ],
309+ "metadata" :{
310+ "datalore" :{
311+ "node_id" :" lA3fnPUHZcqmeuyxiDTK53" ,
312+ "type" :" CODE" ,
313+ "hide_input_from_viewers" :true ,
314+ "hide_output_from_viewers" :true
315+ }
316+ }
317+ },
318+ {
319+ "cell_type" :" markdown" ,
320+ "source" :[
321+ " **Vector similarity is used in many practical applications from NLPs to Classification to Reccommendations**"
322+ ],
323+ "attachments" :{
324+
325+ },
326+ "metadata" :{
327+ "datalore" :{
328+ "node_id" :" OO7npmOt1sYII3rQ9gTkF2" ,
329+ "type" :" MD" ,
330+ "hide_input_from_viewers" :true ,
331+ "hide_output_from_viewers" :true
332+ }
333+ }
334+ }
335+ ],
336+ "metadata" :{
337+ "kernelspec" :{
338+ "display_name" :" Python" ,
339+ "language" :" python" ,
340+ "name" :" python"
341+ },
342+ "datalore" :{
343+ "computation_mode" :" JUPYTER" ,
344+ "package_manager" :" pip" ,
345+ "base_environment" :" default" ,
346+ "packages" :[
347+
348+ ],
349+ "report_row_ids" :[
350+
351+ ],
352+ "version" :3
353+ }
354+ },
355+ "nbformat" :4 ,
356+ "nbformat_minor" :4
357+ }
0 commit comments