You are on page 1of 3

9/26/21, 5:18 PM MBA20299 Q1 (2) - Jupyter Notebook

Question 1
In [2]: def plot_2Dpoints(A,B):
import matplotlib.pyplot as plt
ax = plt.axes()
ax.arrow(0.0, 0.0, A[0], A[1], head_width=0.2, head_length=0.2)
plt.annotate(f"A({A[0]},{A[1]})", xy=(A[0], A[1]),xytext=(A[0]+0.5, A[1]))
ax.arrow(0.0, 0.0, B[0], B[1], head_width=0.2, head_length=0.2)
plt.annotate(f"B({B[0]},{B[1]})", xy=(B[0], B[1]),xytext=(B[0]+0.5, B[1]))
plt.xlim(0,10)
plt.ylim(0,10)
plt.show()
plt.close()

In [3]: import numpy as np

In [4]: from sklearn.metrics.pairwise import euclidean_distances


from sklearn.metrics.pairwise import cosine_similarity
from sklearn.metrics.pairwise import cosine_distances
from sklearn.metrics.pairwise import manhattan_distances
from sklearn.metrics import jaccard_score

consider 4 vectors C,H,P,B in 2-D


In [2]: ​
C=np.array([2,1,0,5,2,0])
H=np.array([5,4,4,3,0,5])
B=np.array([0,1,1,2,4,5])
P=np.array([1,3,0,2,0,3])

---------------------------------------------------------------------------

NameError Traceback (most recent call last)

<ipython-input-2-5882ddd5f4af> in <module>

----> 1 C=np.array([2,1,0,5,2,0])

2 H=np.array([5,4,4,3,0,5])

3 B=np.array([0,1,1,2,4,5])

4 P=np.array([1,3,0,2,0,3])

NameError: name 'np' is not defined

localhost:8891/notebooks/Downloads/MBA20299 Q1 (2).ipynb 1/3


9/26/21, 5:18 PM MBA20299 Q1 (2) - Jupyter Notebook

In [32]: # C-C

CC = cosine_similarity(C.reshape(1,-1),C.reshape(1,-1))
CH = cosine_similarity(C.reshape(1,-1),H.reshape(1,-1))
CP = cosine_similarity(C.reshape(1,-1),P.reshape(1,-1))
CB = cosine_similarity(C.reshape(1,-1),B.reshape(1,-1))

In [33]: print(CC, CH, CP, CB)

[[1.]] [[0.52136016]] [[0.5363989]] [[0.47529715]]

In [34]: HC = cosine_similarity(H.reshape(1,-1),C.reshape(1,-1))
HH = cosine_similarity(H.reshape(1,-1),H.reshape(1,-1))
HP = cosine_similarity(H.reshape(1,-1),P.reshape(1,-1))
HB = cosine_similarity(H.reshape(1,-1),B.reshape(1,-1))

In [35]: print(HC, HH, HP, HB)

[[0.52136016]] [[1.]] [[0.83061349]] [[0.59634143]]

In [36]: PC = cosine_similarity(P.reshape(1,-1),C.reshape(1,-1))
PH = cosine_similarity(P.reshape(1,-1),H.reshape(1,-1))
PP = cosine_similarity(P.reshape(1,-1),P.reshape(1,-1))
PB = cosine_similarity(P.reshape(1,-1),B.reshape(1,-1))

In [37]: print(PC, PH, PP, PB)

[[0.5363989]] [[0.83061349]] [[1.]] [[0.66912897]]

In [38]: BC = cosine_similarity(B.reshape(1,-1),C.reshape(1,-1))
BH = cosine_similarity(B.reshape(1,-1),H.reshape(1,-1))
BP = cosine_similarity(B.reshape(1,-1),P.reshape(1,-1))
BB = cosine_similarity(B.reshape(1,-1),B.reshape(1,-1))

In [39]: print(BC, BH, BP, BB)

[[0.47529715]] [[0.59634143]] [[0.66912897]] [[1.]]

Jaccard similarity

localhost:8891/notebooks/Downloads/MBA20299 Q1 (2).ipynb 2/3


9/26/21, 5:18 PM MBA20299 Q1 (2) - Jupyter Notebook

In [21]: def jaccard_set(list1, list2):


"""Define Jaccard Similarity function for two sets"""
intersection = len(list(set(list1).intersection(list2)))
union = (len(list1) + len(list2)) - intersection
return float(intersection) / union

# Define two sets
C=np.array([2,1,0,5,2,0])
H=np.array([5,4,4,3,0,5])
B=np.array([0,1,1,2,4,5])
P=np.array([1,3,0,2,0,3])


# Find Jaccard Similarity between the two sets
jaccard_set(C, C)

Out[21]: 0.5

In [23]: jaccard_set(C, H)

Out[23]: 0.2

In [24]: jaccard_set(C, B)

Out[24]: 0.5

In [22]: jaccard_set(C, P)

Out[22]: 0.3333333333333333

In [25]: jaccard_set(H, P)

Out[25]: 0.2

In [26]: jaccard_set(H, B)

Out[26]: 0.3333333333333333

In [ ]: ​

localhost:8891/notebooks/Downloads/MBA20299 Q1 (2).ipynb 3/3

You might also like