1.KNN

查找距离已知的几个点最近的类型,并返回这个类型进行预测。

如小明在北京,小红在北京,小刚在河南,而我距离小明和小红比小刚近,则我最可能在北京而不是河南

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : KNN近邻算法.py
# @Author: 赵路仓
# @Date  : 2020/4/2
# @Desc  : 学习网站:https://www.bilibili.com/video/BV1nt411r7tj?p=21
# @Contact : 398333404@qq.com

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
import numpy as np


def knn_iris():
    """
    用KNN算法对鸢尾花进行分类
    :return:
    """
    # 1.获取数据
    iris = load_iris()
    print(iris)

    # 2.划分数据集
    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=6)

    # 3.特征工程:标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)

    # 4.KNN算法预估器
    estimator = KNeighborsClassifier(n_neighbors=6)
    estimator.fit(x_train, y_train)

    # 5.模型评估
    # 方法一:直接对比真实数据和预测值
    y_predit = estimator.predict(x_test)
    print("y_predit:\n", y_predit)
    print("对比真实值和预测值:\n", y_test == y_predit)

    # 方法2:计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为:\n", score)

    # 预测新的鸾尾花品种
    x_new = np.array([[5, 2.9, 1, 0.2]])
    prediction = estimator.predict(x_new)
    print(prediction)
    return None


def knn_iris_gscv():
    """
    用KNN算法对鸢尾花进行分类,添加网格搜索与交叉验证
    :return:
    """
    # 1.获取数据
    iris = load_iris()
    print(iris)

    # 2.划分数据集
    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=6)

    # 3.特征工程:标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)

    # 4.KNN算法预估器
    estimator = KNeighborsClassifier(n_neighbors=5)
    # 加入网格搜索与交叉验证
    # 参数准备 从下侧中取n_neighbors
    param_dict = {
        "n_neighbors": [1, 3, 5, 7, 9, 11]
    }
    estimator = GridSearchCV(estimator, param_grid=param_dict, cv=10)
    estimator.fit(x_train, y_train)

    # 5.模型评估
    # 方法一:直接对比真实数据和预测值
    y_predit = estimator.predict(x_test)
    print("y_predit:\n", y_predit)
    print("对比真实值和预测值:\n", y_test == y_predit)

    # 方法2:计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为:\n", score)

    """
       最佳参数:best_params_
       最佳结果:best_score_
       最佳估计器:best_estimator_
       交叉验证结果:cv_results_
       """
    print("最佳参数:\n", estimator.best_params_)
    print("最佳结果:\n", estimator.best_score_)
    print("最佳估计器:\n", estimator.best_estimator_)
    print("交叉验证结果:\n", estimator.cv_results_)

    # 预测新的鸾尾花品种
    x_new = np.array([[5, 2.9, 1, 0.2]])
    prediction = estimator.predict(x_new)
    print(prediction)
    return None


if __name__ == "__main__":
    # 代码1:KNN对鸾尾花分类
    # knn_iris()
    # 代码2:KNN预测鸾尾花分类并添加网格搜索和交叉验证
    knn_iris_gscv()
View Code

相关文章:

  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2022-02-01
  • 2021-12-07
  • 2022-12-23
  • 2021-08-01
  • 2021-10-30
猜你喜欢
  • 2022-01-30
  • 2021-07-31
  • 2021-05-04
  • 2021-10-05
  • 2022-12-23
  • 2021-11-29
  • 2021-11-25
相关资源
相似解决方案