1

When I was studying machine learning, I came across the cost function for logistic regression written as:

$ Cost(h_\theta(x), y) = -y\log(h_\theta(x)) - (1 - y)\log(1 - h_\theta(x)) $

As a rewrite of the piecewise version:

$ Cost(h_\theta(x), y) = \begin{cases} −log(h_\theta(x)) , y = 1 \\ −log(1−h_\theta(x)), y = 0 \end{cases}$

I'm now experimenting with this pattern ( $ y \ x + z \ (1 - x) $ where $ x \in \{0, 1\} $ ) as a programming technique to avoid using if else expressions for calculations.

from:

function calc(a, b, c) {
  if(a == 1) return b * 3
  else if (a == 0) return c * 5
}

to:

function calc(a, b, c) {
  return a * (b * 3) + (1 - a) * (c * 5)
}

My question is, does this pattern have a name? Is it useful? Is it common?

0 Answers0