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

Python Day34 学习

今日内容

通过“心脏病数据集”对之前的内容进行复习,再进行新内容“元组和OS模块”的学习。

机器学习模型建模和评估(先不考虑调参)

基于之前已经预处理过的心脏病数据集

划分数据值

模型训练与模型评估

# 随机森林
rf_model = RandomForestClassifier(random_state=42)
rf_model.fit(X_train, y_train)
rf_pred = rf_model.predict(X_test)print("\n随机森林 分类报告:")
print(classification_report(y_test, rf_pred))
print("随机森林 混淆矩阵:")
print(confusion_matrix(y_test, rf_pred))rf_accuracy = accuracy_score(y_test, rf_pred)
rf_precision = precision_score(y_test, rf_pred)
rf_recall = recall_score(y_test, rf_pred)
rf_f1 = f1_score(y_test, rf_pred)
print("随机森林 模型评估指标:")
print(f"准确率: {rf_accuracy:.4f}")
print(f"精确率: {rf_precision:.4f}")
print(f"召回率: {rf_recall:.4f}")
print(f"F1 值: {rf_f1:.4f}")

打印结果

元组和OS模块

Q1. 什么是“元组”?

Q2. 元组的创建

my_tuple1 = (1, 2, 3)
my_tuple2 = ('a', 'b', 'c')
my_tuple3 = (1, 'hello', 3.14, [4, 5]) # 可以包含不同类型的元素
print(my_tuple1)
print(my_tuple2)
print(my_tuple3)打印结果:
(1, 2, 3)
('a', 'b', 'c')
(1, 'hello', 3.14, [4, 5])
# 可以省略括号
my_tuple4 = 10, 20, 'thirty' # 逗号是关键
print(my_tuple4)
print(type(my_tuple4)) # 看看它的类型打印结果:
(10, 20, 'thirty')
<class 'tuple'>
# 创建空元组
empty_tuple = ()
# 或者使用 tuple() 函数
empty_tuple2 = tuple()
print(empty_tuple)
print(empty_tuple2)打印结果
()
()

Q3. 元组的常见用法

元组的索引

my_tuple = ('P', 'y', 't', 'h', 'o', 'n')
print(my_tuple[0])  # 第一个元素
print(my_tuple[2])  # 第三个元素
print(my_tuple[-1]) # 最后一个元素打印结果:
P
t
n

元组的切片

# 元组的切片
my_tuple = (0, 1, 2, 3, 4, 5)
print(my_tuple[1:4])  # 从索引 1 到 3 (不包括 4)
print(my_tuple[:3])   # 从开头到索引 2
print(my_tuple[3:])   # 从索引 3 到结尾
print(my_tuple[::2])  # 每隔一个元素取一个打印结果:
(1, 2, 3)
(0, 1, 2)
(3, 4, 5)
(0, 2, 4)

元组的长度获取

my_tuple = (1, 2, 3)
print(len(my_tuple))打印结果:
3

今日学习到这里,从今天开始到之后的学习,将复习占比增加,新内容少量。循序渐进,继续加油!!!@浙大疏锦行

相关文章:

  • AXI3、AXI4 和 AXI5 的详细差异对比
  • Python条件语句完全指南:从if-else到模式匹配
  • NAT的映射类型详解:从基础到高级应用
  • Halcon联合QT ROI绘制
  • Spring Boot事务失效场景及解决方案
  • 超大规模模型训练中的 ZeRO 优化器与混合精度通信压缩技术
  • 【JavaSE】枚举和注解学习笔记
  • 编程日志5.27
  • AI情感陪伴在医疗领域的核心应用潜力
  • 彻底理解一个知识点的具体步骤
  • 西门子-队列
  • 第1章 Redis 概述
  • 【C++】类和对象(上)
  • 每日算法 -【Swift 算法】实现回文数判断!
  • endnote2025安装教程以及激活文件
  • 软考 系统架构设计师系列知识点之杂项集萃(77)
  • 修复SSH 服务支持弱加密算法漏洞
  • 【实战】Tennis-Tracking 安装与运行完整教程(含 CUDA/TF 报错解决)
  • C++优先队列(priority_queue)使用详解
  • SQL 查询慢的常见原因分析
  • 阿里云网站备案流程/自助建站系统源码
  • 做网站需要实名认证吗/百度热搜榜排行
  • 品牌网站设计企业服务/百度浏览器主页网址
  • 广告公司网站首页设计页面/百度竞价排名正确解释
  • 潍坊企业网站建设/新闻头条最新消息国家大事
  • 引流软件下载站/百度收录提交工具