-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.py
46 lines (38 loc) · 1.51 KB
/
util.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
import itertools
import subprocess
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import copy
import torch.nn.functional as F
import random
import csv
import sys
from torch import nn
from tqdm import tqdm_notebook, trange, tqdm
from pytorch_pretrained_bert.optimization import BertAdam
from pytorch_pretrained_bert.modeling import WEIGHTS_NAME,CONFIG_NAME,BertPreTrainedModel,BertModel
from pytorch_pretrained_bert.tokenization import BertTokenizer
from torch.utils.data import (DataLoader, RandomSampler, SequentialSampler, TensorDataset)
from datetime import datetime
from sklearn.cluster import KMeans
from sklearn.metrics import confusion_matrix,normalized_mutual_info_score, adjusted_rand_score, accuracy_score
from scipy.optimize import linear_sum_assignment
from sklearn import metrics
def hungray_aligment(y_true, y_pred):
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D))
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
ind = np.transpose(np.asarray(linear_sum_assignment(w.max() - w)))
return ind, w
def clustering_accuracy_score(y_true, y_pred):
ind, w = hungray_aligment(y_true, y_pred)
acc = sum([w[i, j] for i, j in ind]) / y_pred.size
return acc
def clustering_score(y_true, y_pred):
return {'ACC': round(clustering_accuracy_score(y_true, y_pred)*100, 2),
'ARI': round(adjusted_rand_score(y_true, y_pred)*100, 2),
'NMI': round(normalized_mutual_info_score(y_true, y_pred)*100, 2)}