-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
75 lines (60 loc) · 2.11 KB
/
main.py
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
72
73
74
75
#!/usr/bin/python3
import sys
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
def main(argv, prediction_days=30):
# Loading Dataset
df = pd.read_csv(
'dataset.csv')
df['date'] = pd.to_datetime(df['Timestamp'], unit='s').dt.date
group = df.groupby('date')
Real_Price = group['Weighted_Price'].mean()
if len(argv) >= 2:
try:
prediction_days = int(argv[1])
except ValueError:
prediction_days = 30
df_train = Real_Price[:len(Real_Price)-prediction_days]
df_test = Real_Price[len(Real_Price)-prediction_days:]
# Preparation of the set
training_set = df_train.values
training_set = training_set.reshape(-1, 1)
sc = MinMaxScaler()
training_set = sc.fit_transform(training_set)
X_train = training_set[0:len(training_set)-1]
y_train = training_set[1:len(training_set)]
test_set = df_test.values
test_set = test_set.reshape(-1, 1)
sc = MinMaxScaler()
test_set = sc.fit_transform(test_set)
X_test = test_set[0:len(test_set)-1]
y_test = test_set[1:len(test_set)]
# Create linear regression object
regr = LinearRegression()
# Train the model using the training sets
regr.fit(X_train, y_train)
# Make predictions using the testing set
y_pred = regr.predict(X_test)
# The coefficients
print('Coefficients: \n', regr.coef_)
# The mean squared error
print("Mean squared error: %.5f"
% mean_squared_error(y_test, y_pred))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.5f' % r2_score(y_test, y_pred))
# Plot outputs
plt.title(str(prediction_days) + ' days predicted')
plt.scatter(X_test, y_test, color='black')
plt.plot(X_test, y_pred, color='blue', linewidth=3)
plt.xticks(())
plt.yticks(())
if len(argv) >= 2 and argv[1] == '-m':
plt.show()
if __name__ == "__main__":
main(sys.argv)