人工智能知识分享第八天-机器学习_泰坦尼克生存预估&线性回归和决策树回归对比案例

泰坦尼克生存预估案例

python 复制代码
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree

titanic_df = pd.read_csv('./data/train.csv')
titanic_df.info()

x = titanic_df[['Pclass', 'Sex', 'Age']]
y = titanic_df['Survived']

# x['Age'].fillna(x['Age'].mean(), inplace=True)
x['Age'] = x['Age'].fillna(x['Age'].mean())
x.info()

print(len(x), len(y))
# titanic_df.info()

x = pd.get_dummies(x)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=22)

estimator = DecisionTreeClassifier()
estimator.fit(x_train, y_train)

y_predict = estimator.predict(x_test)

print('预测结果为:\n', y_predict)
print('准确率: \n', estimator.score(x_test, y_test))
print(f'分类评估报告: \n {classification_report(y_test, y_predict, target_names=["Died", "Survivor"])}')
plt.figure(figsize=(70, 45))
plot_tree(estimator, filled=True, max_depth=10)
plt.savefig("./data/titanic_tree.png")
# 7.3 具体的绘制.
plt.show()

运行结果

线性回归和决策树回归对比案例

python 复制代码
"""
案例:
    演示线性回归 和 回归决策树对比.

结论:
    回归类问题, 既能用线性回归, 也能用决策树回归. 优先使用 线性回归, 因为 决策树回归可能会导致 过拟合.
"""

# 导包.
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeRegressor      # 回归决策树
from sklearn.linear_model import LinearRegression   # 线性回归
import matplotlib.pyplot as plt                     # 绘图


# 1. 获取数据.
x = np.array(list(range(1,11))).reshape(-1, 1)
y = np.array([5.56, 5.70, 5.91, 6.40, 6.80, 7.05, 8.90, 8.70, 9.00, 9.05])
print(x)
print(y)

# 2. 创建线性回归 和 决策树回归模型.
estimator1 = LinearRegression()                    # 线性回归
estimator2 = DecisionTreeRegressor(max_depth=1)    # 回归决策树, 层数=1
estimator3 = DecisionTreeRegressor(max_depth=3)    # 回归决策树, 层数=3

# 3. 训练模型.
estimator1.fit(x, y)
estimator2.fit(x, y)
estimator3.fit(x, y)

# 4. 准备测试数据, 用于测试.
# 起始, 结束, 步长.
x_test = np.arange(0.0, 10.0, 0.1).reshape(-1, 1)
print(x_test)

# 5. 模型预测.
y_predict1 = estimator1.predict(x_test)
y_predict2 = estimator2.predict(x_test)
y_predict3 = estimator3.predict(x_test)

# 6. 绘图
plt.figure(figsize=(10, 5))

# 散点图(原始的坐标)
plt.scatter(x, y, color='gray', label='data')
# 线性回归的预测结果
plt.plot(x_test, y_predict1, color='r', label='liner regression')
# 回归决策树, 层数=1
plt.plot(x_test, y_predict2, color='b', label='max depth=1')
# 回归决策树, 层数=3
plt.plot(x_test, y_predict3, color='g', label='max depth=3')
# 显示图例.
plt.legend()
# 设置x轴标签.
plt.xlabel('data')
# 设置y轴标签.
plt.ylabel('target')
# 设置标题
plt.title('Decision Tree Regression')

# 显示图片
plt.show()

运行结果

坚持分享 共同进步

相关推荐
deephub3 分钟前
AI代理性能提升实战:LangChain+LangGraph内存管理与上下文优化完整指南
人工智能·深度学习·神经网络·langchain·大语言模型·rag
EulerBlind4 分钟前
【运维】SGLang 安装指南
运维·人工智能·语言模型
心之语歌7 分钟前
Spring AI MCP 客户端
人工智能·spring·github
go546315846541 分钟前
基于深度学习的食管癌右喉返神经旁淋巴结预测系统研究
图像处理·人工智能·深度学习·神经网络·算法
Blossom.11843 分钟前
基于深度学习的图像分类:使用Capsule Networks实现高效分类
人工智能·python·深度学习·神经网络·机器学习·分类·数据挖掘
想变成树袋熊1 小时前
【自用】NLP算法面经(6)
人工智能·算法·自然语言处理
格林威2 小时前
Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现沙滩小人检测识别(C#代码UI界面版)
人工智能·深度学习·数码相机·yolo·计算机视觉
checkcheckck2 小时前
spring ai 适配 流式回答、mcp、milvus向量数据库、rag、聊天会话记忆
人工智能
Microvision维视智造2 小时前
从“人工眼”到‘智能眼’:EZ-Vision视觉系统如何重构生产线视觉检测精度?
图像处理·人工智能·重构·视觉检测
巫婆理发2222 小时前
神经网络(多层感知机)(第二课第二周)
人工智能·深度学习·神经网络