<jupyter_start><jupyter_text>Build your own Linear Regression Algorithm<jupyter_code>from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
xd = [1,2,3,4,5,6,7,8]
yd = [5,4,6,5,6,7,8,9]
plt.scatter(xd,yd)
plt.show()
xd = np.array([1,2,3,4,5,6,7,8], dtype=np.float64)
yd = np.array([5,4,6,5,6,7,8,9], dtype=np.float64)

def best_fit_slope(xd,yd):
    m= (((mean(xd)*mean(yd)) - mean(xd*yd)) / 
        ((mean(xd)**2) - mean(xd*xd)))
    return m

m = best_fit_slope(xd,yd)
print(m)
xs = [1,2,3,4,5]
ys = [5,4,6,5,6]
xs = np.array([1,2,3,4,5], dtype=np.float64)
ys = np.array([5,4,6,5,6], dtype=np.float64)
def best_fit_slope1(xs,ys):
    m = (((mean(xs)*mean(ys)) - mean(xs*ys)) /
         ((mean(xs)**2) - mean(xs**2)))
    return m

m = best_fit_slope1(xs,ys)
print(m)
def best_fit_slope_and_intercept(xs,ys):
    m = (((mean(xs)*mean(ys)) - mean(xs*ys)) /
         ((mean(xs)**2) - mean(xs**2)))
    b = mean(ys) - m*mean(xs)
    return m, b


m, b = best_fit_slope_and_intercept(xs,ys)
print(m,b)
regression_line = [(m*x + b) for x in xs]
from matplotlib import style
plt.scatter(xs,ys)
plt.plot(xs, regression_line)
plt.show()
m, b = best_fit_slope_and_intercept(xd,yd)
regression_line = [(m*x + b) for x in xd]
plt.scatter(xd,yd)
plt.plot(xd, regression_line)
plt.show()
predictx = 12
predicty = m*predictx + b
plt.scatter(xd,yd,color='#003F72',label='data')
plt.plot(xd, regression_line, label='regression line')
plt.legend(loc=4)
plt.show()
def squared_error(ys_orig, ys_line):
    return sum((ys_line - ys_orig)**2)

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(yd,regression_line)
print(r_squared)
m, b = best_fit_slope_and_intercept(xs,ys)
regression_line = [(m*x + b) for x in xs]
r_squared = coefficient_of_determination(ys,regression_line)
print(r_squared)
#Making a dataset with random values
import random
def create_dataset(hm,variance,step=2,correlation=False):
    val = 1
    ys = []
    for i in range(hm):
        y = val + random.randrange(-variance,variance)
        ys.append(y)
        if correlation and correlation == 'pos':
            val+=step
        elif correlation and correlation == 'neg':
            val-=step

    xs = [i for i in range(len(ys))]
    
    return np.array(xs, dtype=np.float64),np.array(ys,dtype=np.float64)
new_xs, new_ys = create_dataset(40,40,2,correlation='pos')
m, b = best_fit_slope_and_intercept(new_xs,new_ys)
regression_line = [(m*x + b) for x in new_xs]
plt.scatter(new_xs,new_ys)
plt.plot(new_xs, regression_line)
plt.show()
r_squared = coefficient_of_determination(new_ys,regression_line)
print(r_squared)
#decresing the variance in the random dataset
new_xs, new_ys = create_dataset(40,10,2,correlation='pos')
m, b = best_fit_slope_and_intercept(new_xs,new_ys)
regression_line = [(m*x + b) for x in new_xs]
plt.scatter(new_xs,new_ys)
plt.plot(new_xs, regression_line)
plt.show()
r_squared = coefficient_of_determination(new_ys,regression_line)
print(r_squared)
#Increasing the variance
new_xs, new_ys = create_dataset(40,100,2,correlation='pos')
m, b = best_fit_slope_and_intercept(new_xs,new_ys)
regression_line = [(m*x + b) for x in new_xs]
plt.scatter(new_xs,new_ys)
plt.plot(new_xs, regression_line)
plt.show()
r_squared = coefficient_of_determination(new_ys,regression_line)
print(r_squared)
new_xs, new_ys = create_dataset(40,40,2,correlation=False)
m, b = best_fit_slope_and_intercept(new_xs,new_ys)
regression_line = [(m*x + b) for x in new_xs]
plt.scatter(new_xs,new_ys)
plt.plot(new_xs, regression_line)
plt.show()
r_squared = coefficient_of_determination(new_ys,regression_line)
print(r_squared)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14962849090443248, 'fraction_numerical': 0.026646169613118113, 'mean_word_length': 5.731034482758621, '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': '262270', 'license': 'MIT', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1599, 'n_words': 292, 'path': 'Linear_regression_algorithm.ipynb', 'repo_name': 'DivyaKrishnani/Linear-Regression-Algorithm-from-scratch', 'size': '3903'}