<jupyter_start><jupyter_text>Sigmoid/Logistic function [g(z)]![image.png](attachment:image.png)  Hypothesis![image.png](attachment:image.png)  Cost Function![image.png](attachment:image.png)  Gradient Descent<jupyter_code>def sigmoid(z):
    return 1 / (1 + np.exp(-z))
   
def hypothesis(X, theta):
    return X @ theta

def cost(theta,X,y):
    m,n = X.shape
    hx = sigmoid(hypothesis(X,theta.reshape(len(theta),1)))
    J = np.sum((-y * np.log(hx)) - ((1-y) * np.log(1-hx))) * 1/m
    return J

def gradient(theta, X,y):
    m,n = X.shape
    hx = sigmoid(hypothesis(X,theta.reshape(len(theta),1)))
    g = ((hx - y).T @ X) * 1/m
    return g.flatten() if theta.ndim==1 else g
# theta = np.array([-25.16113549,   0.2062301 ,   0.20147003])
test_theta = np.array([-24, 0.2, 0.2]).reshape(3,1)
theta = op.minimize(fun=cost, x0=test_theta,args=(X,y), method='TNC',jac=gradient).x
plot_x = np.array([X[:,1].min() - 2, X[:,1].max() + 2]);
plot_y = (-1./theta[2]) * (theta[1] * plot_x + theta[0])

plt.figure(figsize=(16,8))
plt.scatter(true_val[:,1],true_val[:,2],marker='+',color='g')
plt.scatter(false_val[:,1],false_val[:,2],marker='o',color='r')
plt.plot(plot_x,plot_y)
plt.xlabel('Exam 1 scores')
plt.ylabel('Exam 2 scores')
plt.legend(('Admitted', 'Not admitted'), bbox_to_anchor=(1.15, 1))
plt.grid(color='gray', linestyle='--', linewidth=.6, axis='both', which='both', alpha=.4)
plt.show();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.23179271708683474, 'fraction_numerical': 0.047619047619047616, 'mean_word_length': 5.029535864978903, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '134280', 'license': 'MIT', 'n_tokens_mistral': 657, 'n_tokens_neox': 611, 'n_words': 95, 'path': 'logistics/notebooks/logistic_reg1.ipynb', 'repo_name': 'vickykatoch/py-ml-scratchpad', 'size': '1428'}