-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj1_6_similarity.py
108 lines (90 loc) · 3.21 KB
/
proj1_6_similarity.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 26 10:31:59 2021
@author: changai
"""
from proj1_1_load_data import *
import numpy as np
import scipy.linalg as linalg
from similarity import similarity
import seaborn as sns; sns.set_theme()
import matplotlib.pyplot as plt
M = np.shape(X1)[1]
sim = np.zeros((1, M)).T
cos_sim = np.zeros((M, M)).T
extJacc = np.zeros((M, M)).T
correlation = np.zeros((M, M)).T
for i in range(M):
x1 = X1[:, i]
x2 = X2[:, i]
sim[i] = x1/linalg.norm(x1) @ x2/linalg.norm(x2) #cosine
# sim[i] = (x1 @ x2) / (linalg.norm(x1)**2 + linalg.norm(x2)**2 - (x1 @ x2)) #extended Jaccard
# sim[i] = similarity(x1.astype(np.float).T, x2.astype(np.float).T, 'cos')
# sim[i] = similarity(x1.astype(np.float).T, x2.astype(np.float).T, 'ext')
#sim[i] = similarity(x1.astype(np.float).T, x2.astype(np.float).T, 'cor')
print('Similarity results:\n {0}'.format(sim))
print('Ran similarity')
#cosine similarity
for i in range(M):
for j in range(M):
x1 = X[:, i]
x2 = X[:, j]
# print(x1)
cos_sim[i][j] = x1/linalg.norm(x1) @ x2/linalg.norm(x2) #cosine similarity
print(cos_sim)
fig, ax = plt.subplots(figsize=(11, 9))
ticklabels = ['Temp', ' RH', ' Ws', 'Rain ', 'FFMC', 'DMC', 'DC', 'ISI', 'BUI', 'FWI']
ax = sns.heatmap(cos_sim, annot=True, xticklabels=ticklabels, yticklabels=ticklabels, linewidths=.5, cmap="YlOrRd")
#sns.set(font_scale=2)
plt.title('Cosine similarity')
plt.xlabel('Both region')
plt.ylabel('Both region')
plt.show()
#extJacc similarity
for i in range(M):
for j in range(M):
x1 = X[:, i]
x2 = X[:, j]
# print(x1)
extJacc[i][j] = (x1 @ x2) / (linalg.norm(x1)**2 + linalg.norm(x2)**2 - (x1 @ x2)) #cosine similarity
print(extJacc)
fig, ax = plt.subplots(figsize=(11, 9))
ticklabels = ['Temp', ' RH', ' Ws', 'Rain ', 'FFMC', 'DMC', 'DC', 'ISI', 'BUI', 'FWI']
ax = sns.heatmap(extJacc, annot=True, xticklabels=ticklabels, yticklabels=ticklabels, linewidths=.5, cmap="YlOrRd")
#sns.set(font_scale=2)
plt.title('extJacc similarity')
plt.xlabel('Both region')
plt.ylabel('Both region')
plt.show()
#correlation
for i in range(M):
for j in range(M):
x1 = np.array([X[:, i].astype(np.float)])
x2 = np.array([X[:, j].astype(np.float)])
# print(x1)
cor = np.cov(x1, x2) / (np.std(x1) * np.std(x2)) #correlation
correlation[i][j] = cor[0][1]
print(correlation)
fig, ax = plt.subplots(figsize=(11, 9))
ticklabels = ['Temp', ' RH', ' Ws', 'Rain ', 'FFMC', 'DMC', 'DC', 'ISI', 'BUI', 'FWI']
ax = sns.heatmap(correlation, annot=True, xticklabels=ticklabels, yticklabels=ticklabels, linewidths=.5, cmap="YlOrRd")
#sns.set(font_scale=2)
plt.xlabel('Both region')
plt.ylabel('Both region')
plt.show()
# for i in range(M):
# x1 = np.array(X1[:, i].astype(np.float))
# x2 = np.array(X2[:, i].astype(np.float))
# print(i)
# print(x1)
# sim[i] = similarity(x1, x2, 'cor')
# print('Similarity results:\n {0}'.format(sim))
# x1 = np.array([X1[:, 8].astype(np.float)])
# x2 = np.array([X2[:, 8].astype(np.float)])
# print(x1)
# sim = similarity(x1, x2, 'cor')
# cor = np.cov(x1, x2) / (np.std(x1) * np.std(x2))
# print(cor)
# print(cor[0][1])
# print(sim)
# print('{:9.5}'.format(sim[0,0]))