vif.py 920 B

12345678910111213141516171819202122232425
  1. import pandas as pd
  2. import numpy as np
  3. from statsmodels.stats.outliers_influence import variance_inflation_factor
  4. # 实际数据
  5. df = pd.read_csv("paper_data.csv")
  6. df = df.drop(columns=["before_last_NO2", "before_last_O3", "last_year_pm10",
  7. "before_last_pm1", "before_last_pm2.5","last_year_pm1","last_year_OM","last_year_BC",
  8. 'before_last_pm10', 'last_year_NO3', 'last_year_NH4', 'before_last_SO4',
  9. 'before_last_NO3', 'before_last_NH4', 'before_last_OM', 'before_last_BC', 'before_last_nl'])
  10. # 计算每个变量的 VIF方差膨胀因子
  11. def calculate_vif(df):
  12. vif_data = pd.DataFrame()
  13. vif_data["Variable"] = df.columns
  14. vif_data["VIF"] = [
  15. variance_inflation_factor(df.values, i) for i in range(df.shape[1])
  16. ]
  17. return vif_data
  18. # 调用函数计算 VIF
  19. vif_result = calculate_vif(df)
  20. # 输出 VIF 结果
  21. print(vif_result)