這段代碼將數據進行PCA降維至3維,并繪制一個三維散點圖,展示降維后的前3個主成分。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import matplotlib.colors as mcolors
from mpl_toolkits.mplot3d import Axes3D# 讀取數據
file_path = '4_SmCrTe3_Study_AFM_Select.txt'
data = pd.read_csv(file_path, sep='\t', header=None)# 命名列
columns = ['ID', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'Energy', 'Unused']
data.columns = columns# 刪除不需要的列
data = data.drop(columns=['ID', 'Unused'])# 數據概覽
print(data.describe())# 分析Energy列的統計數據
energy_stats = data['Energy'].describe()
print("\nEnergy column statistics:")
print(energy_stats)# 1. 直方圖(1_Energy_Analysis_Histogram.png)
plt.figure(figsize=(12, 6))# 直方圖
plt.subplot(1, 2, 1)
sns.histplot(data['Energy'], kde=True)
plt.title('Energy Distribution')
plt.xlabel('Energy')# 在直方圖中標注count數量
for patch in plt.gca().patches:height = patch.get_height()plt.annotate(f'{height:.0f}', (patch.get_x() + patch.get_width() / 2, height), ha='center', va='bottom')# 第二個直方圖,用于替代箱線圖
plt.subplot(1, 2, 2)
sns.histplot(data['Energy'], bins=30, kde=True)
plt.title('Energy Distribution (Detailed)')
plt.xlabel('Energy')# 在直方圖中標注count數量
for patch in plt.gca().patches:height = patch.get_height()plt.annotate(f'{height:.0f}', (patch.get_x() + patch.get_width() / 2, height), ha='center', va='bottom')plt.tight_layout()
plt.show()# 檢查并處理NaN值
print("\nNumber of NaN values in each column:")
print(data.isna().sum())# 使用插值方法填補NaN值
data = data.interpolate()# 再次檢查NaN值是否已經處理
print("\nNumber of NaN values in each column after interpolation:")
print(data.isna().sum())# 2. 散點圖(2_Energy_Analysis_Scatter.png)
plt.figure(figsize=(12, 6))
sns.scatterplot(data=data, x=data.index, y='Energy', color='dodgerblue')
plt.title('Selected SmCrTe3 Energy Distribution', fontsize=15)
plt.xlabel('Sample Index', fontsize=12)
plt.ylabel('Energy (meV)', fontsize=12)
plt.show()# 3. 熱力圖(3_Single_f-Orbital_Couplings_with_Energy_Hot.png)
plt.figure(figsize=(12, 8))
sns.heatmap(data.corr(), annot=True, cmap='coolwarm', center=0, linewidths=0.5)
plt.title('Correlation Matrix of f-Orbital Occupations and Energy', fontsize=15)
plt.show()# 雙軌道和能量關系(4_Double_f-Orbital_Couplings_with_Energy_Hot.png)
couplings = pd.DataFrame()
for i in range(1, 8):for j in range(i + 1, 8):couplings[f'f{i}*f{j}'] = data[f'f{i}'] * data[f'f{j}']
couplings['Energy'] = data['Energy']# 計算耦合特征與能量的相關性
coupling_correlation = couplings.corr()['Energy'][:-1].values# 初始化7x7矩陣為0
coupling_correlation_matrix = pd.DataFrame(0, index=[f'f{i}' for i in range(1, 8)],columns=[f'f{j}' for j in range(1, 8)])index = 0
for i in range(1, 8):for j in range(i + 1, 8):correlation_value = coupling_correlation[index]coupling_correlation_matrix.loc[f'f{i}', f'f{j}'] = correlation_valuecoupling_correlation_matrix.loc[f'f{j}', f'f{i}'] = correlation_valueindex += 1# 繪制熱力圖
plt.figure(figsize=(10, 8))
sns.heatmap(coupling_correlation_matrix.astype(float), annot=True, cmap='coolwarm', fmt=".2f", annot_kws={"size": 10})
plt.title('Correlation of f-Orbital Couplings with Energy')
plt.xlabel('f-Orbital')
plt.ylabel('f-Orbital')
plt.show()# 主成分分析(PCA)
features = ['f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7']
x = data[features]
y = data['Energy']# 標準化
scaler = StandardScaler()
x_scaled = scaler.fit_transform(x)# PCA降維
pca = PCA(n_components=3)
principal_components = pca.fit_transform(x_scaled)
pca_df = pd.DataFrame(data=principal_components, columns=['PC1', 'PC2', 'PC3'])
pca_df['Energy'] = y.values# 自定義顏色映射
cmap = mcolors.LinearSegmentedColormap.from_list("custom", ["red", "yellow", "green", "blue"])# 繪制PCA結果3D散點圖
fig = plt.figure(figsize=(16, 10))
ax = fig.add_subplot(111, projection='3d')# 繪制散點
sc = ax.scatter(pca_df['PC1'], pca_df['PC2'], pca_df['PC3'], c=pca_df['Energy'], cmap=cmap)# 添加顏色條
cbar = plt.colorbar(sc, ax=ax, pad=0.1)
cbar.set_label('Energy')# 設置軸標簽
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_zlabel('PC3')
ax.set_title('PCA of f-Orbital Occupations (3D)')plt.show()