欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

机器学习5:KNN

程序员文章站 2022-07-14 20:32:37
...

机器学习5:KNN

KNN

原理

为了判断未知实例的类别,以所有已知类别的实例作为
参照选择参数K
计算未知实例与所有已知实例的距离
选择最近K个已知实例
根据少数服从多数的投票法则(majority-voting),让
未知实例归类为K个最邻近样本中最多数的类别

算法缺点
机器学习5:KNN

算法实现

Iris数据集
机器学习5:KNN

手工实现

# 导入算法包以及数据集
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report,confusion_matrix
import operator
import random

def knn(x_test, x_data, y_data, k):
    # 计算样本数量
    x_data_size = x_data.shape[0]
    # 复制x_test
    np.tile(x_test, (x_data_size,1))
    # 计算x_test与每一个样本的差值
    diffMat = np.tile(x_test, (x_data_size,1)) - x_data
    # 计算差值的平方
    sqDiffMat = diffMat**2
    # 求和
    sqDistances = sqDiffMat.sum(axis=1)
    # 开方
    distances = sqDistances**0.5
    # 从小到大排序
    sortedDistances = distances.argsort()
    classCount = {}
    for i in range(k):
        # 获取标签
        votelabel = y_data[sortedDistances[i]]
        # 统计标签数量
        classCount[votelabel] = classCount.get(votelabel,0) + 1
    # 根据operator.itemgetter(1)-第1个值对classCount排序,然后再取倒序
    sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1), reverse=True)
    # 获取数量最多的标签
    return sortedClassCount[0][0]

# 载入数据
iris = datasets.load_iris()
x_train,x_test,y_train,y_test = train_test_split(iris.data, iris.target, test_size=0.2) #分割数据0.2为测试数据,0.8为训练数据

#打乱数据
# data_size = iris.data.shape[0]
# index = [i for i in range(data_size)] 
# random.shuffle(index)  
# iris.data = iris.data[index]
# iris.target = iris.target[index]

#切分数据集
# test_size = 40
# x_train = iris.data[test_size:]
# x_test =  iris.data[:test_size]
# y_train = iris.target[test_size:]
# y_test = iris.target[:test_size]

predictions = []
for i in range(x_test.shape[0]):
    predictions.append(knn(x_test[i], x_train, y_train, 5))

print(classification_report(y_test, predictions))

机器学习5:KNN

sklearn实现

# 导入算法包以及数据集
from sklearn import neighbors
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import random

# 载入数据
iris = datasets.load_iris()
print(iris)

# 打乱数据切分数据集
x_train,x_test,y_train,y_test = train_test_split(iris.data, iris.target, test_size=0.2) #分割数据0.2为测试数据,0.8为训练数据

#打乱数据
# data_size = iris.data.shape[0]
# index = [i for i in range(data_size)] 
# random.shuffle(index)  
# iris.data = iris.data[index]
# iris.target = iris.target[index]

#切分数据集
# test_size = 40
# x_train = iris.data[test_size:]
# x_test =  iris.data[:test_size]
# y_train = iris.target[test_size:]
# y_test = iris.target[:test_size]

# 构建模型
model = neighbors.KNeighborsClassifier(n_neighbors=3)
model.fit(x_train, y_train)
prediction = model.predict(x_test)

print(classification_report(y_test, prediction))

机器学习5:KNN

相关标签: KNN