Jak funguje logistická regrese

Intermediate Regression with statsmodels in Python

Maarten Van den Broeck

Content Developer at DataCamp

Součet čtverců nefunguje

np.sum((y_pred - y_actual) ** 2)

y_actual je vždy 0 nebo 1.

y_pred je mezi 0 a 1.

Existuje lepší metrika než součet čtverců.

Intermediate Regression with statsmodels in Python

Věrohodnost

       y_pred * y_actual
Intermediate Regression with statsmodels in Python

Věrohodnost

       y_pred * y_actual + (1 - y_pred) * (1 - y_actual)
Intermediate Regression with statsmodels in Python

Věrohodnost

np.sum(y_pred * y_actual + (1 - y_pred) * (1 - y_actual))

Když y_actual = 1

y_pred * 1 + (1 - y_pred) * (1 - 1) = y_pred

Když y_actual = 0

y_pred * 0 + (1 - y_pred) * (1 - 0) = 1 - y_pred
Intermediate Regression with statsmodels in Python

Log-likelihood

  • Výpočet věrohodnosti zahrnuje sčítání mnoha velmi malých čísel, což vede k numerickým chybám.
  • Log-likelihood je snazší vypočítat.
log_likelihood = np.log(y_pred) * y_actual + np.log(1 - y_pred) * (1 - y_actual)

Obě rovnice dávají stejný výsledek.

Intermediate Regression with statsmodels in Python

Záporná log-likelihood

Maximalizace log-likelihood je totéž jako minimalizace záporné log-likelihood.

-np.sum(log_likelihoods)
Intermediate Regression with statsmodels in Python

Algoritmus logistické regrese

def calc_neg_log_likelihood(coeffs)

intercept, slope = coeffs
# More calculation!
from scipy.optimize import minimize

minimize(
  fun=calc_neg_log_likelihood,
  x0=[0, 0]
)
Intermediate Regression with statsmodels in Python

Vyzkoušejte si to!

Intermediate Regression with statsmodels in Python

Preparing Video For Download...