-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (56 loc) · 1.69 KB
/
main.py
File metadata and controls
71 lines (56 loc) · 1.69 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
import matplotlib.pyplot as plt
import csv
class LinearRegression(Object):
def __init__(self):
self.w = 0
self.b = 0
self.rho = 0
def fit(self, X, y):
mean_x = X.mean()
mean_y = y.mean()
errors_x = X - mean_X
errors_y = y - mean_y
errors_product_xy = np.sum(np.multiply(errors_x, errors_y))
squared_errors_x = np.sum(errora_x ** 2)
self.w = errors_product_xy / squared_errors_x
self.b = mean_y - self.w * mean_x
N = len(X)
std_x = X.std()
std_y = y.std()
cov = errors_product_xy / N
self.rho = cov / (std_x * std_y)
def visualize_solution(X, y, lin_reg):
plt.xlabel('Number of share')
plt.ylabel('Number of like')
plt.scatter(X, y)
x = np.arange(0, 800)
y = lin_reg.predict(x)
plt.plot(x, y, 'r--', label='r = %.2f' % lin_reg.rho)
plt.legend()
plt.show()
#Load the dataset and parse it
def load_dataset():
num_rows = sum(1 for line in open('Facebook_metrics\\dataset_Facebook.csv')) - 1
X = np.zeros((num_rows, 1))
y = np.zeros((num_rows, 1))
with open('Facebook_metrics\\dataset_Facebook.csv') as f:
reader = csv.DictReader(f, delimiter=';')
next(reader, None)
for i, row in enumerate(reader):
X[i] = int(row['share']) if len(row['share']) > 0 else 0
y[i] = int(row['like']) if len(row['like']) > 0 else 0
return X, y
#Visualize dataset
def visualize_dataset(X, y):
plt.xlabel('Number of shares')
plt.ylabel('Number of likes')
plt.scatter(X, y)
plt.show()
#Main driver
if __name__ == '__main__':
X, y = load_dataset()
"""visualize_dataset(X, y)"""
lin_reg = LinearRegression()
lin_reg.fit(X, y)
visualize_solution(X, y, lin_reg)