一天一个算法·笔记

kNN__---只为自己记录

# !/usr/bin/python
# -*- coding: UTF-8 -*-
#创建数据集
a = \
[
    {
        "电影名称": "man",
        "打斗镜头": 3,
        "接吻镜头": 104,
        "电影类型": "爱情片"
    },
{
        "电影名称": "He's Not Really into Dudes",
        "打斗镜头": 2,
        "接吻镜头": 100,
        "电影类型": "爱情片"
    },
{
        "电影名称": "Amped II",
        "打斗镜头": 98,
        "接吻镜头": 2,
        "电影类型": "动作片"
    },
{
        "电影名称": "Robo Slayer 3000",
        "打斗镜头": 99,
        "接吻镜头": 5,
        "电影类型": "爱情片"
    },
]

b = [1, 5, 8, 10, 66, 9]


def quick_sort(quick_list):
    if len(quick_list) < 2:
        return quick_list
    mid = quick_list[len(quick_list)//2]
    left_list = []
    right_list = []
    quick_list.remove(mid)
    for i in quick_list:
        if mid >= i:
            left_list.append(i)
        else:
            right_list.append(i)
    return quick_sort(left_list) + [mid] + quick_sort(right_list)

print(quick_sort(b))

from numpy import *
import operator



def creatDataSet():
    group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
    labels = ["A","A","B","B"]
    return group,labels

group,labels = creatDataSet()

def classify0(inX, dataset, labels, k):
    datasetSize = dataset.shape[0]
    diffMat = tile(inX, (datasetSize, 1)) - dataset
    sqDiffMat = diffMat ** 2
    print(sqDiffMat)
    sqDistances = sqDiffMat.sum(axis=1)
    print(sqDistances)
    distances = sqDistances ** 0.5
    print(distances)
    sortedDistIndicies = distances.argsort()

    classCount = {}
    for i in range(k):
        voteIlabel = labels[sortedDistIndicies[i]]
        classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
    sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1),reverse=True)
    print(sortedClassCount)
    return sortedClassCount[0][0]

print(classify0([0,0],group,labels,3))

 

kNN算法基础使用

posted @ 2020-07-13 19:04  James·Sean  阅读(12)  评论(0编辑  收藏