<jupyter_start><jupyter_text>**Linear Regression**<jupyter_code># Import required libraries

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv(r"C:\Users\91939\Downloads\Admission_Predict.csv")
df.head()
df.describe()
df.info()
frame = df.loc["Serial No.":]
frame
df.columns
frame = list(df.columns)
df.columns[[8]]
# Plotting graphs
df.hist(column="GRE Score", bins=20, figsize=(10,5))
df.hist(column="TOEFL Score", bins=20, figsize=(10,5))
df.hist(column="CGPA", bins=20, figsize=(10,5))
df.hist(column="Chance of Admit ", bins=20, figsize=(10,5))
df.loc[1]
X_df = df.drop(df.columns[[0, 2, 3, 4, 5, 6, 7, 8]], axis = 1)
Y_df = df.drop(df.columns[[0, 1, 2, 3, 4, 5, 6, 7]], axis = 1)
X_df.head()
Y_df.head()
x_df = X_df.to_numpy()
y_df = Y_df.to_numpy()
#x_df
#y_df
#for i in range(len(y_df)):
#    if y_df[i] > 0.75:
#        y_df[i] = 1
#    else:
#        y_df[i] = 0
        
#y_df
Y_df = pd.DataFrame(y_df)
Y_df
print("Shape x_df : ", x_df.shape)
print("Shape y_df : ", y_df.shape)
#plt.scatter(x_df, y_df)
#plt
X_df = pd.DataFrame(x_df)
X_df
X_train = X_df.sample(frac = 0.8, random_state = 200)
X_test = X_df.drop(X_train.index)

Y_train = Y_df.sample(frac = 0.8, random_state = 200)
Y_test = Y_df.drop(Y_train.index)
X_train_arr = X_train.to_numpy()
X_test_arr = X_test.to_numpy()
Y_train_arr = Y_train.to_numpy()
Y_test_arr = Y_test.to_numpy()
print("Input Train Shape : ", X_train_arr.shape)
print("Input Test Shape : ", X_test_arr.shape)
print("Output Train Shape : ", Y_train_arr.shape)
print("Output Test Shape : ", Y_test_arr.shape)
#X_train_arr
#Y_train_arr
plt.scatter(X_train_arr, Y_train_arr)
plt
plt.scatter(X_test_arr, Y_test_arr)
plt
def initialize_with_zeros(dim):
    weight = np.zeros((dim, 1))
    bias = 0
    return weight, bias
# hypotesis function

def hypo(x, weight, bias):
    return x * weight + bias
# cost function

def cost(x, y, weight, bias):
    length = len(x)
    train_error = 0
    for i in range(length):
        train_error += (y[i] - hypo(x[i], weight, bias))**2
    return train_error/length
# gradient function

def grad(x, y, weight, bias, alpha):
    weight_deriv = 0
    bias_deriv = 0
    length = len(x)
    grad_list = []
    error_list = []

    for i in range(length):
        weight_deriv += -2 * x[i] * (y[i] - (weight * x[i] + bias))
        bias_deriv += -2 * (y[i] - (weight * x[i] + bias))
        
        error_list.append(cost(x, y, weight, bias)[0])

        # We subtract because the derivatives point in direction of steepest ascent
        weight -= (weight_deriv / length) * alpha
        bias -= (bias_deriv / length) * alpha
        grad_list = {"weight": weight, "bias": bias}

    return grad_list, error_list
def train(x, y, weight, bias, alpha, iters):
    cost_history = []

    for i in range(iters):
        grad_list, error_list = grad(x, y, weight, bias, alpha)
        weight = grad_list["weight"]
        bias = grad_list["bias"]

        #Calculate cost for auditing purposes
        costs = cost(x, y, weight, bias)
        cost_history.append(costs)

        # Log Progress
        if i % 10 == 0:
            print ("Cost after iteration %i: %f" % (i, costs))
            #print("At Iteration : ", i, " Weight : ", weight, " Bias : ", bias, " Cost : ", costs)

    return cost_history, grad_list, error_list
def predict(final_pred, m):
    m = m.shape[1]
    y_pred = np.zeros((1, m))
    y_prediction = []
    for i in range(final_pred.shape[1]):
        if final_pred[0, i] > 0.75:                    # if final prediction crosses 0.5 declare it 1 else 0    
            y_pred[0, i] = 1
        y_prediction.append(y_pred[0, i])
    return y_prediction
def model():
    alpha = 0.000001
    iters = 1000
    weight, bias = initialize_with_zeros(1)
    #costs = cost(X_train_arr, Y_train_arr, weight, bias)
    grad_list, error_list = grad(X_train_arr, Y_train_arr, weight, bias, alpha)
    weight = grad_list["weight"]
    bias = grad_list["bias"]
    cost_history, grad_list, error_list = train(X_train_arr, Y_train_arr, weight, bias, alpha, iters)
    return cost_history, grad_list, error_list
if __name__ == "__main__":
    
    cost_history, grad_list, error_list = model()
    weight = grad_list["weight"]
    bias = grad_list["bias"]
    
    final_train_pred = hypo(X_train_arr.T, weight, bias)
    #final_train_pred = np.dot(weight, X_train_arr.T) + bias
    #print("final_train_pred shape : ", final_train_pred.shape)
    #print("Y_train_arr shape : ", Y_train_arr.shape)
    #print(final_train_pred)
    train_predict = predict(final_train_pred, Y_train_arr.T)
    print("Training Prediction : ", train_predict)
    
    final_test_pred = hypo(X_test_arr.T, weight, bias)
    #final_test_pred = np.dot(weight, X_test_arr.T) + bias
    test_predict = predict(final_test_pred, Y_test_arr.T)
    print("Testing Prediction : ", test_predict)
    
    plt.plot(error_list)
    plt.title("Cost")
    plt.show()<jupyter_output>Cost after iteration 0: 0.015170
Cost after iteration 10: 0.016367
Cost after iteration 20: 0.016367
Cost after iteration 30: 0.016367
Cost after iteration 40: 0.016367
Cost after iteration 50: 0.016367
Cost after iteration 60: 0.016367
Cost after iteration 70: 0.016367
Cost after iteration 80: 0.016367
Cost after iteration 90: 0.016367
Cost after iteration 100: 0.016367
Cost after iteration 110: 0.016367
Cost after iteration 120: 0.016366
Cost after iteration 130: 0.016366
Cost after iteration 140: 0.016366
Cost after iteration 150: 0.016366
Cost after iteration 160: 0.016366
Cost after iteration 170: 0.016366
Cost after iteration 180: 0.016366
Cost after iteration 190: 0.016366
Cost after iteration 200: 0.016366
Cost after iteration 210: 0.016366
Cost after iteration 220: 0.016366
Cost after iteration 230: 0.016366
Cost after iteration 240: 0.016366
Cost after iteration 250: 0.016366
Cost after iteration 260: 0.016366
Cost after iteration 270: 0.016365
Cost after iteration 280: 0.016[...]
{'fraction_non_alphanumeric': 0.12822661749916192, 'fraction_numerical': 0.06268856855514583, 'mean_word_length': 3.561926605504587, 'pattern_counts': {'":': 4, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '408897', 'license': 'MIT', 'n_tokens_mistral': 2489, 'n_tokens_neox': 2168, 'n_words': 585, 'path': 'LinearRegression/LinearRegression1 (1).ipynb', 'repo_name': 'khushi-411/Machine-Learning-Algorithms', 'size': '5966'}