- - figure logistic regression data (blue dots)fitted curve (red)and true curve (black dashedlogreg py import numpy as np import matplotlib pyplot as plt from numpy linalg import lstsq ( np random rand( - reshape ( , beta np array ([- ]xmat np hstack (np ones (( , )) ) /( np exp(-xmat beta) np random binomial ( , ,nsample size explanatory variables response variables initial guess betat lstsq (xmat xmat),xmat yrcond =none)[ grad np array ([ , ]gradient while (np sum(np abs(grad) - stopping criteria mu /( np exp(-xmat betat )gradient delta (mu yreshape ( , grad np sum(np multiply np hstack (delta delta )),xmat)axis = hessian xmat np diag(np multiply (mu ,( mu))xmat betat betat lstsq ( ,grad rcond =none)[ print betat plt plot( , 'plot data xx np linspace - , , reshape ( , xxmat np hstack (np ones (len(xx, ))xx)yy /( np exp(xxmat beta)plt plot(xx ,yy ,' -'#true logistic curve yy /( np exp(xxmat betat ))plt plot(xx ,yy ,' --'