-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.py
More file actions
41 lines (33 loc) · 1.06 KB
/
task1.py
File metadata and controls
41 lines (33 loc) · 1.06 KB
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
# -*- coding: utf-8 -*-
"""Task1.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1JERPg9cxxJHdyIb1OYDDK06Yaf7CIgY5
"""
import numpy as np
def euclidean_dist(a,b):
a = np.array(a)
b = np.array(b)
return np.linalg.norm(a - b)
def manhattan_dist(a, b):
a = np.array(a)
b = np.array(b)
return np.sum(np.abs(a - b))
def cosine_dist_angle (a,b):
a = np.array(a)
b = np.array(b)
dot_prod = np.dot(a, b)
norm1 = np.linalg.norm(a)
norm2 = np.linalg.norm(b)
if norm1 == 0 or norm2 == 0:
print("One of the vectors is zero; cosine distance is not defined; Angle between them is not defined.")
else:
cosine_sim = dot_prod / (norm1 * norm2)
cosine_sim = np.clip(cosine_sim, -1.0, 1.0)
angle = np.arccos(cosine_sim)
return 1 - cosine_sim , angle
a = [1, 2, 0]
b = [4, 8, 6]
print("Euclidean Distance:", euclidean_dist(a,b))
print("Manhattan Distance:", manhattan_dist(a,b))
print("Cosine Distance & Angle between vectors :", cosine_dist_angle(a,b))