-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScatterMat.py
47 lines (39 loc) · 1.19 KB
/
ScatterMat.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
import os
import numpy as np
import pickle
def ScatterMat(X, y):
dim, _ = X.shape
nclass = int(np.max(y)) + 1
mean_X = np.mean(X, axis=1)
Sw = np.zeros((dim,dim))
Sb = np.zeros((dim,dim))
for i in range(nclass):
inx_i = np.where(y==i)[0]
X_i = X[:,inx_i]
mean_Xi = np.mean(X_i, axis=1)
Sw += np.cov(X_i, rowvar=True, bias=True)
Sb += len(inx_i)*(mean_Xi-mean_X)[:,None]*(mean_Xi-mean_X)[None,:]
return Sw, Sb
if __name__ == '__main__':
DATA_FOLDER = "P:/RESEARCH/DATA/CIFAR-100/CIFARDB/train/"
features = pickle.load(open("./database/features_YCrCb_CifarDataset.pkl", 'rb'))
paths = pickle.load(open("./database/paths_YCrCb_CifarDataset.pkl", 'rb'))
features = features
paths = paths
classes = os.listdir(DATA_FOLDER)
X = np.array(features)
y = []
class_check = 'apple'
for path in paths:
label = 0
if class_check in path:
label = 1
y.append(label)
y = np.array(y)
X = X.T
y = y.reshape(-1, 1)
print(X.shape)
print(y.shape)
Sw, Sb = ScatterMat(X, y)
print(Sw.shape, Sb.shape)
print(np.max(Sw), np.max(Sb))