python day9

  1. 介绍了热力图的绘制方法
    # 首先走一遍完整的之前的流程
    # 1. 读取数据
    import pandas as pd
    data  = pd.read_csv('data.csv')
    # 2. 查看数据
    data.info()
    data.head(5)
    
    data['Year 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()
    
    
    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()

    3.心脏病数据集绘制热力图和单特征分布的大图

# 定义要绘制的特征
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 // 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)
 
# 调整子图之间的间距
plt.tight_layout()
 
# 显示图形
plt.show()

你可能感兴趣的:(python,开发语言)