数据集介绍:
虹膜(花的名字)
from sklearn import neighbors
from sklearn import datasets
knn = neighbors.KNeighborsClassifier()
iris = datasets.load_iris()
print iris
knn.fit(iris.data,iris.target)
predictedLabel = knn.predict([[0.1,0.2,0.3,0.4]])
print predictedLabel
KNN的应用:
import csv
import random
import math
import operator
def loadDataset(filename,split,trainingSet=[],testSet=[]):
with open(filename,'rb') as csvfile:
lines = csv.reader(csvfile)
dataset = list(lines)
for x in range(len(dataset) - 1):
for y in range(len(dataset[x])-1):
dataset[x][y] = float(dataset[x][y])
if random.random() < split:
trainingSet.append(dataset[x])
else:
testSet.append(dataset[x])
def euclideanDistance(instance1,instance2):
distance = 0
print(instance1)
print("instance1 length = " + str(len(instance1)))
print("bird-sup")
print(instance2)
print("instance2 length = " + str(len(instance2)))
for x in range(len(instance1)-1):
distance += pow((instance1[x] - instance2[x]),2)
return math.sqrt(distance)
def getNeighbors(trainingSet, testInstance, k):
distances = []
print("trainingSet\'s length: "+str(len(trainingSet[0])))
print(trainingSet[0])
print(testInstance)
print("testInstance\'s length: "+str(len(testInstance)))
for x in range(len(trainingSet)):
dist = euclideanDistance(testInstance,trainingSet[x])
distances.append((trainingSet[x], dist))
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(k):
neighbors.append(distances[x][0])
return neighbors
def getResponse(neighbors):
classVotes = {}
for x in range(len(neighbors)):
response = neighbors[x][-1]
if response in classVotes:
classVotes[response] += 1
else:
classVotes[response] = 1
sortedVotes = sorted(classVotes.iteritems(),key=operator.itemgetter(1),reverse=True)
return sortedVotes[0][0]
def getAccuracy(testSet,predictions):
correct = 0
for x in range(len(testSet)):
if testSet[x][-1] == predictions[x]:
correct += 1
return (correct/float(len(testSet))) * 100.0
def main():
trainingSet = []
testSet = []
split = 0.67
loadDataset(r'E:\\data\\iris_data.txt',split,trainingSet,testSet)
print 'Train set: ' + repr(len(trainingSet))
print 'Test set: ' + repr(len(testSet))
predictions = []
k = 3
for x in range(len(testSet)):
neighbors = getNeighbors(trainingSet,testSet[x],k)
result = getResponse(neighbors)
predictions.append(result)
print('>predicted=' + repr(result) + ',actual=' + repr(testSet[x][-1]))
accuracy = getAccuracy(testSet,predictions)
print('Accuracy: ' + repr(accuracy) + '%')
main()
多标签学习(Multi-label Learning)
在图像、视频语义标注、文本分类等领域,数据标注结构复杂,一个事件往往同时具有多个标签:
体育,非洲,经济.......
多标签分类:确定样本所属的标签集合。
标签排序:依据标签与样本相关程序对标签进行排序。
0.7 0.1 0.9
天空(2) 海洋(3) 山脉(1)
问题转化法:转化为单标签学习问题
算法适应法:改进现有的单标签学习算法
决策树 ---》ML-DT
根据利用标签关联策略的不同,可将多标签分类方法划分为三类:
一阶策略
二阶策略
高阶策略
把多标签学习问题分解为多个独立的单标签,二分类问题,针对每个标签构建一个二分类器。
每个标签的置信度也可由每个分类器得到。
标签置信度即为投票数。
把训练集中每个不同的标签集合视为一个类别,将多标签学习问题转化为一个多分类问题。