代码拉取完成,页面将自动刷新
同步操作将从 wanghairui-harry/DESOM 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
"""
Clustering metrics functions
@author Florent Forest
@version 2.0
"""
import numpy as np
from sklearn import metrics
from sklearn.utils.linear_assignment_ import linear_assignment
def cluster_acc(y_true, y_pred):
"""
Calculate unsupervised clustering accuracy. Requires scikit-learn installed
# Arguments
y_true: 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
ind = linear_assignment(w.max() - w)
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
def cluster_purity(y_true, y_pred):
"""
Calculate clustering purity
# Arguments
y_true: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
purity, 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
label_mapping = w.argmax(axis=1)
y_pred_voted = y_pred.copy()
for i in range(y_pred.size):
y_pred_voted[i] = label_mapping[y_pred[i]]
return metrics.accuracy_score(y_pred_voted, y_true)
def quantization_error(d):
"""
Calculate k-means quantization error (internal DESOM function)
"""
return d.min(axis=1).mean()
def topographic_error(d, map_size):
"""
Calculate SOM topographic error (internal DESOM function)
Topographic error is the ratio of data points for which the two best matching units are not neighbors on the map.
"""
h, w = map_size
def is_adjacent(k, l):
return (abs(k//w-l//w) == 1 and abs(k % w - l % w) == 0) or (abs(k//w-l//w) == 0 and abs(k % w - l % w) == 1)
btmus = np.argsort(d, axis=1)[:, :2] # best two matching units
return 1.-np.mean([is_adjacent(btmus[i, 0], btmus[i, 1]) for i in range(d.shape[0])])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。