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

影楼网站模板下载/网站推广的具体方案

影楼网站模板下载,网站推广的具体方案,济南seo网站推广公司,销售管理系统的功能有哪些全流程 以下是一个更复杂、全流程的决策树和随机森林示例,不仅包括模型训练和预测,还涵盖了数据预处理、超参数调优以及模型评估的可视化。我们依旧使用鸢尾花数据集,并额外引入 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.cadmedia.cn/news/132.html

相关文章:

  • 网站 需求分析/app用户量排名
  • 2022年企业所得税税率表一览/北京seo优化诊断
  • 黄河勘测规划设计公司/星沙网站优化seo
  • 网站建设专业是干什么的/每日新闻简报
  • 怎样做个网站/我想做app推广怎么做
  • 柳州网站建设哪家便宜/个人博客seo
  • wordpress站长统计插件/seo产品优化推广
  • 手机上怎么设计广告图片/深圳seo优化电话
  • h5 css3 网站开发实例/私人做网站的流程
  • 电器网站制作价格/指数是什么意思
  • 做全景图有哪些网站/商品关键词怎么优化
  • 无锡工程建设监察支队网站/电商数据查询平台
  • 有模版之后怎么做网站/全网推广平台有哪些
  • 在家帮别人做网站赚钱/专业关键词优化平台
  • 对网页设计的认识/seo有哪些优化工具
  • 厦门网站设计一般要多久/重庆快速排名优化
  • 做鸭服务的网站或群/建站平台
  • 网站建设知识/竞价推广账户竞价托管
  • 北京建设网站公司推荐/百度一下就知道了官网榡
  • app运营一般多少钱一个月/长沙seo网站管理
  • 昆山专业网站建设公司/百度在线扫一扫
  • 天津建设安全协会网站/高级搜索指令
  • 斗门区建设局网站/seo深圳网络推广
  • 网站设计原型/网络平台销售
  • 什么网站做任务赚钱/网站之家查询
  • 企业网站名称怎么写/个人免费网站创建入口
  • iframe wordpress/windows优化大师兑换码
  • 微站和网站数据/seo是搜索引擎优化
  • 怎么做网站一张图/优网营销
  • 广州网络建站/关键词推广seo怎么优化