<jupyter_start><jupyter_text>Logisitic FunctionSo we want to return a value between 0 and 1 to make sure we are actually representing a probability. To do this we will make use of the logistic function. The logistic function mathematically looks like this:y=1/1+e^(-x)Let's take a look at the plot:<jupyter_code>x_val = np.linspace(-5,5,100)
y_val = [1/(1+math.e**(-x)) for x in x_val]
plt.plot(x_val,y_val)
plt.axhline(0.5)
plt.axvline(0)
sns.despine()
def logistic_func(b,x):
    return float(1)/(1+math.e**(-x.dot(b))) #f(x)

def log_gradient(b,x,y):
    first_calc = logistic_func(b,x) - np.squeeze(y) #-[yi - fxi] Squeeze se [[1]] --> [1] ye hota hai
    final_calc = first_calc.T.dot(x) #(yi-fxi)*xi
    return final_calc

def cost_func(b,x,y): #l(b)
    log_func_v = logistic_func(b,x)
    stp1 = y*np.log(log_func_v) #yi*log(fxi)
    stp2 = (1-y)*np.log(1-log_func_v) #(1-y)*(1-log(fxi))
    final = -stp1 - stp2
    return np.mean(final)

def grad_desc(b_vals, X, y, lr = 0.001, converge_change = 0.001):
    #normalize
    #X = (X - np.mean(X, axis=0)) / np.std(X, axis=0)
    #setup cost iter
    cost_iter = []
    b_list = []
    cost = cost_func(b_vals,X,y)
    cost_iter.append([0,cost])
    change_cost = converge_change+1
    i = 1
    while(change_cost>converge_change):
        old_cost = cost
        b_list.append(b_vals)
        b_vals = b_vals - (lr*log_gradient(b_vals,X,y))
        cost = cost_func(b_vals,X,y)
        cost_iter.append([i,cost])
        change_cost = old_cost-cost
        i+=1
    return b_vals, np.array(cost_iter), b_list

def pred_values(b,X,hard =True):
    pred_prob = logistic_func(b,X)
    pred_value = np.where(pred_prob>=0.5,1,0)
    if(hard):
        return pred_value
    return pred_prob
shape = X.shape[1]
y_flip = np.logical_not(y)
betas = np.zeros(shape)
fitted_vals, cost_iter, b_list = grad_desc(betas, X, y_flip, lr = 0.001, converge_change = 0.001)
print(fitted_vals)
cost_iter
predicted_y = pred_values(fitted_vals, X)
predicted_y
np.sum(y_flip == predicted_y)
plt.plot(cost_iter[:,0], cost_iter[:,1])
plt.ylabel("Cost")
plt.xlabel("Iteration")
sns.despine()
plt.plot(X.dot(b_list[-1]),logistic_func(b_list[-1],X),color = 'g')
def sigmoid_anim(X,y,b_list):
    fig = plt.figure()
    ax = fig.gca()
    t = []
    t.append(X)
    t = tuple(t)
    b = b_list[0]
    ax.scatter(X.dot(b_list[-1]),np.zeros(100))
    sig, = ax.plot(X.dot(b),logistic_func(b,X),color = 'g')
    def update(b):
        sig.set_data(X.dot(b),logistic_func(b,X))
        return sig

    fig.canvas.draw()
    ani = matplotlib.animation.FuncAnimation(fig, update, frames=b_list, blit=False, interval=10 )
    plt.show()
sigmoid_anim(X,y_flip,b_list)
print(X.dot(b_list[1]))
from sklearn import linear_model
logreg = linear_model.LogisticRegression()
logreg.fit(X, y_flip)
sum(y_flip == logreg.predict(X))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16091954022988506, 'fraction_numerical': 0.022640195053988156, 'mean_word_length': 3.8760611205432935, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '58735', 'license': 'MIT', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1210, 'n_words': 224, 'path': '1 Supervised Learning/Classification/Logistic Regression (Full Code).ipynb', 'repo_name': 'HJJ256/Machine-Learning-2018-present-', 'size': '2871'}