add decisiontree regression problem details
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
- class_name: 友情鏈接
|
||||
- class_name: 友情链接
|
||||
class_desc: 那些人,那些事
|
||||
link_list:
|
||||
- name: Hexo
|
||||
link: https://hexo.io/zh-tw/
|
||||
link: https://hexo.io/zh-cn/
|
||||
avatar: https://d33wubrfki0l68.cloudfront.net/6657ba50e702d84afb32fe846bed54fba1a77add/827ae/logo.svg
|
||||
descr: 快速、簡單且強大的網誌框架
|
||||
descr: 快速、简单且強大的文档框架
|
||||
|
||||
- class_name: 網站
|
||||
class_desc: 值得推薦的網站
|
||||
- class_name: 网站
|
||||
class_desc: 值得推荐的网站
|
||||
link_list:
|
||||
- name: Youtube
|
||||
link: https://www.youtube.com/
|
||||
avatar: https://i.loli.net/2020/05/14/9ZkGg8v3azHJfM1.png
|
||||
descr: 視頻網站
|
||||
descr: 视频网站
|
||||
- name: Weibo
|
||||
link: https://www.weibo.com/
|
||||
avatar: https://i.loli.net/2020/05/14/TLJBum386vcnI1P.png
|
||||
descr: 中國最大社交分享平台
|
||||
descr: 中国最大社交分享平台
|
||||
- name: Twitter
|
||||
link: https://twitter.com/
|
||||
avatar: https://i.loli.net/2020/05/14/5VyHPQqR6LWF39a.png
|
||||
|
||||
149
source/_posts/machinelearning/decisiontree.md
Normal file
149
source/_posts/machinelearning/decisiontree.md
Normal file
@@ -0,0 +1,149 @@
|
||||
---
|
||||
title: 决策树算法
|
||||
tags: decisiontree
|
||||
categories: machinelearning
|
||||
abbrlink: 95
|
||||
date: 2025-01-24 12:39:59
|
||||
---
|
||||
|
||||
### C4.5
|
||||
C4.5 是一种用于生成决策树的算法,不再使用信息增益,而是使用信息增益比,来避免偏向于选择取值较多的特征。信息增益比是信息增益与特征的熵的比值。
|
||||
### ID3
|
||||
D3 是一种基于信息增益(Information Gain)的决策树算法
|
||||
### Cart
|
||||
CART(分类与回归树)一种决策树算法,,CART 使用 **二叉树结构**,即每个节点只能有两个子节点。
|
||||
### cart剪枝
|
||||
CART 决策树的剪枝方法分为 **预剪枝**(Pre-pruning)和 **后剪枝**(Post-pruning)两种:
|
||||
#### **预剪枝**:
|
||||
预剪枝是在构建决策树时就决定是否停止进一步划分某个节点。主要通过以下标准来控制:
|
||||
- 当某个节点的样本数小于某个阈值时,不再继续划分。
|
||||
- 当某个节点的 Gini 不纯度小于某个阈值时,不再继续划分。
|
||||
预剪枝的优点是能够减少计算量,但缺点是可能会导致模型不够复杂,从而产生欠拟合。
|
||||
#### **后剪枝**:
|
||||
后剪枝是在决策树完全构建出来之后,对树进行修剪。具体过程如下:
|
||||
- 构建完整的决策树。
|
||||
- 从叶子节点开始,逐渐向上遍历树的每个节点。
|
||||
- 对每个节点进行判断,是否合适剪去该节点及其子树。如果剪去该子树后,模型的性能没有显著下降,就可以剪枝。
|
||||
后剪枝通过避免过度拟合来提高模型的泛化能力,但其计算开销较大。
|
||||
### 特征工程(特征提取)
|
||||
- **字典特征提取**
|
||||
主要用于处理包含键值对(key-value pairs)的数据结构
|
||||
```python
|
||||
from sklearn.feature_extraction import DictVectorizer
|
||||
# 字典特征提取
|
||||
data = [
|
||||
{'city': 'beijing', 'temperature': 100},
|
||||
{'city': 'shanghai', 'temperature': 95},
|
||||
{'city': 'guangzhou', 'temperature': 98}
|
||||
]
|
||||
transfer = DictVectorizer(sparse=False)
|
||||
new_data = transfer.fit_transform(data)
|
||||
print(transfer.feature_names_)
|
||||
print(new_data)
|
||||
```
|
||||
- **文本特征提取**
|
||||
主要用于将文本数据(如句子、段落、文章等)转换成数值型特征。这对于文本分类、信息检索等任务非常重要。
|
||||
```python
|
||||
from sklearn.feature_extraction.text import CountVectorizer
|
||||
# 示例文本数据
|
||||
data = [
|
||||
"I love programming",
|
||||
"Python is great",
|
||||
"I love machine learning"
|
||||
]
|
||||
# 创建 CountVectorizer 对象
|
||||
transfer = CountVectorizer()
|
||||
# 将文本数据转换为特征向量
|
||||
new_data = transfer.fit_transform(data)
|
||||
# 输出特征名称
|
||||
print("Feature Names:", transfer.get_feature_names_out())
|
||||
# 输出转换后的特征矩阵
|
||||
print("Transformed Data:", new_data.toarray())
|
||||
```
|
||||
- 文本特征提取(中文文本)
|
||||
```python
|
||||
from sklearn.feature_extraction.text import CountVectorizer
|
||||
import jieba
|
||||
# 中文文本数据(大于20个字)
|
||||
data = [
|
||||
"我热爱编程,学习编程语言是一件非常有趣的事情,它能够提升我们解决问题的能力,编程让我变得更加有创意。",
|
||||
"Python语言是一门非常强大的编程语言,具有简洁的语法和丰富的库,可以帮助开发者更高效地完成任务。",
|
||||
"机器学习是一项非常有前途的技术,它能够让计算机从数据中自动学习,逐步提高模型的精确度,解决实际问题。"
|
||||
|
||||
]
|
||||
# 使用jieba分词
|
||||
text_list = []
|
||||
for line in data:
|
||||
text_list.append(" ".join(list(jieba.cut(line))))
|
||||
# 创建 CountVectorizer 对象
|
||||
transfer = CountVectorizer()
|
||||
# 将文本数据转换为特征向量
|
||||
new_data = transfer.fit_transform(text_list)
|
||||
# 输出特征名称
|
||||
print("Feature Names:", transfer.get_feature_names_out())
|
||||
# 输出转换后的特征矩阵
|
||||
print("Transformed Data:", new_data.toarray())
|
||||
```
|
||||
### tf-idf
|
||||
> 词频 * 逆文档频率
|
||||
```python
|
||||
# tfi-df
|
||||
from sklearn.feature_extraction.text import TfidfVectorizer
|
||||
import jieba
|
||||
data=["一种还是一种今天很残酷,明天更残酷,后天很美好,但绝对大部分是死在明天晚上,所以每个人不要放弃今天。",
|
||||
"我们看到的从很远星系来的光是在几百万年之前发出的,这样当我们看到宇宙时,我们是在看它的过去",
|
||||
"如果只用一种方式了解某样事物,你就不会真正了解它。了解事物真正含义的秘密取决于如何将其与我们所了解的事物相联系"]
|
||||
list = []
|
||||
for item in data:
|
||||
list.append(" ".join(jieba.cut(item)))
|
||||
transfer = TfidfVectorizer()
|
||||
new_data = transfer.fit_transform(list)
|
||||
print(f"特征名字:\n{transfer.get_feature_names_out()}")
|
||||
|
||||
print(f"转换后的特征矩阵:\n{ new_data.toarray()}")
|
||||
print(f"转换后的数据:\n{new_data}")
|
||||
```
|
||||
|
||||
|
||||
### 回归决策树
|
||||
[Webgraphviz](http://webgraphviz.com/)
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.linear_model import LinearRegression
|
||||
from sklearn.tree import DecisionTreeRegressor
|
||||
from matplotlib import rcParams
|
||||
|
||||
# 设置matplotlib使用的字体为SimHei(黑体)
|
||||
rcParams['font.sans-serif'] = ['SimHei'] # 也可以使用 'Microsoft YaHei'
|
||||
rcParams['axes.unicode_minus'] = False # 正常显示负号
|
||||
x = np.array(list(range(1,11))).reshape(-1,1)
|
||||
y = ([5.56,5.70,5.91,6.40,6.80,7.05,8.90,8.70,9.00,9.05])
|
||||
|
||||
m1 = DecisionTreeRegressor(max_depth=1)
|
||||
m2 = DecisionTreeRegressor(max_depth=3)
|
||||
m3 = DecisionTreeRegressor()
|
||||
|
||||
# 模型训练
|
||||
m1.fit(x,y)
|
||||
m2.fit(x,y)
|
||||
m3.fit(x,y)
|
||||
|
||||
# 模型预测
|
||||
x_test = np.arange(0,10,0.01).reshape(-1,1)
|
||||
y_1 = m1.predict(x_test)
|
||||
y_2 = m2.predict(x_test)
|
||||
y_3 = m3.predict(x_test)
|
||||
|
||||
# 结果展示
|
||||
plt.figure(figsize=(10,6),dpi=100)
|
||||
plt.scatter(x,y ,label = "data")
|
||||
plt.plot(x_test,y_1,label = "max_depth=1")
|
||||
plt.plot(x_test,y_2,label = "max_depth=3")
|
||||
plt.plot(x_test,y_3,label = "linearregression")
|
||||
plt.xlabel("数据")
|
||||
plt.ylabel("预测值")
|
||||
plt.legend()
|
||||
plt.show()
|
||||
```
|
||||
@@ -1,6 +1,7 @@
|
||||
---
|
||||
title: k近邻算法(K-Nearest Neighbors)KNN
|
||||
tags: machinelearning
|
||||
tags: KNN
|
||||
categories: machinelearning
|
||||
abbrlink: 29139
|
||||
mathjax: true
|
||||
date: 2025-01-13 17:20:59
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
---
|
||||
title: 线性回归
|
||||
tags: linear-regression
|
||||
categories: machinelearning
|
||||
mathjax: true
|
||||
abbrlink: 52662
|
||||
date: 2025-01-19 16:46:51
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
---
|
||||
title: 逻辑回归
|
||||
tags: logistic-regression
|
||||
categories: machinelearning
|
||||
mathjax: true
|
||||
abbrlink: 60504
|
||||
date: 2025-01-20 15:30:08
|
||||
|
||||
Reference in New Issue
Block a user