1、先查看数据看哪些不是数字形式,然后value_counts看非数字特征内容统计,决定怎样把其换成数字(字典映射)
import pandas as pd
data = pd.read_csv('data.csv')
# 查看数据
data.info()
data.head(5)
# 把years in current job列和Home Ownership 列转化为数字
# 先查看内容
data["Years in current job"].value_counts()
data["Home Ownership"].value_counts()
# 创建嵌套字典用于映射
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()
2、画图:
先找到连续特征,然后计算相关系数矩阵,图片清晰度,定义图大小,之后seaborn画图,最后写标题输出。
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()
data[continuous_features]
:从 DataFrame 中筛选出连续特征列。corr()
:默认计算 Pearson 相关系数(取值范围 [-1, 1]
):
1
:完全正相关(两变量同增同减)。-1
:完全负相关(一增一减)。0
:无线性相关关系。'figure.dpi'
来访问并修改 matplotlib 配置字典中的对应值。correlation_matrix
:输入的相关系数矩阵。annot=True
:在每个单元格中显示相关系数数值。cmap='coolwarm'
:使用冷暖色调的配色方案(蓝→白→红),负数为蓝色,正数为红色。vmin=-1, vmax=1
:设置颜色映射的最小值和最大值,确保 0 对应白色。1
(变量自身完全相关)。Income
和 Spending Score
可能正相关。Age
和 Spending Score
可能负相关。Default
)相关性最强的特征。1. 导入库
import pandas as pd
import matplotlib.pyplot as plt
2. 定义特征列表
features = ['Annual Income', 'Years in current job', 'Tax Liens', 'Number of Open Accounts']
# 随便选的4个特征,不要在意对不对
3. 设置图片清晰度
plt.rcParams['figure.dpi'] = 300
4. 创建子图布局
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
plt.subplots(2, 2, figsize=(12, 8))
:创建一个 2 行 2 列的子图网格(总共 4 个子图,对应 4 个特征),返回两个值,一个给fig,一个给axes:
fig
:Figure
对象(画布),可以理解成 “一整块画板”,用来承载所有子图、标题、保存图片等全局操作。axes
: 2x2
的数组,通过索引 [行号, 列号]
就能精准选中子图,然后在上面画箱线图、加标题、标签等。Axes
对象(或 Axes
数组),可以理解成 “画板上的小画布”,每个 Axes
对应一个子图。figsize=(12, 8)
:设置整个画布的尺寸,宽 12 英寸,高 8 英寸。5.第一个子图(Annual Income)
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 = 0
:取 features
列表的第 0 个元素(即 'Annual Income'
)。feature = features[i]
:把特征名存到 feature
变量,方便复用。axes[0, 0].boxplot(data[feature].dropna())
:在第 1 行第 1 列的子图(axes[0, 0]
)上,绘制 data
中 feature
列的箱线图,dropna()
是去掉该特征列的缺失值(避免绘图报错)。axes[0, 0].set_title(f'Boxplot of {feature}')
:给这个子图加标题,显示 “Boxplot of 特征名”。axes[0, 0].set_ylabel(feature)
:设置 Y 轴标签为特征名,说明 Y 轴数据的含义。6. 第二个子图(Years in current job)
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)
7. 第三个子图(Tax Liens)
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)
8.第四个子图(Number of Open Accounts)
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)
6. 调整子图间距
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))
# 使用 for 循环遍历特征
for i in range(len(features)): #len()得4,又经range得0-3四个数字
row = i // 2
col = i % 2
# 绘制箱线图
feature = features[i]
axes[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()
注:
axes.boxplot()
:Matplotlib 的底层绘图函数,直接在指定的子图轴上绘制箱线图sns.boxplot()
:Seaborn 的高级接口,基于 Matplotlib 封装,默认添加更美观的样式和统计信息axes[row, col]
)绘制多个箱线图,这是 Matplotlib 的标准用法。Seaborn 的sns.boxplot
更适合快速绘制单个箱线图或按类别分组的箱线图。和二比,仅把画第1234图和成了一个循环:
for i in range(len(features)):
row = i // 2 # 计算行索引(0或1)
col = i % 2 # 计算列索引(0或1)
feature = features[i]
axes[row, col].boxplot(data[feature].dropna())
axes[row, col].set_title(f'Boxplot of {feature}')
axes[row, col].set_ylabel(feature)
循环逻辑:
i
从 0 到 3,对应 4 个特征。row
:通过整数除法 i // 2
确定(0→第 1 行,1→第 2 行)。col
:通过取余运算 i % 2
确定(0→第 1 列,1→第 2 列)。子图绘制:
feature
(如 'Annual Income'
)。axes[row, col]
位置绘制该特征的箱线图,过滤缺失值。'Boxplot of 特征名'
。补: 将连续的整数索引(如 0,1,2,3)转换为对应的二维坐标(行号,列号)。
特征索引 i |
行号 i // 2 |
列号 i % 2 |
对应子图位置 |
---|---|---|---|
0 | 0 // 2 = 0 | 0 % 2 = 0 | 第 1 行第 1 列 |
1 | 1 // 2 = 0 | 1 % 2 = 1 | 第 1 行第 2 列 |
2 | 2 // 2 = 1 | 2 % 2 = 0 | 第 2 行第 1 列 |
3 | 3 // 2 = 1 | 3 % 2 = 1 | 第 2 行第 2 列 |
如果网格是 n
行 m
列,线性索引 i
到二维坐标 (row, col)
的映射公式为:
row = i // m # 列数决定每行能容纳多少元素
col = i % m # 列数决定余数的循环周期
在遍历列表(或其他可迭代对象)时同时获取元素的索引和值,对比:
fruits = ['苹果', '香蕉', '橙子']
# 传统方法:通过索引访问元素
for i in range(len(fruits)):
print(f"第 {i+1} 个水果是:{fruits[i]}")
# 输出:
# 第 1 个水果是:苹果
# 第 2 个水果是:香蕉
# 第 3 个水果是:橙子
fruits = ['苹果', '香蕉', '橙子']
# 使用 enumerate()
for index, fruit in enumerate(fruits):
print(f"第 {index+1} 个水果是:{fruit}")
enumerate(fruits)
返回一个迭代器,每次迭代会返回 (索引, 元素值)
组成的元组。for index, fruit in ...
直接将元组解包为两个变量:index
(索引)和 fruit
(元素值)。for i, feature in enumerate(features):
row = i // 2
col = i % 2
axes[row, col].boxplot(data[feature].dropna())
axes[row, col].set_title(f'Boxplot of {feature}')
axes[row, col].set_ylabel(feature)
对比
for i in range(len(features)):
row = i // 2
col = i % 2
feature = features[i]
axes[row, col].boxplot(data[feature].dropna())
axes[row, col].set_title(f'Boxplot of {feature}')
axes[row, col].set_ylabel(feature)