当前位置: 首页 > wzjs >正文

做外贸网站客服东台网络推广

做外贸网站客服,东台网络推广,wordpress 4.9.2 中文,安徽茶叶网站建设全流程 以下是一个更复杂、全流程的决策树和随机森林示例,不仅包括模型训练和预测,还涵盖了数据预处理、超参数调优以及模型评估的可视化。我们依旧使用鸢尾花数据集,并额外引入 GridSearchCV 进行超参数调优,使用 matplotlib 进…

全流程

以下是一个更复杂、全流程的决策树和随机森林示例,不仅包括模型训练和预测,还涵盖了数据预处理、超参数调优以及模型评估的可视化。我们依旧使用鸢尾花数据集,并额外引入 GridSearchCV 进行超参数调优,使用 matplotlib 进行简单的可视化。

 

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score

from sklearn.preprocessing import StandardScaler

from sklearn.tree import DecisionTreeClassifier, export_graphviz

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, roc_curve, auc

from sklearn.externals.six import StringIO  

from IPython.display import Image  

from graphviz import Source

import pydotplus

 

# 1. 加载鸢尾花数据集

iris = load_iris()

X = pd.DataFrame(iris.data, columns=iris.feature_names)

y = pd.Series(iris.target)

 

# 2. 数据预处理

# 2.1 特征标准化

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

 

# 3. 划分训练集和测试集

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.3, random_state=42)

 

# 4. 决策树模型

# 4.1 定义超参数搜索空间

dtc_param_grid = {

    'max_depth': [3, 5, 7, 10],

    'min_samples_split': [2, 5, 10],

    'min_samples_leaf': [1, 2, 4]

}

 

# 4.2 使用GridSearchCV进行超参数调优

dtc_grid_search = GridSearchCV(DecisionTreeClassifier(random_state=42), dtc_param_grid, cv=5)

dtc_grid_search.fit(X_train, y_train)

 

# 4.3 输出最佳超参数

print("决策树最佳超参数:", dtc_grid_search.best_params_)

 

# 4.4 使用最佳超参数构建决策树模型

dtc_best = dtc_grid_search.best_estimator_

 

# 4.5 预测并评估

y_pred_dtc = dtc_best.predict(X_test)

dtc_accuracy = accuracy_score(y_test, y_pred_dtc)

print(f"决策树模型的准确率: {dtc_accuracy}")

print("决策树分类报告:\n", classification_report(y_test, y_pred_dtc))

print("决策树混淆矩阵:\n", confusion_matrix(y_test, y_pred_dtc))

 

# 4.6 可视化决策树(需要graphviz工具支持)

dot_data = StringIO()

export_graphviz(dtc_best, out_file=dot_data,  

                filled=True, rounded=True,

                special_characters=True, feature_names=iris.feature_names, class_names=iris.target_names)

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  

Image(graph.create_png())

 

# 5. 随机森林模型

# 5.1 定义超参数搜索空间

rfc_param_grid = {

    'n_estimators': [50, 100, 200],

   'max_depth': [3, 5, 7, 10],

   'min_samples_split': [2, 5, 10],

   'min_samples_leaf': [1, 2, 4]

}

 

# 5.2 使用GridSearchCV进行超参数调优

rfc_grid_search = GridSearchCV(RandomForestClassifier(random_state=42), rfc_param_grid, cv=5)

rfc_grid_search.fit(X_train, y_train)

 

# 5.3 输出最佳超参数

print("随机森林最佳超参数:", rfc_grid_search.best_params_)

 

# 5.4 使用最佳超参数构建随机森林模型

rfc_best = rfc_grid_search.best_estimator_

 

# 5.5 预测并评估

y_pred_rfc = rfc_best.predict(X_test)

rfc_accuracy = accuracy_score(y_test, y_pred_rfc)

print(f"随机森林模型的准确率: {rfc_accuracy}")

print("随机森林分类报告:\n", classification_report(y_test, y_pred_rfc))

print("随机森林混淆矩阵:\n", confusion_matrix(y_test, y_pred_rfc))

 

# 5.6 绘制ROC曲线(以二分类为例,这里简单取其中一类演示)

fpr_rfc, tpr_rfc, thresholds_rfc = roc_curve(y_test == 0, rfc_best.predict_proba(X_test)[:, 0])

roc_auc_rfc = auc(fpr_rfc, tpr_rfc)

 

plt.figure()

plt.plot(fpr_rfc, tpr_rfc, label='Random Forest (area = %0.2f)' % roc_auc_rfc)

plt.plot([0, 1], [0, 1], 'k--')

plt.xlim([0.0, 1.0])

plt.ylim([0.0, 1.05])

plt.xlabel('False Positive Rate')

plt.ylabel('True Positive Rate')

plt.title('Receiver operating characteristic example')

plt.legend(loc="lower right")

plt.show()

 

 

代码解释:

 

1. 数据加载与预处理:加载鸢尾花数据集,将其转换为 DataFrame 和 Series 形式,并对特征进行标准化处理。

2. 数据划分:将数据集划分为训练集和测试集。

3. 决策树模型:定义超参数搜索空间,使用 GridSearchCV 进行超参数调优,得到最佳超参数后构建决策树模型,进行预测和评估,并可视化决策树。

4. 随机森林模型:类似地,定义随机森林的超参数搜索空间,进行超参数调优,构建模型,预测评估,并绘制ROC曲线进行可视化。

 

http://www.dtcms.com/wzjs/50171.html

相关文章:

  • 个人养老金制度西安网站排名优化培训
  • 网站建设彩铃常州免费网站建站模板
  • 公司起名字推荐北京自动seo
  • 廊坊网站建设的公司外贸平台自建站
  • 地图网站抓取百度短链接在线生成
  • 网站备案被注销了什么叫优化关键词
  • 餐饮手机微网站怎么做网页制作免费模板
  • 专注做一家男生最爱的网站关键词seo优化排名
  • 做我女朋友好不好套路网站百度投放广告联系谁
  • 辽宁住房和城乡建设网站网站建设方案内容
  • 专业做写生的网站百度seo优化技巧
  • 上海网站建设sheji021百度发广告需要多少钱
  • 济南网站建设伍际网络惠州seo按天计费
  • 传送门网站是怎么做的刚刚发生了一件大事
  • 链接网站怎么做企业seo顾问
  • 注册网站到公安机关备案aso优化的主要内容
  • 国内永久免费的建站百度关键词seo公司
  • 什么值得买 网站开发24小时最新国际新闻
  • 网站logo尺寸一般多大微信推广软件有哪些
  • 手机网站制作注意事项网站建设网络公司
  • 新疆建设厅进疆备案官方网站免费关键词挖掘网站
  • 晋江做网站模板宁波seo快速优化公司
  • 建设工程执业注册中心网站网页制作公司
  • wordpress主体下载关键词优化的五个步骤
  • 可以做请柬的网站网络服务
  • 鼓楼徐州网站开发优化网络搜索引擎
  • 秀山网站建设新手怎么做网络推广
  • 庆阳市建设局网站站长工具怎么用
  • 系部网站开发项目的目的百度关键词刷搜索量
  • 施工企业市场部360seo