A Review on Multi-Label Learning Algorithms - KNN

import numpy as np

def NB(X,Y,k,NN):
NBdis = [];
for i in range(0,Y.shape[1]):
NBdis.append( (np.sum(Y[:,i])+1) /float(Y.shape[0]+2));

NBtable = [];
for i in range(0,Y.shape[1]):
dis = np.zeros((k+1,2));
for j in range(0,X.shape[0]):
neighbours = NN[j];
tmpX = np.sum(Y[neighbours,i]);
if Y[j,i] == 0:
dis[tmpX,1] += 1;
else:
dis[tmpX,0] += 1;
smooth = 1;
dis = dis+1 / np.sum(dis+1,axis = 1,keepdims = True);
NBtable.append(dis);

return (NBdis,NBtable);

def findKNN(X,k):
NN = [];
for x in X:
tmpX = X.copy();
tmpX -= x;
tmpX = tmpX * tmpX;
distance = np.sum(tmpX,axis = 1);
NN.append(np.argsort(distance)[1:k+1]);
return(NN);

def predictFindNN(X,x,k):
tmpX = X.copy();
tmpX -= x;
tmpX = tmpX * tmpX;
distance = np.sum(tmpX,axis = 1);
return(np.argsort(distance)[0:k]);

def predictLabel(nn,NBdis,NBtable,Y):
tmpY = Y[nn];
tmpY = np.sum(tmpY,axis = 0);
labels = np.zeros((1,Y.shape[1]));
for i in range(labels.shape[1]):
if NBdis[i]*NBtable[i][tmpY[i],0] > (1-NBdis[i])*NBtable[i][tmpY[i],1]:
labels[0][i] = 1;
else:
labels[0][i] = 0;
return labels;

X = np.array([[1,0,1,1,0],[0,1,1,1,0],[1,0,1,0,1]])
Y = np.array([[1,0,1,1],[1,0,1,0],[1,0,0,0]]);
k = 2;
NN = findKNN(X,k);
print(NN);
print('\n');
(NBdis,NBtable) = NB(X,Y,k,NN);

print('\nNaive Bayes probs');
print(NBdis);

print('\nNaive Bayes table:');
for table in NBtable:
print(table);
print('\n');
x = ([0,1,1,1,0]);
nn = predictFindNN(X,x,k);

print('\nNearest neighbours');
print(nn);
label = predictLabel(nn,NBdis,NBtable,Y);

print('\nLabel predict');
print(label);

【笔记】A Review on Multi-Label Learning Algorithms

2016-04-16 19:56:41

Multi-Label Learning（一）之A Review on Multi-Label Learning Algorithms

2016-10-23 11:14:30

多标签分类，论文：A Review on Multi-Label Learning Algorithms

2016-11-12 22:06:48

scikit-learn（工程中用的相对较多的模型介绍）：1.12. Multiclass and multilabel algorithms

2015-08-07 08:27:58

1.12. Multiclass and multilabel algorithms 多分类多标签算法

2017-08-11 17:30:15

KNN for Multi-label learning

2017-04-05 09:40:36

根据《LDA漫游指南》来对gibbsLDA++结果进行分析

2016-06-21 18:57:44

A Tour of Machine Learning Algorithms

2016-11-04 16:28:08

Caffe 实现多标签分类 支持Multi-Label的ＬＭＤＢ数据格式输入

2017-12-11 14:32:05

笔记：A Review of Relational Machine Learning for Knowledge Graphs

2018-02-23 23:53:58

不良信息举报

A Review on Multi-Label Learning Algorithms - KNN