# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Linear Regression

# +
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
import random

style.use('fivethirtyeight')


# +
# xs = np.array([1,2,3,4,5,6], dtype=np.float64)
# ys = np.array([5,4,6,5,6,7], dtype=np.float64)

# plt.scatter(xs,ys)
# plt.show()
# -

def create_dataset(hm, varience, step=2, correlation=False):
    
    val = 1
    ys = []
    
    for i in range(hm):
        y = val + random.randrange(-varience, varience)
        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)


# We can test our assumption using varience. If varience decreases, r-square increases
xs, ys = create_dataset(40, 40, 2, correlation='pos')

plt.scatter(xs,ys)
plt.show()


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)
print(b)

regression_line = [(m*x)+b for x in xs]
print(regression_line)

plt.scatter(xs,ys)
plt.plot(regression_line)
plt.show()

predict_x = 8
predict_y = (m*predict_x) + b

plt.scatter(xs,ys)
plt.plot(regression_line)
plt.scatter(predict_x,predict_y, color='red', s=100)
plt.show()
# This line is good fit line but not the best fit line.

def squared_error(ys_orig,ys_line):
    return sum((ys_line-ys_orig)**2)


def coeff_of_determination(ys_orig, ys_line):
#     y_mean_line = [mean(y) for y in ys_orig]
#     y_mean_line = mean(ys_orig)
    y_mean_line = [mean(ys_orig)] * len(ys_orig)
    squared_error_regr = squared_error(ys_orig,ys_line)
    squared_error_regr_y_mean = squared_error(ys_orig,y_mean_line)
    
    return 1 - (squared_error_regr/squared_error_regr_y_mean)


r_squared = coeff_of_determination(ys, regression_line)
print(r_squared)



StarcoderdataJupyterScriptsDedupFiltered