Public
Edited
Nov 2, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
train = {
const X = data3_2_1.map(d=>[1, d.x1, d.x2]);
const y = data3_2_1.map(d=>d.y);

return {X:X, y:y}
}
Insert cell
lr = py`import numpy as np
import numpy as np
from scipy.optimize import minimize

def sigmoid(z):
return(1 / (1 + np.exp(-z)))

def costFunction(theta, X, y):
m = y.size
h = sigmoid(X.dot(theta))
J = -1.0*(1.0/m)*(np.log(h).T.dot(y)+np.log(1-h).T.dot(1-y))
if np.isnan(J[0]):
return(np.inf)
return J[0]

def gradient(theta, X, y):
m = y.size
h = sigmoid(X.dot(theta.reshape(-1,1)))
grad =(1.0/m)*X.T.dot(h-y)

return(grad.flatten())

def run(X, y):

X = np.array(X)
y = np.array(y)

initial_theta = np.zeros(X.shape[1])
cost = costFunction(initial_theta, X, y)
grad = gradient(initial_theta, X, y)
grad_all = [grad]
cost_all = [cost]
def history(x):
grad_all.append(x)
cost_all.append(costFunction(x, X, y))
res = minimize(costFunction, initial_theta, args=(X,y), jac=gradient, callback=history, options={'maxiter':400, 'disp':True})

return {'res':res, 'grad_all':grad_all, 'cost_all':cost_all}

run`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// train = {
// const X = math.matrix(data3_2_1.map(d=>[1, d.x1, d.x2]));
// const y = math.matrix(data3_2_1.map(d=>d.y));
// const w0 = math.matrix(new Array(X.size()[1]).fill(0));

// return {X:X, y:y, w0:w0}
// }

// sigmoid = (z) => {
// return 1 / (1 + math.exp(-z))
// }

// lossFunction = (w, X, y) => {
// const m = y.size()[0];
// const h = math.matrix(math.multiply(X, w).toArray().map(d=>sigmoid(d)));

// const J =
// (
// math.multiply( math.transpose(math.log(h)), y ) +
// math.multiply( math.transpose(math.log(math.subtract(math.transpose(math.matrix(new Array(h.size()[0]).fill(1))) , h))), math.subtract(math.transpose(math.matrix(new Array(y.size()[0]).fill(1))) , y) )
// ) / m * (-1.0);

// return isNaN(J) ? Infinity : J;
// }

// gradient = (w, X, y) => {
// const m = y.size()[0];
// const h = math.matrix(math.multiply(X, w).toArray().map(d=>sigmoid(d)));

// const grad = (math.multiply(math.transpose(X), math.subtract(h, y))).toArray().map(d=>d/m);

// return grad;
// }
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more