DataWhale-(scikit-learn教程)-Task04(决策树)-202112

一、决策树基本算法
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
二、基于sklearn的算法实现
https://github.com/datawhalechina/machine-learning-toy-code/tree/main/ml-with-sklearn

import seaborn as sns
from pandas import plotting
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import tree

data = load_iris() 
# 转换成.DataFrame形式
df = pd.DataFrame(data.data, columns = data.feature_names)
# 添加品种列
df['Species'] = data.target
# 查看数据集信息
print(f"数据集信息:\n{df.info()}")
# 查看前5条数据
print(f"前5条数据:\n{df.head()}")
# 查看各特征列的摘要信息
df.describe()
# 设置颜色主题
antV = ['#1890FF', '#2FC25B', '#FACC14', '#223273', '#8543E0', '#13C2C2', '#3436c7', '#F04864'] 
# 绘制violinplot
f, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True)
sns.despine(left=True) # 删除上方和右方坐标轴上不需要的边框,这在matplotlib中是无法通过参数实现的
sns.violinplot(x='Species', y=df.columns[0], data=df, palette=antV, ax=axes[0, 0])
sns.violinplot(x='Species', y=df.columns[1], data=df, palette=antV, ax=axes[0, 1])
sns.violinplot(x='Species', y=df.columns[2], data=df, palette=antV, ax=axes[1, 0])
sns.violinplot(x='Species', y=df.columns[3], data=df, palette=antV, ax=axes[1, 1])
plt.show()
# 绘制pointplot
f, axes = plt.subplots(2, 2, figsize=(8, 6), sharex=True)
sns.despine(left=True)
sns.pointplot(x='Species', y=df.columns[0], data=df, color=antV[1], ax=axes[0, 0])
sns.pointplot(x='Species', y=df.columns[1], data=df, color=antV[1], ax=axes[0, 1])
sns.pointplot(x='Species', y=df.columns[2], data=df, color=antV[1], ax=axes[1, 0])
sns.pointplot(x='Species', y=df.columns[3], data=df, color=antV[1], ax=axes[1, 1])
plt.show()
# g = sns.pairplot(data=df, palette=antV, hue= 'Species')
# 安德鲁曲线
plt.subplots(figsize = (8,6))
plotting.andrews_curves(df, 'Species', colormap='cool')

plt.show()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

# 加载数据集
data = load_iris() 
# 转换成.DataFrame形式
df = pd.DataFrame(data.data, columns = data.feature_names)
# 添加品种列
df['Species'] = data.target

# 用数值替代品种名作为标签
target = np.unique(data.target)
target_names = np.unique(data.target_names)
targets = dict(zip(target, target_names))
df['Species'] = df['Species'].replace(targets)

# 提取数据和标签
X = df.drop(columns="Species")
y = df["Species"]
feature_names = X.columns
labels = y.unique()

X_train, test_x, y_train, test_lab = train_test_split(X,y,
                                                 test_size = 0.4,
                                                 random_state = 42)
model = DecisionTreeClassifier(max_depth =3, random_state = 42)
model.fit(X_train, y_train) 
# 以文字形式输出树     
text_representation = tree.export_text(model)
print(text_representation)
# 用图片画出
plt.figure(figsize=(30,10), facecolor ='g') #
a = tree.plot_tree(model,
                   feature_names = feature_names,
                   class_names = labels,
                   rounded = True,
                   filled = True,
                   fontsize=14)
plt.show()             

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Scikit-learn是一个基于Python的机器学习库,它提供了丰富的机器学习算法和工具,用于数据预处理、模型选择、模型训练和评估等任务。你可以通过安装Scikit-learn来开始学习和使用它。 Scikit-learn的优势包括简单易用的API、广泛的机器学习算法支持、丰富的文档和教程资源、适用于各种应用场景等。然而,由于它依赖于Python,相对于C或Java等语言,其运行效率较低。此外,Scikit-learn尚未完全支持大规模深度学习模型。 关于Scikit-learn教程,你可以参考《Python机器学习:Scikit-learn入门指南》这本书。该教程包含了Scikit-learn的简介、安装方法、数据准备、模型训练、机器学习算法、实战案例等内容。你可以按照教程的步骤,逐步学习和实践Scikit-learn的使用。 教程的内容包括了Scikit-learn的安装、数据准备、模型训练、机器学习算法和实战案例等多个方面。你可以先了解Scikit-learn的基本概念和优势,然后学习如何安装和使用它,接着学习数据准备的方法,包括特征选择和数据清洗等。然后,你可以学习模型训练的步骤和常用的机器学习算法,如线性回归、逻辑回归、决策树、支持向量机和随机森林等。最后,你可以通过实战案例来进一步巩固所学知识,并学习Scikit-learn的进阶内容,如流水线、模型调参和特征选择等。 请注意,Scikit-learn虽然是一个功能强大的机器学习库,但它并不支持大规模深度学习模型。如果你对深度学习有兴趣,可能需要使用其他专门的深度学习库,如TensorFlow或PyTorch。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [机器学习神器Scikit-Learn保姆级入门教程](https://blog.csdn.net/SeizeeveryDay/article/details/122531826)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python机器学习:Scikit-learn入门指南](https://blog.csdn.net/u010349629/article/details/130663015)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值