一、题目
原生python实现knn分类算法(鸢尾花数据集)
二、算法设计
总体算法设计:
构建KNN分类器算法:
三、代码实现
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 13 15:37:22 2019
@author: 哩洛
"""
import pandas as pd
import numpy as np
from math import sqrt
from collections import Counter
class knnClass:
def __init__(self,k):
'''初始化'''
assert k>=1,"k必须是有效的"
self.k=k
self._x=None
self._y=None
def fit(self,x,y):
assert x.shape[0] == y.shape[0],"必须相同"
assert self.k<=x.shape[0