@shamdasani/

Linear Regression

Python

No description

fork
loading
Files
  • main.py
  • graph.png

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.6636086266673387","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"ZnJvbSBzdGF0aXN0aWNzIGltcG9ydCBtZWFuCmltcG9ydCBudW1weSBhcyBucAoKeF92YWx1ZXMgPSBucC5hcnJheShbMSwyLDMsNCw1LDYsNyw4LDksMTBdLCBkdHlwZT1ucC5mbG9hdDY0KQp5X3ZhbHVlcyA9IG5wLmFycmF5KFsxLDQsMSw2LDQsNyw0LDYsMTAsOF0sIGR0eXBlPW5wLmZsb2F0NjQpCgpkZWYgYmVzdF9maXRfbGluZSh4X3ZhbHVlcyx5X3ZhbHVlcyk6CiAgICBtID0gKCgobWVhbih4X3ZhbHVlcykqbWVhbih5X3ZhbHVlcykpIC0gbWVhbih4X3ZhbHVlcyp5X3ZhbHVlcykpIC8KICAgICAgICAgKChtZWFuKHhfdmFsdWVzKSptZWFuKHhfdmFsdWVzKSkgLSBtZWFuKHhfdmFsdWVzKnhfdmFsdWVzKSkpCiAgICAKICAgIGIgPSBtZWFuKHlfdmFsdWVzKSAtIG0qbWVhbih4X3ZhbHVlcykKICAgIAogICAgcmV0dXJuIG0sIGIKICAgCm0sIGIgPSBiZXN0X2ZpdF9saW5lKHhfdmFsdWVzLHlfdmFsdWVzKQoKcHJpbnQoInJlZ3Jlc3Npb24gbGluZTogIiArICJ5ID0gIiArIHN0cihyb3VuZChtLDIpKSArICJ4ICsgIiArIHN0cihyb3VuZChiLDIpKSApCgojIFByZWRpY3Rpb24KeF9wcmVkaWN0aW9uID0gMTUKeV9wcmVkaWN0aW9uID0gKG0qeF9wcmVkaWN0aW9uKStiCnByaW50KCJwcmVkaWN0ZWQgY29vcmRpbmF0ZTogKCIgKyBzdHIocm91bmQoeF9wcmVkaWN0aW9uLDIpKSArICIsICIgKyBzdHIocm91bmQoeV9wcmVkaWN0aW9uLDIpKS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XR0ZXIoeF92YWx1ZXMseV92YWx1ZXMsY29sb3I9JyM1YjlkZmYnLGxhYmVsPSdkYXRhJykKcGx0LnNjYXR0ZXIoeF9wcmVkaWN0aW9uLCB5X3ByZWRpY3Rpb24sIGNvbG9yPScjZmMwMDNmJywgbGFiZWw9InByZWRpY3RlZCIpCnBsdC5wbG90KHhfdmFsdWVzLCByZWdyZXNzaW9uX2xpbmUsIGNvbG9yPScwMDAwMDAnLCBsYWJlbD0ncmVncmVzc2lvbiBsaW5lJykKcGx0LmxlZ2VuZChsb2M9NCkKcGx0LnNhdmVmaWcoImdyYXBoLnBuZyIp"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from statistics import mean
import numpy as np

x_values = np.array([1,2,3,4,5,6,7,8,9,10], dtype=np.float64)
y_values = np.array([1,4,1,6,4,7,4,6,10,8], dtype=np.float64)

def best_fit_line(x_values,y_values):
    m = (((mean(x_values)*mean(y_values)) - mean(x_values*y_values)) /
         ((mean(x_values)*mean(x_values)) - mean(x_values*x_values)))
    
    b = mean(y_values) - m*mean(x_values)
    
    return m, b
   
m, b = best_fit_line(x_values,y_values)

print("regression line: " + "y = " + str(round(m,2)) + "x + " + str(round(b,2)) )

# Prediction
x_prediction = 15
y_prediction = (m*x_prediction)+b
print("predicted coordinate: (" + str(round(x_prediction,2)) + ", " + str(round(y_prediction,2)) + ")")

# y values of regression line
regression_line = [(m*x)+b for x in x_values] 

# R Squared Error 
def squared_error(ys_orig, ys_line):
    return sum((ys_line - ys_orig) * (ys_line - ys_orig))

def r_squared_value(ys_orig,ys_line):
    squared_error_regr = squared_error(ys_orig, ys_line) # squared error of regression line 
    y_mean_line = [mean(ys_orig) for y in ys_orig] # horizontal line (mean of y values)
    squared_error_y_mean = squared_error(ys_orig, y_mean_line) # squared error of the mean horizontal line 
    return 1 - (squared_error_regr/squared_error_y_mean)

r_squared = r_squared_value(y_values,regression_line)
print("r^2 value: " + str(r_squared))

# Plotting 
import matplotlib.pyplot as plt
from matplotlib import style
style.use('seaborn')

plt.title('Linear Regression')
plt.scatter(x_values,y_values,color='#5b9dff',label='data')
plt.scatter(x_prediction, y_prediction, color='#fc003f', label="predicted")
plt.plot(x_values, regression_line, color='000000', label='regression line')
plt.legend(loc=4)
plt.savefig("graph.png")