
226
■
16
章 ロジスティック回帰
微積分の知識を少し使えば、この勾配が求められます。
from scratch.linear_algebra import vector_sum
def _negative_log_partial_j(x: Vector, y: float, beta: Vector, j: int) -> float:
"""
1
つのデータポイントの
j
番目の偏微分
"""
return -(y - logistic(dot(x, beta))) * x[j]
def _negative_log_gradient(x: Vector, y: float, beta: Vector) -> Vector:
"""
1
つのデータポイントの勾配
"""
return [_negative_log_partial_j(x, y, beta, j)
for j in range(len(beta))]
def negative_log_gradient(xs: List[Vector],
ys: List[float],
beta: Vector) -> Vector:
return vector_sum([_negative_log_gradient(x, y, beta)
for x, y in zip(xs, ys)])
以上で必要なものはすべて揃いました。
16.3
モデルの適用
最初にデータを学習用とテスト用に分けます。
from scratch.machine_learning ...