Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách khớp mô hình cho dữ liệu phi tuyến tính trong Python

Dữ liệu thực tế thường không tuân theo quan hệ tuyến tính. Cần các phương pháp khớp mô hình phi tuyến: hồi quy đa thức, spline, kernel regression, tree-based, neural nets...

Ví dụ đơn giản: dùng seaborn.lmplot với order để khớp hồi quy đa thức.

Ví dụ với Anscombe Dataset I (quan hệ tuyến tính)


import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt

my_df = sb.load_dataset('anscombe')
sb.lmplot(x="x", y="y", data=my_df.query("dataset == 'I'"))
plt.show()

Giải thích

  • Dataset I: quan hệ gần tuyến tính → order=1 (mặc định) phù hợp.
  • Dataset II: quan hệ parabolic → cần order=2.
  • Dataset III: tuyến tính nhưng có outlier → cần robust regression.
  • Dataset IV: x không đổi → hồi quy vô nghĩa.

Phương pháp khác cho phi tuyến

  • PolynomialFeatures + LinearRegression (sklearn): tạo đặc trưng bậc cao.
  • Spline regression (patsy + statsmodels): piecewise polynomial mượt.
  • Kernel Ridge / Gaussian Process: non-parametric.
  • Tree-based (Random Forest, XGBoost): tự động bắt tương tác phi tuyến.