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

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

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

http://www.lryc.cn/news/2387787.html

相关文章:

  • 【ASR】基于分块非自回归模型的流式端到端语音识别
  • 国芯思辰|国产FRAM SF25C128助力监控系统高效低功耗解决方案,对标MB85RS128/FM25V01
  • 攻防世界逆向刷题笔记(新手模式9-1?)
  • 【golang】能否在遍历map的同时删除元素
  • 制作一款打飞机游戏58:子弹模式组合
  • 使用新一代达梦管理工具SQLark,高效处理 JSON/XML 数据!
  • Qt基础:数据容器类
  • Vue3监听对象数组属性变化方法
  • 深入了解PyTorch:起源、优势、发展与安装指南
  • DeepSeek智能对话助手项目
  • 浅谈Mysql的MVCC机制(RC与RR隔离级别)
  • uniapp-商城-72-shop(5-商品列表,购物车实现回顾)
  • 【git】 pull + rebase 或 pull + merge什么区别?
  • 1. 编程语言进化史与JavaScript
  • Vue3 中 Axios 深度整合指南:从基础到高级实践引言
  • MySQL#Select语句执行过程
  • hbuilder中h5转为小程序提交发布审核
  • 文档注释:删还是不删
  • 【数据结构】单链表练习
  • JVM 性能优化终极指南:全版本兼容、参数公式与场景实战
  • 分布式爬虫监控架构设计
  • MySQL的参数 innodb_force_recovery 详解
  • 学习vue3:跨组件通信(provide+inject)
  • Alibaba Sentinel 入门教程:从理论到实战
  • 2.3 TypeScript 非空断言操作符(后缀 !)详解
  • 【菜狗work前端】小程序加if判断时不及时刷新 vs Web
  • 01 NLP的发展历程和挑战
  • TCP 三次握手:详解与原理
  • LabVIEW累加器标签通道
  • 在 Unity 中,Start 方法直接设置 RectTransform 的位置,时出现问题,与预计位置不匹配。