chuyển đổi cấu trúc

This commit is contained in:
Victor Phan
2026-07-08 15:11:34 +07:00
parent b041ccc11a
commit b22d327c5f
45 changed files with 201 additions and 4 deletions
@@ -0,0 +1,8 @@
,Temperature,Noise,Stormwind,Respiratory,Exercises,Culture,Beauty,Education,Society,Spirit,Dirty,Unsafe,Danger
Park,0.5861277411758214,0.46872135461497316,0.5433785341732876,0.552634098528652,0.6278282242144403,0.5249311710036608,0.6010523214572643,0.6698801970338424,0.592175252811864,0.5873765414225279,-0.4119222589542689,-0.36935992311052424,-0.47499404654301275
Residential,0.610813681241285,0.5010876976294922,0.5812495058768619,0.6308055529227076,0.6905711940417876,0.5336682510392587,0.7050811390341176,0.6880894492038738,0.6281007540295365,0.6725382465666585,-0.4172763562532894,-0.3683423748854766,-0.47006223532477215
Garden,0.45224716780106355,0.3994427050538716,0.41423108867853403,0.4171592620121273,0.5703375228779752,0.42420205471782707,0.5225523665124601,0.5586006221718542,0.4604821197705184,0.4741367465117808,-0.3021035545730409,-0.2690208910002917,-0.33928178580122825
Rooftop,0.4960914195506356,0.4389763334105073,0.4741573996062278,0.4043299683546717,0.5234138548608293,0.40036627443478834,0.5613291264190557,0.5137072417522562,0.4507351073588117,0.533245963504779,-0.3044536381978058,-0.3287078138585324,-0.3540628872780644
Recreation,0.40870892534299835,0.36018022246155923,0.38726035929996405,0.327640261869753,0.4435840120247182,0.41597785467253595,0.41702432907643733,0.46845721173859056,0.337735516408859,0.346506311430844,-0.08584770592904668,-0.14113338302330178,-0.2698727403572271
Agriculture,-0.3186847619885256,-0.22410153490737583,-0.31116783542012505,-0.31164843416963245,-0.29672463363412205,-0.08295065998935172,-0.407725512606722,-0.34041202068253673,-0.33774567405476646,-0.3661505107268029,0.4906684665223075,0.4667053886536356,0.5249948486680992
Nature,-0.4377274782376116,-0.2893202018289482,-0.47487184947631206,-0.4416794342162952,-0.4395911413850507,-0.2583285861208512,-0.4891875676625891,-0.4669790589031381,-0.4642512201471137,-0.5258378364708906,0.5182221077803194,0.5103204875306926,0.5338960402111311
1 Temperature Noise Stormwind Respiratory Exercises Culture Beauty Education Society Spirit Dirty Unsafe Danger
2 Park 0.5861277411758214 0.46872135461497316 0.5433785341732876 0.552634098528652 0.6278282242144403 0.5249311710036608 0.6010523214572643 0.6698801970338424 0.592175252811864 0.5873765414225279 -0.4119222589542689 -0.36935992311052424 -0.47499404654301275
3 Residential 0.610813681241285 0.5010876976294922 0.5812495058768619 0.6308055529227076 0.6905711940417876 0.5336682510392587 0.7050811390341176 0.6880894492038738 0.6281007540295365 0.6725382465666585 -0.4172763562532894 -0.3683423748854766 -0.47006223532477215
4 Garden 0.45224716780106355 0.3994427050538716 0.41423108867853403 0.4171592620121273 0.5703375228779752 0.42420205471782707 0.5225523665124601 0.5586006221718542 0.4604821197705184 0.4741367465117808 -0.3021035545730409 -0.2690208910002917 -0.33928178580122825
5 Rooftop 0.4960914195506356 0.4389763334105073 0.4741573996062278 0.4043299683546717 0.5234138548608293 0.40036627443478834 0.5613291264190557 0.5137072417522562 0.4507351073588117 0.533245963504779 -0.3044536381978058 -0.3287078138585324 -0.3540628872780644
6 Recreation 0.40870892534299835 0.36018022246155923 0.38726035929996405 0.327640261869753 0.4435840120247182 0.41597785467253595 0.41702432907643733 0.46845721173859056 0.337735516408859 0.346506311430844 -0.08584770592904668 -0.14113338302330178 -0.2698727403572271
7 Agriculture -0.3186847619885256 -0.22410153490737583 -0.31116783542012505 -0.31164843416963245 -0.29672463363412205 -0.08295065998935172 -0.407725512606722 -0.34041202068253673 -0.33774567405476646 -0.3661505107268029 0.4906684665223075 0.4667053886536356 0.5249948486680992
8 Nature -0.4377274782376116 -0.2893202018289482 -0.47487184947631206 -0.4416794342162952 -0.4395911413850507 -0.2583285861208512 -0.4891875676625891 -0.4669790589031381 -0.4642512201471137 -0.5258378364708906 0.5182221077803194 0.5103204875306926 0.5338960402111311
@@ -0,0 +1,14 @@
,0,1
Temperature,-0.15445511914940652,0.006990515901429791
Noise,-0.07615894298740968,0.00700492282153885
Stormwind,-0.1563914823055307,0.007878888704259901
Respiratory,-0.14800873993854352,-0.011635761620414456
Exercises,-0.16208617036601874,-0.0003806098068870642
Culture,-0.04550439013316019,0.00881620114706321
Beauty,-0.19796961667785237,-0.0012069882779817856
Education,-0.17984311395007466,0.008345565782577814
Society,-0.16523244764182207,-0.011179747262609863
Spirit,-0.1938278638629291,-0.01479224672248243
Dirty,0.662990083180058,0.026367956173616355
Unsafe,0.6446258846820032,-0.0004123561994046179
Danger,0.7399178159934315,-0.028168947040193765
1 0 1
2 Temperature -0.15445511914940652 0.006990515901429791
3 Noise -0.07615894298740968 0.00700492282153885
4 Stormwind -0.1563914823055307 0.007878888704259901
5 Respiratory -0.14800873993854352 -0.011635761620414456
6 Exercises -0.16208617036601874 -0.0003806098068870642
7 Culture -0.04550439013316019 0.00881620114706321
8 Beauty -0.19796961667785237 -0.0012069882779817856
9 Education -0.17984311395007466 0.008345565782577814
10 Society -0.16523244764182207 -0.011179747262609863
11 Spirit -0.1938278638629291 -0.01479224672248243
12 Dirty 0.662990083180058 0.026367956173616355
13 Unsafe 0.6446258846820032 -0.0004123561994046179
14 Danger 0.7399178159934315 -0.028168947040193765
@@ -0,0 +1,8 @@
,0,1
Park,-0.20737329331501148,-0.002436125971632047
Residential,-0.2155834258211865,-0.011730849372120936
Garden,-0.14244079709884974,-0.004395477787898166
Rooftop,-0.15500847340894128,-0.003399988119557015
Recreation,-0.0682883185805637,0.027918075759378963
Agriculture,0.5601149289877119,-0.005751109303690985
Nature,0.6998145216538895,-0.0007509375795379177
1 0 1
2 Park -0.20737329331501148 -0.002436125971632047
3 Residential -0.2155834258211865 -0.011730849372120936
4 Garden -0.14244079709884974 -0.004395477787898166
5 Rooftop -0.15500847340894128 -0.003399988119557015
6 Recreation -0.0682883185805637 0.027918075759378963
7 Agriculture 0.5601149289877119 -0.005751109303690985
8 Nature 0.6998145216538895 -0.0007509375795379177
Binary file not shown.

After

Width:  |  Height:  |  Size: 330 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 395 KiB

@@ -0,0 +1,8 @@
,Temperature,Noise,Stormwind,Respiratory,Exercises,Culture,Beauty,Education,Society,Spirit,Dirty,Unsafe,Danger
Park,0.5861277411758214,0.4687213546149731,0.5433785341732876,0.552634098528652,0.6278282242144403,0.5249311710036608,0.6010523214572643,0.6698801970338424,0.592175252811864,0.5873765414225279,-0.4119222589542689,-0.3693599231105242,-0.4749940465430127
Residential,0.610813681241285,0.5010876976294922,0.5812495058768619,0.6308055529227076,0.6905711940417876,0.5336682510392587,0.7050811390341176,0.6880894492038738,0.6281007540295365,0.6725382465666585,-0.4172763562532894,-0.3683423748854766,-0.4700622353247721
Garden,0.4522471678010635,0.3994427050538716,0.414231088678534,0.4171592620121273,0.5703375228779752,0.424202054717827,0.5225523665124601,0.5586006221718542,0.4604821197705184,0.4741367465117808,-0.3021035545730409,-0.2690208910002917,-0.3392817858012282
Rooftop,0.4960914195506356,0.4389763334105073,0.4741573996062278,0.4043299683546717,0.5234138548608293,0.4003662744347883,0.5613291264190557,0.5137072417522562,0.4507351073588117,0.533245963504779,-0.3044536381978058,-0.3287078138585324,-0.3540628872780644
Recreation,0.4087089253429983,0.3601802224615592,0.387260359299964,0.327640261869753,0.4435840120247182,0.4159778546725359,0.4170243290764373,0.4684572117385905,0.337735516408859,0.346506311430844,-0.0858477059290466,-0.1411333830233017,-0.2698727403572271
Agriculture,-0.3186847619885256,-0.2241015349073758,-0.311167835420125,-0.3116484341696324,-0.296724633634122,-0.0829506599893517,-0.407725512606722,-0.3404120206825367,-0.3377456740547664,-0.3661505107268029,0.4906684665223075,0.4667053886536356,0.5249948486680992
Nature,-0.4377274782376116,-0.2893202018289482,-0.474871849476312,-0.4416794342162952,-0.4395911413850507,-0.2583285861208512,-0.4891875676625891,-0.4669790589031381,-0.4642512201471137,-0.5258378364708906,0.5182221077803194,0.5103204875306926,0.5338960402111311
1 Temperature Noise Stormwind Respiratory Exercises Culture Beauty Education Society Spirit Dirty Unsafe Danger
2 Park 0.5861277411758214 0.4687213546149731 0.5433785341732876 0.552634098528652 0.6278282242144403 0.5249311710036608 0.6010523214572643 0.6698801970338424 0.592175252811864 0.5873765414225279 -0.4119222589542689 -0.3693599231105242 -0.4749940465430127
3 Residential 0.610813681241285 0.5010876976294922 0.5812495058768619 0.6308055529227076 0.6905711940417876 0.5336682510392587 0.7050811390341176 0.6880894492038738 0.6281007540295365 0.6725382465666585 -0.4172763562532894 -0.3683423748854766 -0.4700622353247721
4 Garden 0.4522471678010635 0.3994427050538716 0.414231088678534 0.4171592620121273 0.5703375228779752 0.424202054717827 0.5225523665124601 0.5586006221718542 0.4604821197705184 0.4741367465117808 -0.3021035545730409 -0.2690208910002917 -0.3392817858012282
5 Rooftop 0.4960914195506356 0.4389763334105073 0.4741573996062278 0.4043299683546717 0.5234138548608293 0.4003662744347883 0.5613291264190557 0.5137072417522562 0.4507351073588117 0.533245963504779 -0.3044536381978058 -0.3287078138585324 -0.3540628872780644
6 Recreation 0.4087089253429983 0.3601802224615592 0.387260359299964 0.327640261869753 0.4435840120247182 0.4159778546725359 0.4170243290764373 0.4684572117385905 0.337735516408859 0.346506311430844 -0.0858477059290466 -0.1411333830233017 -0.2698727403572271
7 Agriculture -0.3186847619885256 -0.2241015349073758 -0.311167835420125 -0.3116484341696324 -0.296724633634122 -0.0829506599893517 -0.407725512606722 -0.3404120206825367 -0.3377456740547664 -0.3661505107268029 0.4906684665223075 0.4667053886536356 0.5249948486680992
8 Nature -0.4377274782376116 -0.2893202018289482 -0.474871849476312 -0.4416794342162952 -0.4395911413850507 -0.2583285861208512 -0.4891875676625891 -0.4669790589031381 -0.4642512201471137 -0.5258378364708906 0.5182221077803194 0.5103204875306926 0.5338960402111311
@@ -0,0 +1,93 @@
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import prince
# Load data
df = pd.read_excel('Data_VN_filter_v5.xlsx')
# Extract variables
ugs_cols = ['Park', 'Residential', 'Garden', 'Rooftop', 'Recreation', 'Agriculture', 'Nature']
# Detailed ESS and DES variables
ess_des_detailed = [
# RES (Điều hòa)
'Temperature', 'Noise', 'Stormwind', 'Respiratory',
# CES (Văn hóa)
'Exercises', 'Culture', 'Beauty', 'Education', 'Society', 'Spirit',
# DES (Bất lợi)
'Dirty', 'Unsafe', 'Danger'
]
# Ensure the columns exist in df
available_detailed = [col for col in ess_des_detailed if col in df.columns]
# We want to run CA on the relationships between the 7 UGS types and the 13 specific services.
# Dữ liệu thang đo Likert thường là dữ liệu thứ bậc (Ordinal), nên dùng Spearman's rho sẽ chính xác hơn Pearson.
data = df[ugs_cols + available_detailed].dropna()
corr = data.corr(method='spearman').loc[ugs_cols, available_detailed]
# Tương tự như trước, dịch chuyển ma trận hệ số RHO để các giá trị đều dương (dùng cho CA)
# Alternatively, since prince CA handles frequencies, we can just pass the raw data?
# If we pass raw data of shape (307, 20), CA will treat rows as individuals.
# We want to see relationships between UGS and Detailed Services. Passing the correlation matrix (shifted) is a good proxy for similarity.
corr_shifted = corr + 1
# Initialize CA
ca = prince.CA(n_components=2, n_iter=3, copy=True, check_input=True, engine='scipy', random_state=42)
ca = ca.fit(corr_shifted)
# Extract column and row coordinates
row_coords = ca.row_coordinates(corr_shifted) # UGS
col_coords = ca.column_coordinates(corr_shifted) # Detailed Services
# Save to CSV
corr.to_csv('CA_Detailed_Correlation_Matrix.csv')
row_coords.to_csv('CA_Detailed_UGS_Coords.csv')
col_coords.to_csv('CA_Detailed_Services_Coords.csv')
# Plot Biplot
fig, ax = plt.subplots(figsize=(20, 15))
# Plot UGS points (blue dots)
p_ugs = ax.scatter(row_coords[0], row_coords[1], c='blue', label='UGS Types', s=80, marker='o', edgecolors='black')
# Plot ESS/DES points (red/green squares)
res_cols = ['Temperature', 'Noise', 'Stormwind', 'Respiratory']
ces_cols = ['Exercises', 'Culture', 'Beauty', 'Education', 'Society', 'Spirit']
des_cols = ['Dirty', 'Unsafe', 'Danger']
p_ess = []
for col in available_detailed:
color = 'green' if col in res_cols else ('purple' if col in ces_cols else 'red')
p = ax.scatter(col_coords.loc[col, 0], col_coords.loc[col, 1], c=color, marker='s', s=80, edgecolors='black')
p_ess.append(p)
# Gắn nhãn (Text) tĩnh để đảm bảo tính nhất quán (Deterministic) 100% mỗi lần chạy
# Phân tách vị trí nhãn: UGS nằm lệch trên-trái, ESS/DES nằm lệch dưới-phải
for i, txt in enumerate(ugs_cols):
ax.annotate(txt, (row_coords.iloc[i, 0], row_coords.iloc[i, 1]),
xytext=(-10, 10), textcoords='offset points',
color='blue', fontweight='bold', fontsize=12, ha='right', va='bottom')
for col in available_detailed:
color = 'green' if col in res_cols else ('purple' if col in ces_cols else 'red')
ax.annotate(col, (col_coords.loc[col, 0], col_coords.loc[col, 1]),
xytext=(10, -10), textcoords='offset points',
color=color, fontsize=12, fontweight='bold', ha='left', va='top')
# Add dummy plots for legend
ax.scatter([], [], c='green', marker='s', label='RES (Điều hòa)')
ax.scatter([], [], c='purple', marker='s', label='CES (Văn hóa)')
ax.scatter([], [], c='red', marker='s', label='DES (Bất lợi)')
ax.axhline(0, color='grey', linestyle='--', linewidth=1)
ax.axvline(0, color='grey', linestyle='--', linewidth=1)
ax.set_title('Detailed CA Biplot (UGS vs Specific ESS/DES)', fontsize=16)
ax.set_xlabel('Component 0', fontsize=12)
ax.set_ylabel('Component 1', fontsize=12)
ax.legend(loc='center left', bbox_to_anchor=(1.02, 0.5), fontsize=12)
plt.grid(True, linestyle=':', alpha=0.6)
plt.savefig('CA_plot_detailed.png', dpi=300, bbox_inches='tight')
print("\nDetailed CA Plot saved to CA_plot_detailed.png")
print("Detailed data exported to CSV files.")
@@ -0,0 +1,24 @@
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Đọc ma trận RHO đã tính từ file CSV
corr = pd.read_csv('CA_Detailed_Correlation_Matrix.csv', index_col=0)
# Vẽ biểu đồ nhiệt (Heatmap)
plt.figure(figsize=(14, 8))
sns.heatmap(corr, annot=True, fmt=".2f", cmap="coolwarm", center=0,
vmin=-1, vmax=1, linewidths=.5, cbar_kws={"shrink": .8})
plt.title('Spearman RHO Correlation Matrix (UGS vs Specific ESS/DES)', fontsize=16, pad=20)
plt.ylabel('UGS Types', fontsize=12)
plt.xlabel('ESS/DES Variables', fontsize=12)
# Xoay nhãn trục X để dễ đọc hơn
plt.xticks(rotation=45, ha='right')
plt.yticks(rotation=0)
plt.tight_layout()
plt.savefig('RHO_Heatmap.png', dpi=300)
corr.to_csv('RHO_Matrix.csv')
print("Heatmap saved to RHO_Heatmap.png and data to RHO_Matrix.csv")