<jupyter_start><jupyter_text>Creating a custom Linear regression algorithm using pure python<jupyter_code>from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style<jupyter_output><empty_output><jupyter_text>Create our data<jupyter_code>X = np.array([1,2,3,4,5,6], dtype=np.float64)
Y = np.array([5,4,6,5,6,7], dtype=np.float64)<jupyter_output><empty_output><jupyter_text>Getting the slope and intercept using y = mx + b (where m is slope and b is y intercept)<jupyter_code>def best_fit_slope_and_intercept(X,Y):
    m = (  ((mean(X) * mean(Y)) - mean(X*Y)) /
           ((mean(X) * mean(X)) - mean(X*X))  )

    b = mean(Y) - m * mean(X)
    return m, b

m, b = best_fit_slope_and_intercept(X,Y)
print(m,b)<jupyter_output>0.42857142857142866 4.0<jupyter_text>Creating a regression line because now we have value of m(slope) and b(intercept)<jupyter_code>regression_line = [(m*x) + b for x in X]<jupyter_output><empty_output><jupyter_text>Plotting the regression line<jupyter_code>style.use('fivethirtyeight')
plt.scatter(X,Y)
plt.plot(X,regression_line)
plt.show()<jupyter_output><empty_output><jupyter_text>Making a prediction with this Linear model<jupyter_code>predict_X = 5
predict_Y = (m * predict_X) + b
print(predict_Y)<jupyter_output>6.142857142857143<jupyter_text>Plotting our prediction over X<jupyter_code>plt.scatter(X,Y)
plt.scatter(predict_X,predict_Y, color='purple')
plt.plot(X,regression_line)
plt.show()<jupyter_output><empty_output><jupyter_text>Calculating the R-squared-error (Co-efficient of determination)<jupyter_code>def squared_error(ys_orig,ys_line):
    return sum((ys_line - ys_orig) * (ys_line - ys_orig))
def coefficient_of_determination(ys_orig,ys_line):
    y_mean_line = [mean(ys_orig) for y in ys_orig]
    squared_error_regr = squared_error(ys_orig, ys_line)
    squared_error_y_mean = squared_error(ys_orig, y_mean_line)
    return 1 - (squared_error_regr/squared_error_y_mean)
r_squared = coefficient_of_determination(Y,regression_line)
print(r_squared)<jupyter_output>0.5844155844155845
{'fraction_non_alphanumeric': 0.11855421686746988, 'fraction_numerical': 0.034216867469879515, 'mean_word_length': 7.370967741935484, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '96497', 'license': 'MIT', 'n_tokens_mistral': 889, 'n_tokens_neox': 811, 'n_words': 143, 'path': 'Code/CustomLinRegAlgo.ipynb', 'repo_name': 'MDSADABWASIM/ML-Project', 'size': '2075'}