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([3,4,6,8,9,10], dtype=np.float64)

def create_dataset(n, variance, step=2, correlation=False):
	val =1
	ys= []
	for i in range(n):
		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)
#what the above function does is create a dataset with random numbers 
#we take a value and start adding steps to it 
#the data points ys are generated by taking a random number between posetive and negative values of variance 
#xs are generated by taking all indexes upto length of ys

def best_fit_slope(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 
#this is to calculate the best fit slope using the formula above 

def squared_error(ys_orig,ys_line):
	return sum((ys_orig-ys_line)**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)

xs,ys= create_dataset(40,10,2,correlation='pos')
m,b=best_fit_slope(xs,ys)
regression_line = [(m*x)+b for x in xs]

plt.scatter(xs,ys)
plt.plot(xs,regression_line)
predict_x=8
predict_y=(m*predict_x)+b 

r_squared = coefficient_of_determination(ys, regression_line)
print(r_squared)
plt.scatter(predict_x,predict_y,s=100)
plt.show()
# print(m,b)
c40d7c82686ac79d2b44d2047ad8224bb7199133
<mask token>


def best_fit_slope(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


<mask token>


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


<mask token>

<mask token>


def best_fit_slope(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


def squared_error(ys_orig, ys_line):
    return sum((ys_orig - ys_line) ** 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


<mask token>

<mask token>
style.use('fivethirtyeight')


def create_dataset(n, variance, step=2, correlation=False):
    val = 1
    ys = []
    for i in range(n):
        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)


def best_fit_slope(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


def squared_error(ys_orig, ys_line):
    return sum((ys_orig - ys_line) ** 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


<mask token>
plt.scatter(xs, ys)
plt.plot(xs, regression_line)
<mask token>
print(r_squared)
plt.scatter(predict_x, predict_y, s=100)
plt.show()

from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
import random
style.use('fivethirtyeight')


def create_dataset(n, variance, step=2, correlation=False):
    val = 1
    ys = []
    for i in range(n):
        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)


def best_fit_slope(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


def squared_error(ys_orig, ys_line):
    return sum((ys_orig - ys_line) ** 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


xs, ys = create_dataset(40, 10, 2, correlation='pos')
m, b = best_fit_slope(xs, ys)
regression_line = [(m * x + b) for x in xs]
plt.scatter(xs, ys)
plt.plot(xs, regression_line)
predict_x = 8
predict_y = m * predict_x + b
r_squared = coefficient_of_determination(ys, regression_line)
print(r_squared)
plt.scatter(predict_x, predict_y, s=100)
plt.show()

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([3,4,6,8,9,10], dtype=np.float64)

def create_dataset(n, variance, step=2, correlation=False):
	val =1
	ys= []
	for i in range(n):
		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)
#what the above function does is create a dataset with random numbers 
#we take a value and start adding steps to it 
#the data points ys are generated by taking a random number between posetive and negative values of variance 
#xs are generated by taking all indexes upto length of ys

def best_fit_slope(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 
#this is to calculate the best fit slope using the formula above 

def squared_error(ys_orig,ys_line):
	return sum((ys_orig-ys_line)**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)

xs,ys= create_dataset(40,10,2,correlation='pos')
m,b=best_fit_slope(xs,ys)
regression_line = [(m*x)+b for x in xs]

plt.scatter(xs,ys)
plt.plot(xs,regression_line)
predict_x=8
predict_y=(m*predict_x)+b 

r_squared = coefficient_of_determination(ys, regression_line)
print(r_squared)
plt.scatter(predict_x,predict_y,s=100)
plt.show()
# print(m,b)