定制做网站报价,电子商务网站建设系统功能,移动应用开发网站,有没有找项目的网站机器学习实战通常是将理论与实践结合#xff0c;通过实际的项目或案例#xff0c;帮助你理解并应用各种机器学习算法。下面是一个简单的机器学习实战例程#xff0c;使用Scikit-Learn库来完成一个常见的分类任务——**鸢尾花数据集#xff08;Iris Dataset#xff09;**的…机器学习实战通常是将理论与实践结合通过实际的项目或案例帮助你理解并应用各种机器学习算法。下面是一个简单的机器学习实战例程使用Scikit-Learn库来完成一个常见的分类任务——**鸢尾花数据集Iris Dataset**的分类。我们将通过该数据集来演示数据预处理、模型训练、评估和预测的全过程。 访问更多内容来源 https://ai.tmqcjr.com 
1. 安装所需库 
首先确保你已安装了scikit-learn和matplotlib等库如果没有请通过以下命令安装 bash 
复制代码 
pip install scikit-learn matplotlib  
2. 机器学习实战例程 
导入必要的库 python 
复制代码 
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import classification_report, confusion_matrix, accuracy_score  
加载数据集 
我们使用Scikit-Learn自带的鸢尾花数据集这是一个经典的机器学习数据集。 python 
复制代码 
# 加载鸢尾花数据集 iris  load_iris() X  iris.data # 特征数据花瓣和萼片的长度和宽度 y  iris.target # 标签数据花的种类  
数据探索 
在开始训练模型之前我们可以对数据进行简单的探索比如查看数据的维度和前几行。 python 
复制代码 
# 查看数据集的结构 print(f数据集的特征名称: {iris.feature_names}) print(f数据集的标签名称: {iris.target_names}) print(f数据集的特征形状: {X.shape}) print(f数据集的标签形状: {y.shape}) # 查看前5行数据 print(f特征数据:\n{X[:5]}) print(f标签数据:\n{y[:5]})  
数据划分 
我们将数据集划分为训练集和测试集通常使用70%训练30%测试的比例。 python 
复制代码 
# 划分数据集为训练集和测试集 X_train, X_test, y_train, y_test  train_test_split(X, y, test_size0.3, random_state42) print(f训练集的样本数量: {X_train.shape[0]}) print(f测试集的样本数量: {X_test.shape[0]})  
数据预处理 
在使用机器学习模型之前通常需要对数据进行标准化处理以便提高模型的性能。 python 
复制代码 
# 数据标准化将特征缩放至均值为0方差为1的标准正态分布 scaler  StandardScaler() X_train  scaler.fit_transform(X_train) X_test  scaler.transform(X_test)  
训练模型 
我们将训练多个机器学习模型进行比较。这里使用常见的几种分类模型K近邻KNN、支持向量机SVM、决策树和随机森林。 
1. K近邻KNN python 
复制代码 
# 初始化KNN模型并训练 knn  KNeighborsClassifier(n_neighbors3) knn.fit(X_train, y_train) # 在测试集上评估模型 y_pred_knn  knn.predict(X_test) print(KNN分类报告:) print(classification_report(y_test, y_pred_knn)) print(fKNN的准确率: {accuracy_score(y_test, y_pred_knn)})  
2. 支持向量机SVM python 
复制代码 
# 初始化SVM模型并训练 svm  SVC(kernellinear) svm.fit(X_train, y_train) # 在测试集上评估模型 y_pred_svm  svm.predict(X_test) print(SVM分类报告:) print(classification_report(y_test, y_pred_svm)) print(fSVM的准确率: {accuracy_score(y_test, y_pred_svm)})  
3. 决策树Decision Tree python 
复制代码 
# 初始化决策树模型并训练 dt  DecisionTreeClassifier(random_state42) dt.fit(X_train, y_train) # 在测试集上评估模型 y_pred_dt  dt.predict(X_test) print(决策树分类报告:) print(classification_report(y_test, y_pred_dt)) print(f决策树的准确率: {accuracy_score(y_test, y_pred_dt)})  
4. 随机森林Random Forest python 
复制代码 
# 初始化随机森林模型并训练 rf  RandomForestClassifier(n_estimators100, random_state42) rf.fit(X_train, y_train) # 在测试集上评估模型 y_pred_rf  rf.predict(X_test) print(随机森林分类报告:) print(classification_report(y_test, y_pred_rf)) print(f随机森林的准确率: {accuracy_score(y_test, y_pred_rf)})  
评估模型 
使用classification_report来评估模型的性能显示精确度Precision、召回率Recall和F1-score。accuracy_score则显示整体的分类准确率。 python 
复制代码 
# 显示每个模型的准确率 models  [KNN, SVM, 决策树, 随机森林] accuracies  [ accuracy_score(y_test, y_pred_knn), accuracy_score(y_test, y_pred_svm), accuracy_score(y_test, y_pred_dt), accuracy_score(y_test, y_pred_rf) ] for model, accuracy in zip(models, accuracies): print(f{model}的准确率: {accuracy})  
混淆矩阵 
为了进一步分析模型的分类效果可以绘制混淆矩阵。 python 
复制代码 
# 绘制混淆矩阵 def plot_confusion_matrix(cm, classes): plt.figure(figsize(6, 6)) plt.imshow(cm, interpolationnearest, cmapplt.cm.Blues) plt.title(Confusion Matrix) plt.colorbar() tick_marks  np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation45) plt.yticks(tick_marks, classes) plt.xlabel(Predicted label) plt.ylabel(True label) plt.tight_layout() # KNN模型的混淆矩阵 cm_knn  confusion_matrix(y_test, y_pred_knn) plot_confusion_matrix(cm_knn, iris.target_names) # 显示图形 plt.show()  
预测新数据 
最后我们可以使用训练好的模型对新的数据进行预测。 python 
复制代码 
# 使用KNN模型对新样本进行预测 new_data  np.array([[5.1, 3.5, 1.4, 0.2]]) # 一个新的样本鸢尾花特征 new_data  scaler.transform(new_data) # 标准化 prediction  knn.predict(new_data) print(f预测的花种类: {iris.target_names[prediction]})  
3. 模型总结 
通过上述步骤我们完成了以下内容 
数据加载与预处理加载鸢尾花数据集并进行标准化处理。模型训练与评估训练了4个常见的机器学习模型KNN、SVM、决策树和随机森林并通过classification_report和accuracy_score评估了各个模型的性能。模型预测使用训练好的模型对新数据进行了预测。 
4. 总结 
KNN适合用于小型数据集计算复杂度较高。SVM对于中小型数据集效果不错但训练时间较长。决策树易于理解和解释但容易过拟合。随机森林通过集成多棵决策树通常表现良好减少了过拟合的风险。 
在实际的机器学习项目中你可以根据任务的特点选择合适的模型并不断调整参数以优化模型的表现。 文章转载自: http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.wtwhj.cn.gov.cn.wtwhj.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.lmctj.cn.gov.cn.lmctj.cn http://www.morning.fwmln.cn.gov.cn.fwmln.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.kkwbw.cn.gov.cn.kkwbw.cn http://www.morning.dbnpz.cn.gov.cn.dbnpz.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn http://www.morning.tfei69.cn.gov.cn.tfei69.cn http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.hrrmb.cn.gov.cn.hrrmb.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.kzpxc.cn.gov.cn.kzpxc.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.kqkmx.cn.gov.cn.kqkmx.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.rqhdt.cn.gov.cn.rqhdt.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.rtkz.cn.gov.cn.rtkz.cn http://www.morning.ityi666.cn.gov.cn.ityi666.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.kryr.cn.gov.cn.kryr.cn http://www.morning.lkjzz.cn.gov.cn.lkjzz.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.krjrb.cn.gov.cn.krjrb.cn http://www.morning.nzhzt.cn.gov.cn.nzhzt.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.nkwgy.cn.gov.cn.nkwgy.cn http://www.morning.hymmq.cn.gov.cn.hymmq.cn http://www.morning.qmfhh.cn.gov.cn.qmfhh.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.rqsnl.cn.gov.cn.rqsnl.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.gnkdp.cn.gov.cn.gnkdp.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.mrpqg.cn.gov.cn.mrpqg.cn http://www.morning.trjr.cn.gov.cn.trjr.cn http://www.morning.zbqry.cn.gov.cn.zbqry.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn