栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

机器学习 day2

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

机器学习 day2

from sklearn.datasets import load_iris
from sklearn.datasets import fetch_20newsgroups
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.tree import DecisionTreeClassifier, export_graphviz


def knn_iris():
    # 1.加载数据集
    iris = load_iris()
    # 2.数据划分
    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
    # 3.特征工程:标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)
    # 4.KNN算法预估器
    estimator = KNeighborsClassifier(n_neighbors=3)
    estimator.fit(x_train, y_train)
    # 5.模型评估
    # 1.直接比较
    y_predict = estimator.predict(x_test)
    print("直接比较:n", y_test == y_predict)
    # 2.计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为:", score)


def knn_iris_gscv():
    """
    KNN算法对鸢尾花进行分类:添加网格搜索和交叉验证
    :return:
    """
    # 加载数据集
    iris = load_iris()
    # 数据集拆分
    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
    # 标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)
    # 预估器生成模型
    estimator = KNeighborsClassifier()
    param_list = {"n_neighbors": [1, 3, 5, 7, 9]}
    estimator = GridSearchCV(estimator, param_grid=param_list, cv=10)
    estimator.fit(x_train, y_train)
    # 评估模型
    # 1.直接比较模型值
    y_predict = estimator.predict(x_test)
    print("直接比较:", y_test == y_predict)
    # 2.计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率:", score)

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


def nb_demo():
    """
    朴素贝叶斯:对新闻进行分类
    :return:
    """
    # 加载数据集
    news = fetch_20newsgroups(subset="all")
    # 对数据集进行划分
    x_train, x_test, y_train, y_test = train_test_split(news.data, news.target)
    # 进行文本特征提取
    transfer = TfidfVectorizer()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)
    # 生成模型
    estimator = MultinomialNB()
    estimator.fit(x_train, y_train)
    # 模型评估
    # 直接比较真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:", y_predict)
    print("真实值和预测值:", y_predict == y_test)
    # 计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率:", score)


def decision_iris():
    """
    用决策树对鸢尾花进行分类
    :return:
    """
    # 加载数据
    iris = load_iris()
    # 数据集划分
    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
    # 生成模型
    estimator = DecisionTreeClassifier(criterion="entropy")
    estimator.fit(x_train, y_train)
    # 模型评估
    # 将真实值和预估值进行比较
    y_predict = estimator.predict(x_test)
    print("y_predict:", y_predict)
    print("真实值和预估值:", y_predict == y_test )
    # 计算准确率
    score = estimator.score(x_test, y_test)
    print("score:", score)

    # 可视化树
    export_graphviz(estimator, out_file="iris_tree.dot", feature_names=iris.feature_names)


if __name__ == '__main__':
    # knn_iris()
    # knn_iris_gscv()
    # nb_demo()
    decision_iris()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/299691.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号