DAY9 热力图和箱线图的绘制

浙大疏锦行
学会了绘制两个图:
热力图:表示每个特征之间的影响,颜色越深数值越大表示这两个特征的关系越紧密
箱线图:表示每个特征的数据分布情况
箱体(Box):
箱体的上下边界分别表示第一四分位数(Q1)和第三四分位数(Q3),即数据的25%和75%分位数。
箱体内的水平线表示中位数(Median),即数据的50%分位数。
须(Whiskers):
须的上下端点通常表示数据的最小值和最大值,但不包括异常值。
在这个图中,须的下端点接近0,上端点大约在200,000左右。
异常值(Outliers):
图中箱体外的圆点表示异常值,即显著偏离其他数据点的值。
在这个图中,可以看到许多异常值,这些值远高于第三四分位数(Q3)。
数据分布:
从图中可以看出,年收入的中位数较低,大部分数据集中在较低的收入范围内。
然而,存在一些高收入的异常值,这些值显著高于大多数数据点

# 首先走一遍完整的之前的流程
# 1. 读取数据
import pandas as pd
data  = pd.read_csv('data.csv')
# 2. 查看数据
data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7500 entries, 0 to 7499
Data columns (total 18 columns):#   Column                        Non-Null Count  Dtype  
---  ------                        --------------  -----  0   Id                            7500 non-null   int64  1   Home Ownership                7500 non-null   object 2   Annual Income                 5943 non-null   float643   Years in current job          7129 non-null   object 4   Tax Liens                     7500 non-null   float645   Number of Open Accounts       7500 non-null   float646   Years of Credit History       7500 non-null   float647   Maximum Open Credit           7500 non-null   float648   Number of Credit Problems     7500 non-null   float649   Months since last delinquent  3419 non-null   float6410  Bankruptcies                  7486 non-null   float6411  Purpose                       7500 non-null   object 12  Term                          7500 non-null   object 13  Current Loan Amount           7500 non-null   float6414  Current Credit Balance        7500 non-null   float6415  Monthly Debt                  7500 non-null   float6416  Credit Score                  5943 non-null   float6417  Credit Default                7500 non-null   int64  
dtypes: float64(12), int64(2), object(4)
memory usage: 1.0+ MB
data["Years in current job"].value_counts()
Years in current job
10+ years    2332
2 years       705
3 years       620
< 1 year      563
5 years       516
1 year        504
4 years       469
6 years       426
7 years       396
8 years       339
9 years       259
Name: count, dtype: int64
data["Home Ownership"].value_counts()
Home Ownership
Home Mortgage    3637
Rent             3204
Own Home          647
Have Mortgage      12
Name: count, dtype: int64
# 创建嵌套字典用于映射
mappings = {"Years in current job": {"10+ years": 10,"2 years": 2,"3 years": 3,"< 1 year": 0,"5 years": 5,"1 year": 1,"4 years": 4,"6 years": 6,"7 years": 7,"8 years": 8,"9 years": 9},"Home Ownership": {"Home Mortgage": 0,"Rent": 1,"Own Home": 2,"Have Mortgage": 3}
}
# 使用映射字典进行转换
data["Years in current job"] = data["Years in current job"].map(mappings["Years in current job"])
data["Home Ownership"] = data["Home Ownership"].map(mappings["Home Ownership"])
data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7500 entries, 0 to 7499
Data columns (total 18 columns):#   Column                        Non-Null Count  Dtype  
---  ------                        --------------  -----  0   Id                            7500 non-null   int64  1   Home Ownership                7500 non-null   int64  2   Annual Income                 5943 non-null   float643   Years in current job          7129 non-null   float644   Tax Liens                     7500 non-null   float645   Number of Open Accounts       7500 non-null   float646   Years of Credit History       7500 non-null   float647   Maximum Open Credit           7500 non-null   float648   Number of Credit Problems     7500 non-null   float649   Months since last delinquent  3419 non-null   float6410  Bankruptcies                  7486 non-null   float6411  Purpose                       7500 non-null   object 12  Term                          7500 non-null   object 13  Current Loan Amount           7500 non-null   float6414  Current Credit Balance        7500 non-null   float6415  Monthly Debt                  7500 non-null   float6416  Credit Score                  5943 non-null   float6417  Credit Default                7500 non-null   int64  
dtypes: float64(13), int64(3), object(2)
memory usage: 1.0+ MB
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt# 提取连续值特征
continuous_features = ['Annual Income', 'Years in current job', 'Tax Liens','Number of Open Accounts', 'Years of Credit History','Maximum Open Credit', 'Number of Credit Problems','Months since last delinquent', 'Bankruptcies','Current Loan Amount', 'Current Credit Balance', 'Monthly Debt','Credit Score'
]# 计算相关系数矩阵
correlation_matrix = data[continuous_features].corr()# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300# 绘制热力图
plt.figure(figsize=(12, 10))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', vmin=-1, vmax=1)
plt.title('Correlation Heatmap of Continuous Features')
plt.show()

在这里插入图片描述

import pandas as pd
import matplotlib.pyplot as plt# 定义要绘制的特征
features = ['Annual Income', 'Years in current job', 'Tax Liens', 'Number of Open Accounts']
# 随便选的4个特征,不要在意对不对# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300# 创建一个包含 2 行 2 列的子图布局
fig, axes = plt.subplots(2, 2, figsize=(12, 8))# 手动指定特征索引进行绘图,仔细观察下这个坐标
i = 0
feature = features[i]
axes[0, 0].boxplot(data[feature].dropna())
axes[0, 0].set_title(f'Boxplot of {feature}')
axes[0, 0].set_ylabel(feature)i = 1
feature = features[i]
axes[0, 1].boxplot(data[feature].dropna())
axes[0, 1].set_title(f'Boxplot of {feature}')
axes[0, 1].set_ylabel(feature)i = 2
feature = features[i]
axes[1, 0].boxplot(data[feature].dropna())
axes[1, 0].set_title(f'Boxplot of {feature}')
axes[1, 0].set_ylabel(feature)i = 3
feature = features[i]
axes[1, 1].boxplot(data[feature].dropna())
axes[1, 1].set_title(f'Boxplot of {feature}')
axes[1, 1].set_ylabel(feature)# 调整子图之间的间距
plt.tight_layout()# 显示图形
plt.show()

在这里插入图片描述


# 定义要绘制的特征
features = ['Annual Income', 'Years in current job', 'Tax Liens', 'Number of Open Accounts']# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300# 创建一个包含 2 行 2 列的子图布局,其中
fig, axes = plt.subplots(2, 2, figsize=(12, 8))#返回一个Figure对象和Axes对象
# 这里的axes是一个二维数组,包含2行2列的子图
# 这里的fig是一个Figure对象,表示整个图形窗口
# 你可以把fig想象成一个画布,axes就是在这个画布上画的图形# 遍历特征并绘制箱线图
for i, feature in enumerate(features):row = i // 2col = i % 2axes[row, col].boxplot(data[feature].dropna())axes[row, col].set_title(f'Boxplot of {feature}')axes[row, col].set_ylabel(feature)# 调整子图之间的间距
plt.tight_layout()# 显示图形
plt.show()
# 定义要绘制的特征
features = ['Annual Income', 'Years in current job', 'Tax Liens', 'Number of Open Accounts']# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300# 创建一个包含 2 行 2 列的子图布局,其中
fig, axes = plt.subplots(2, 2, figsize=(12, 8))#返回一个Figure对象和Axes对象
# 这里的axes是一个二维数组,包含2行2列的子图
# 这里的fig是一个Figure对象,表示整个图形窗口
# 你可以把fig想象成一个画布,axes就是在这个画布上画的图形# 遍历特征并绘制箱线图
for i, feature in enumerate(features):row = i // 2col = i % 2axes[row, col].boxplot(data[feature].dropna())axes[row, col].set_title(f'Boxplot of {feature}')axes[row, col].set_ylabel(feature)# 调整子图之间的间距
plt.tight_layout()# 显示图形
plt.show()

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/347.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

VUE项目部署IIS服务器手册

IIS部署Vue项目完整手册 &#x1f4cb; 目录 基础概念准备工作Vue项目构建web.config详解IIS部署步骤不同场景配置常见问题实用配置模板 基础概念 Vue单页应用&#xff08;SPA&#xff09;工作原理 重要理解&#xff1a;Vue项目是单页应用&#xff0c;这意味着&#xff1a;…

【Day38】

DAY 38 Dataset和Dataloader类 对应5. 27作业 知识点回顾&#xff1a; Dataset类的__getitem__和__len__方法&#xff08;本质是python的特殊方法&#xff09;Dataloader类minist手写数据集的了解 作业&#xff1a;了解下cifar数据集&#xff0c;尝试获取其中一张图片 import …

怎么查找idea插件的下载位置,并更改

长期使用 IntelliJ IDEA 时&#xff0c;默认存储在 C 盘的配置文件会持续生成大量缓存和日志文件&#xff0c;可能导致系统盘空间不足。通过修改默认配置文件存储位置&#xff0c;可以有效释放 C 盘空间并提升系统性能。 1&#xff0c;先找到自己idea的下载目录&#xff0c;再打…

什么是 WPF 技术?什么是 WPF 样式?下载、安装、配置、基本语法简介教程

什么是 WPF 技术&#xff1f;什么是 WPF 样式&#xff1f;下载、安装、配置、基本语法简介教程 摘要 WPF教程、WPF开发、.NET 8 WPF、Visual Studio 2022 WPF、WPF下载、WPF安装、WPF配置、WPF样式、WPF样式详解、XAML语法、XAML基础、MVVM架构、数据绑定、依赖属性、资源字典…

鸿蒙OSUniApp 开发的多图浏览器组件#三方框架 #Uniapp

使用 UniApp 开发的多图浏览器组件 在移动应用开发中&#xff0c;图片浏览器是非常常见且实用的功能&#xff0c;尤其是在社交、资讯、电商等场景下&#xff0c;用户对多图浏览体验的要求越来越高。随着 HarmonyOS&#xff08;鸿蒙&#xff09;生态的不断壮大&#xff0c;开发…

Flink流处理基础概论

文章目录 引言Flink基本概述传统数据架构的不足Dataflow中的几大基本概念Dataflow流式处理宏观流程数据并行和任务并行的区别Flink中几种数据传播策略Flink中事件的延迟和吞吐事件延迟事件的吞吐如何更好的理解事件的延迟和吞吐flink数据流的几种操作输入输出转换操作滚动聚合窗…

华为云Flexus+DeepSeek征文 | Dify-LLM平台一键部署教程及问题解决指南

作者简介 我是摘星&#xff0c;一名专注于云计算和AI技术的开发者。本次通过华为云MaaS平台体验DeepSeek系列模型&#xff0c;将实际使用经验分享给大家&#xff0c;希望能帮助开发者快速掌握华为云AI服务的核心能力。 目录 1. 前言 2. 准备工作 2.1 注册华为云账号 2.2 确…

第九届水动力学与能源电力系统国际学术会议(HEEPS 2025)

水动力学与电力系统融合&#xff1a;全球能源转型的新引擎 随着全球能源转型加速&#xff0c;水动力学与电力系统的融合正成为破解可持续发展难题的关键。 新能源接入的挑战与机遇 传统电力系统像一条单向行驶的高速公路&#xff0c;而风电、光伏等间歇性能源的加入&#xf…

鸿蒙仓颉开发语言实战教程:自定义tabbar

大家周末好呀&#xff0c;今天继续分享仓颉语言开发商城应用的实战教程&#xff0c;今天要做的是tabbar。 大家都知道ArkTs有Tabs和TabContent容器&#xff0c;能够实现上图的样式&#xff0c;满足基本的使用需求。而仓颉就不同了&#xff0c;它虽然也有这两个组件&#xff0c;…

腾讯2025年校招笔试真题手撕(三)

一、题目 今天正在进行赛车车队选拔&#xff0c;每一辆赛车都有一个不可以改变的速度。现在需要选取速度差距在10以内的车队&#xff08;车队中速度的最大值减去最小值不大于10&#xff09;&#xff0c;用于迎宾。车队的选拔按照的是人越多越好的原则&#xff0c;给出n辆车的速…

腾讯2025年校招笔试真题手撕(二)

一、题目 最近以比特币为代表的数字货币市场非常动荡&#xff0c;聪明的小明打算用马尔科夫链来建模股市。如图所示&#xff0c;该模型有三种状态&#xff1a;“行情稳定”&#xff0c;“行情大跌”以及“行情大涨”。每一个状态都以一定的概率转化到下一个状态。比如&#xf…

华为2025年校招笔试真题手撕教程(一)

一、题目 输入&#xff1a; 第一行为记录的版本迭代关系个数N&#xff0c;范围是[1&#xff0c;100000]; 第二行到第N1行&#xff1a;每行包含两个字符串&#xff0c;第一个字符串为当前版本&#xff0c;第二个字符串为前序版本&#xff0c;用空格隔开。字符串包含字符个数为…