Jak29 commited on
Commit
de48227
1 Parent(s): c37db61

Delete main.py

Browse files
Files changed (1) hide show
  1. main.py +0 -126
main.py DELETED
@@ -1,126 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- Created on Sun Nov 6 18:29:53 2022
4
-
5
- @author: culli
6
- """
7
-
8
- import pandas as pd
9
- import numpy as np
10
- import pandas_datareader.data as web
11
- import datetime as dt
12
- from datetime import timedelta
13
- from sklearn.preprocessing import MinMaxScaler
14
- import tensorflow as tf
15
- from tensorflow import lite
16
- from tensorflow.keras.models import Sequential
17
- from tensorflow.keras.layers import Dense, Dropout, LSTM
18
- import matplotlib.pyplot as plt
19
-
20
- # Load Training Data
21
- company = "BTC-USD"
22
- start = dt.datetime(2015,1,1)
23
- end = dt.datetime(2022,1,1)
24
- data = web.DataReader(company, "yahoo", start, end)
25
-
26
-
27
- # Prepare Data
28
- scaler = MinMaxScaler(feature_range=(0,1))
29
- scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1,1))
30
-
31
- prediction_days = 60
32
-
33
- x_train = []
34
- y_train = []
35
-
36
- for x in range(prediction_days, len(scaled_data)):
37
- x_train.append(scaled_data[x-prediction_days:x, 0])
38
- y_train.append(scaled_data[x, 0])
39
-
40
-
41
- x_train, y_train = np.array(x_train), np.array(y_train)
42
- x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
43
-
44
-
45
- # Build the model
46
- model = Sequential()
47
-
48
- model.add(LSTM(units=50, return_sequences=True, input_shape = (x_train.shape[1], 1)))
49
- model.add(Dropout(0.2))
50
- model.add(LSTM(units=50, return_sequences=True))
51
- model.add(Dropout(0.2))
52
- model.add(LSTM(units=50))
53
- model.add(Dropout(0.2))
54
- model.add(Dense(units=1))
55
-
56
-
57
- model.compile(optimizer="adam", loss="mean_squared_error")
58
- model.fit(x_train, y_train, epochs=25, batch_size=64)
59
-
60
- # Load Data
61
- test_start = dt.datetime(2022,1,1)
62
- test_end = dt.datetime.now()
63
-
64
- test_data = web.DataReader(company, "yahoo", test_start, test_end)
65
- actual_prices = test_data["Close"].values
66
-
67
- total_dataset = pd.concat((data["Close"], test_data["Close"]), axis = 0)
68
-
69
-
70
-
71
- model_inputs = total_dataset[len(total_dataset) - len(test_data) - prediction_days:].values # Important
72
- model_inputs = model_inputs.reshape(-1, 1)
73
- model_inputs = scaler.transform(model_inputs)
74
-
75
- # Loop for making predictions
76
- for x in range(prediction_days):
77
-
78
-
79
- # Make Predictions on Test Data
80
- x_test = []
81
-
82
- for x in range(prediction_days, len(model_inputs)+1):
83
- x_test.append(model_inputs[x-prediction_days:x, 0])
84
-
85
- x_test = np.array(x_test)
86
- x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
87
-
88
- predicted_prices = model.predict(x_test)
89
- predicted_prices = scaler.inverse_transform(predicted_prices)
90
-
91
-
92
- # Predict Next Day
93
- real_data = [model_inputs[len(model_inputs) + 1 - prediction_days:len(model_inputs+1), 0]]
94
- real_data = np.array(real_data)
95
- real_data = np.reshape(real_data, (real_data.shape[0], real_data.shape[1], 1))
96
-
97
- # Formatting the prediction and printing it
98
- prediction = model.predict(real_data)
99
- prediction = scaler.inverse_transform(prediction)
100
- print(f"Prediction: {prediction}")
101
-
102
- # Adding the prediction back into the model for the next prediction
103
- model_prediction_input = prediction
104
- model_prediction_input = model_prediction_input.reshape(-1, 1)
105
- model_prediction_input = scaler.transform(model_prediction_input)
106
- model_inputs = np.concatenate((model_inputs, model_prediction_input))
107
-
108
-
109
- plt.plot(actual_prices, color="black", label=f"Actual {company} Price")
110
- plt.plot(predicted_prices, color="green", label=f"Predicted {company} Price")
111
- plt.title(f"{company} Share Price")
112
- plt.xlabel("Days")
113
- plt.ylabel(f"{company} Share Price")
114
- plt.legend()
115
- plt.show()
116
-
117
-
118
- tf.keras.models.save_model(model,"model.pbtxt")
119
- converter = lite.TFLiteConverter.from_keras_model(model = model)
120
- converter.optimizations = [tf.lite.Optimize.DEFAULT]
121
- converter.experimental_new_converter=True
122
- converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
123
- tf.lite.OpsSet.SELECT_TF_OPS]
124
- model_tflite = converter.convert()
125
- open("BTCPrediction.tflite", "wb").write(model_tflite)
126
-