用Python可视化

问题描述 投票:0回答:1

我正在建立一个信用风险模型。我在直方图中可视化数据时遇到问题

我尝试了这段代码:

# List of numerical columns for histogram visualization
numerical_columns = ['case_id', 'collater_valueofguarantee_1124L', 'collater_valueofguarantee_876L',
                     'num_group1', 'num_group2', 'pmts_month_158T', 'pmts_month_706T',
                     'pmts_year_1139T', 'pmts_year_507T']

# Plot histograms for each numerical column
for column in numerical_columns:
    plt.hist(df[column].dropna(), bins=20)  # Drop null values before plotting
    plt.xlabel(column)
    plt.ylabel('Frequency')
    plt.title(f'Histogram of {column}')
    plt.show()

我最终得到了下面的直方图,如附件所示。我看不懂这是什么意思enter image description here

非常欢迎任何有关如何改进直方图的建议。

干杯

python-3.x visualization
1个回答
0
投票

对于信用风险分析,我不会使用直方图,因为它们没有提供有关数据分布的信息。他们只是提供统计数据。我会选择箱形图或小提琴图。它们提供有关异常值和密度分布的信息。

这里有两个例子:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

data = {
    'case_id': np.random.randint(1, 1000, 100),
    'collater_valueofguarantee_1124L': np.random.normal(50000, 10000, 100),
    'collater_valueofguarantee_876L': np.random.normal(50000, 15000, 100),
    'num_group1': np.random.gamma(shape=2, scale=2, size=100),
    'num_group2': np.random.gamma(shape=2, scale=2, size=100),
    'pmts_month_158T': np.random.poisson(lam=300, size=100),
    'pmts_month_706T': np.random.poisson(lam=500, size=100),
    'pmts_year_1139T': np.random.randint(1, 12, 100),
    'pmts_year_507T': np.random.randint(1, 12, 100)
}
df = pd.DataFrame(data)

plt.figure(figsize=(12, 8))
sns.boxplot(data=df)
plt.xticks(rotation=45)
plt.title('Box Plot of Numerical Variables')
plt.show()

plt.figure(figsize=(12, 8))
sns.violinplot(data=df)
plt.xticks(rotation=45)
plt.title('Violin Plot of Numerical Variables')
plt.show()

这给出了

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.