-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
70 lines (58 loc) · 2.22 KB
/
utils.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
from __future__ import division, print_function
import numpy as np
import torch
from torch.utils.data import Dataset
import torchvision
from torchvision import datasets, transforms
from sklearn.metrics.cluster import normalized_mutual_info_score as nmi_score
from sklearn.metrics import adjusted_rand_score as ari_score
import h5py
import scipy.io
from sklearn import preprocessing
def load_reuters(data_path='./data/reuters'):
import os
if not os.path.exists(os.path.join(data_path, 'reutersidf10k.npy')):
print('making reuters idf features')
# make_reuters_data(data_path)
print(('reutersidf saved to ' + data_path))
data = np.load(os.path.join(data_path, 'reutersidf10k.npy'),allow_pickle=True).item()
# has been shuffled
x = data['data']
y = data['label']
x = x.reshape((x.shape[0], -1)).astype('float32')
y = y.reshape((y.size,))
print(('REUTERSIDF10K samples', x.shape))
return x, y
def LoadDatasetByName(dataset_name):
if dataset_name == 'reuters':
x, y = load_reuters()
return x, y
class LoadDataset(Dataset):
def __init__(self, dataset_name):
self.x, self.y = LoadDatasetByName(dataset_name)
def __len__(self):
return self.x.shape[0]
def __getitem__(self, idx):
return torch.from_numpy(np.array(self.x[idx])), torch.from_numpy(
np.array(self.y[idx])), torch.from_numpy(np.array(idx))
#######################################################
# Evaluate Critiron
#######################################################
def cluster_acc(y_true, y_pred):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
from sklearn.utils.linear_assignment_ import linear_assignment
ind = linear_assignment(w.max() - w)
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size