11.28

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

# 提供文件的绝对路径
file_path = r'D:\BP_R_Data.xlsx' # 请替换为实际路径

# 尝试读取Excel文件
try:
df = pd.read_excel(file_path, sheet_name='Sheet1', engine='openpyxl')

# 检查是否有重复列名,并重命名以避免冲突
df.columns = [f"feature_{i}" for i in range(len(df.columns))]

# 显示数据框的前几行以确认正确加载
print("显示数据集的前六条数据:")
print(df.head(6))

target_column = df.columns[-1]
feature_columns = df.columns[:-1]

pairplot_features = list(feature_columns[:5]) + [target_column] # 可视化部分特征与目标的关系

# 使用seaborn创建pairplot
sns.pairplot(df[pairplot_features], diag_kind='kde', plot_kws={"s": 10})

# 调整布局,防止标签被截断
plt.tight_layout()

plt.savefig('pairplot.png')

plt.show()

except FileNotFoundError:
print(f"错误:无法找到文件,请检查路径是否正确:{file_path}")
except Exception as e:
print(f"发生错误:{e}")
posted @ 2024-12-29 15:02  liuxuechao  阅读(12)  评论(0)    收藏  举报