代码:
from sklearn.datasets import load_iris, fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.naive_bayes import MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier,export_graphvizdef knn_iris():# 用KNN 算法对鸢尾花进行分类# 1、获取数据iris = load_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=3)estimator.fit(x_train,y_train)# 5、模型评估# 方法1 :直接比对真实值和预测值y_predict = estimator.predict(x_test)print("y_predict:\n",y_predict)print("直接比对真实值和预测值:\n",y_test == y_predict)# 方法2:计算准确率score = estimator.score(x_test,y_test)print("准确率为:\n",score)return Nonedef knn_iris_gscv():# 用KNN 算法对鸢尾花进行分类,添加网格搜索和交叉验证# 1、获取数据iris = load_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()# 加入网格搜索和交叉验证# 参数准备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、模型评估# 方法1 :直接比对真实值和预测值y_predict = estimator.predict(x_test)print("y_predict:\n",y_predict)print("直接比对真实值和预测值:\n",y_test == y_predict)# 方法2:计算准确率score = estimator.score(x_test,y_test)print("准确率为:\n",score)# 最佳参数:best_params_print("最佳参数:\n",estimator.best_params_)# 最佳结果:best_score_print("最佳结果:\n",estimator.best_score_)# 最佳估计值:best_estimator_print("最佳估计值:\n",estimator.best_estimator_)# 交叉验证结果:cv_results_print("交叉验证结果:\n",estimator.cv_results_)return Nonedef nb_news():# 用朴素贝叶斯算法对新闻进行分类# 1、获取数据news = fetch_20newsgroups(subset="all")# 2、划分数据集x_train,x_test,y_train,y_test = train_test_split(news.data,news.target)# 3、特征工程:文本特征抽取-tfidftransfer = TfidfVectorizer()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# 4、用朴素贝叶斯算法预估器流程estimator = MultinomialNB()estimator.fit(x_train,y_train)# 5、模型评估# 方法1 :直接比对真实值和预测值y_predict = estimator.predict(x_test)print("y_predict:\n", y_predict)print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法2:计算准确率score = estimator.score(x_test, y_test)print("准确率为:\n", score)return Nonedef decision_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、决策树预估器estimator = DecisionTreeClassifier(criterion="entropy")estimator.fit(x_train,y_train)# 4、模型评估# 方法1 :直接比对真实值和预测值y_predict = estimator.predict(x_test)print("y_predict:\n", y_predict)print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法2:计算准确率score = estimator.score(x_test, y_test)print("准确率为:\n", score)# 可视化决策树export_graphviz(estimator,out_file="iris_tree.dot",feature_names=iris.feature_names)return Noneif __name__ == "__main__":# 代码1 :用KNN算法对鸢尾花进行分类# knn_iris()# 代码2 :用KNN算法对鸢尾花进行分类,添加网格搜索和交叉验证# knn_iris_gscv()# 代码3:用朴素贝叶斯算法对新闻进行分类# nb_news()# 代码4:用决策树对鸢尾花进行分类decision_iris()