statsmodels

Science

統計分析

概述 statsmodels

Statsmodels 是一個 Python 函式庫,它提供了用於估計許多不同統計模型以及進行統計測試和探索統計資料的類別和函數。它透過用於迴歸、時間序列分析、非參數統計等的更高層級介面補充了SciPy。

使用PyRun,您可以擬合 OLS 迴歸模型、執行假設檢定並直接在瀏覽器中探索統計摘要。 Statsmodels 捆綁在Pyodide發行版中,因此您可以立即開始分析數據,而無需任何本地 Python 設定。

程式碼和執行輸出

擬合並評估一般最小平方法模型。

OLS Linear Regression在編輯器中執行
import statsmodels.api as sm
import numpy as np

np.random.seed(42)
x = np.linspace(0, 10, 50)
y = 3 * x + 7 + np.random.normal(0, 2, size=50)

X = sm.add_constant(x)
model = sm.OLS(y, X).fit()

print("OLS Regression Results")
print("=" * 40)
print(f"R-squared  : {model.rsquared:.4f}")
print(f"Intercept  : {model.params[0]:.4f}  (true: 7)")
print(f"Slope      : {model.params[1]:.4f}  (true: 3)")
print(f"P-values   : {model.pvalues.round(4).tolist()}")
print(f"AIC        : {model.aic:.2f}")

相關套餐

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。