逻辑回归的工作原理

Python 中级回归:使用 statsmodels

Maarten Van den Broeck

Content Developer at DataCamp

平方和不适用

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

y_actual 始终为 01

y_pred 介于 01 之间。

有比平方和更好的指标。

Python 中级回归:使用 statsmodels

似然

       y_pred * y_actual
Python 中级回归:使用 statsmodels

似然

       y_pred * y_actual + (1 - y_pred) * (1 - y_actual)
Python 中级回归:使用 statsmodels

似然

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

y_actual = 1

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

y_actual = 0

y_pred * 0 + (1 - y_pred) * (1 - 0) = 1 - y_pred
Python 中级回归:使用 statsmodels

对数似然

  • 计算似然需要相加许多极小数,易产生数值误差。
  • 对数似然更易计算。
log_likelihood = np.log(y_pred) * y_actual + np.log(1 - y_pred) * (1 - y_actual)

两种公式结果相同。

Python 中级回归:使用 statsmodels

负对数似然

最大化对数似然等同于最小化负对数似然。

-np.sum(log_likelihoods)
Python 中级回归:使用 statsmodels

逻辑回归算法

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]
)
Python 中级回归:使用 statsmodels

开始练习吧!

Python 中级回归:使用 statsmodels

Preparing Video For Download...