Function calculates sigmoid function (g) for every sample. With g it calculates part of Hessian matrix and gradient, aggregates and output them. It also calculates J function which is needed for checking the convergence of parameters theta.

def map_fit(interface, state, label, inp):
    """
    Function calculates sigmoid function (g) for every sample. With g it calculates part of Hessian matrix and gradient, aggregates and output them. It also calculates J function which is needed for checking the convergence of parameters theta.
    """
    import numpy as np
    out = interface.output(0)

    H, J, grad = 0, 0, 0
    for row in inp:
        row = row.strip().split(state["delimiter"])  # split row
        if len(row) > 1:  # check if row is empty
            # add intercept term to every sample
            x = np.array([1] + [(0 if v in state["missing_vals"] else float(v)) for i, v in enumerate(row) if
                                i in state["X_indices"]])
            # map label value to 0 or 1. If label does not match set error
            y = 0 if state["y_map"][0] == row[state["y_index"]] else 1 if state["y_map"][1] == row[
                state["y_index"]] else "Error"

            g = 1. / (1 + np.exp(-np.dot(x, state["thetas"])))  # sigmoid function
            grad += x * (g - y)  # gradient
            H += np.multiply(np.outer(x, x), g * (1 - g))  # Hessian matrix
            J -= np.log(g) if y == 1 else np.log(1 - g)  # J cost function
    out.add("grad", grad)
    out.add("J", J)
    for i, row in enumerate(H):
        out.add(i, row)