4-2 scikit-learn中的机器学习算法封装

import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from collections import Counter
from math import sqrt

class kNNClassifier:
    def __init__(self, k):
        # initialize kNN classifier
        assert  k >= 1, "k must be valid"
        self.k = k
        self._X_train = None
        self._y_train = None

    def fit(self, X_train, y_train):
        # train the kNN classifier based on the train set _X_train and _y_train
        assert  X_train.shape[0] == y_train.shape[0], \
            "the size of X_train must be equal to the size of y_train"
        assert  self.k <= X_train.shape[0], \
            "the size of X_train must be at least k"

        self._X_train = X_train
        self._y_train = y_train
        return self

    def predict(self, X_predict):
        # given the X_predict to be predict, return the result vector X_predict
        assert  self._X_train is not None and self._y_train is not None, \
            "must fit before predict"
        assert X_predict.shape[1] == self._X_train.shape[1], \
            "the feature number of X_predict must be equal to X_train"

        y_predict = [self._predict(x) for x in X_predict]
        return np.array(y_predict)

    def _predict(self, x):
        # given the single x to be predict, return the predict value
        assert  x.shape[0] == self._X_train.shape[1], \
            "the feature number of X_predict must be equal to X_train "
        distances = [sqrt(np.sum((x_train - x)**2)) for x_train in self._X_train]
        nearest = np.argsort(distances)
        topK_y = [self._y_train[i] for i in nearest[:self.k]]
        votes = Counter(topK_y)
        return votes.most_common(1)[0][0]

# 一般的sklearn中的kNN用法
kNN_classifier = KNeighborsClassifier(n_neighbors= 6)
kNN_classifier.fit(X_train, y_train)
X_predict = x.reshape(1,-1)
y_predict = kNN_classifier.predict(X_predict)
y_predict[0]


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值