K-Nearest Neighbors近邻算法(KNN)

通过KNN算法计算唐人街探案是哪个类型的电影

import pandas as pd


# 取出列表中出现次数最多的字符
def max_list(lt):
    temp = 0
    for i in lt:
        if lt.count(i) > temp:
            max_str = i
            temp = lt.count(i)
    return max_str


def fun(x, y, z, company, n):
    list = []
    new_list = []
    type = []
    for i in range(len(company)):
        # 得到欧式距离
        dis = ((company.iloc[i, 0] - x) ** 2 + (company.iloc[i, 1] - y) ** 2 + (company.iloc[i, 2] - z) ** 2) ** 0.5
        # 将欧式距离全部放到list里面
        list.append(dis)
    # 排序得到list_
    list_ = sorted(list)
    # 排序后将前n个放入new_list 中
    for i in range(n):
        new_list.append(list_[i])
    # 通过对于的值在list里面找下标,然后再求出电影的类型
    for i in range(n):
        type.append(company.iloc[list.index(new_list[i]), 3])
    # 将得到的类型列表调用方法max_list
    max_str = max_list(type)
    print(type)
    return max_str


# 读取文件获得数组
company = pd.read_excel(r"C:\Users\Administrator\Desktop\电影分类数据.xlsx", encoding="gbk")
# 除去名字叫电影名称和序号的两列
company.drop(labels=['电影名称', '序号'], axis=1, inplace=True)

print(fun(23, 3, 17, company, 5))

猜你喜欢

转载自blog.csdn.net/runs_after_the_wind/article/details/82889818