-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearRegressionForOnevariable.py
More file actions
42 lines (36 loc) · 1.38 KB
/
linearRegressionForOnevariable.py
File metadata and controls
42 lines (36 loc) · 1.38 KB
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
import csv
import os
path = os.getcwd() + '\\ex1data1.txt'
with open(path) as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
features = []
outputs = []
for row in readCSV:
feature = float(row[0])
output = float(row[1])
features.append(feature)
outputs.append(output)
def gradientDescent(features, outputs, alpha=0.01, iters=10000, theta0=0, theta1=0):
num_features = len(features)
for i in range(0,iters):
temp_theta0 = theta0
temp_theta1 = theta1
mse = cost(features,outputs,temp_theta0,temp_theta1,num_features)
error_theta0 = 0
error_theta1 = 0
for i in range(0,num_features):
error_theta0 += theta0 + theta1 * features[i] - outputs[i]
error_theta1 += (theta0 + theta1 * features[i] - outputs[i])*features[i]
theta0 = temp_theta0 - alpha * error_theta0 / num_features
theta1 = temp_theta1 - alpha * error_theta1 / num_features
if(cost(features,outputs,theta0,theta1,num_features) >= mse):
break
print(f"theta0: {theta0}, theta1: {theta1}")
print(mse)
def cost(features,outputs,theta0,theta1,num_features):
x = 0
for i in range(0,num_features):
x += (theta0 + theta1*features[i] - outputs[i])**2
mse = x/(2*num_features)
return mse
gradientDescent(features,outputs)