-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptopriceprediction.py
428 lines (325 loc) Β· 12.8 KB
/
cryptopriceprediction.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
### Importing libraries
import numpy as np
import pandas as pd
import seaborn as sb
import seaborn as sns
from scipy import stats
import plotly.express as px
from sklearn.svm import SVR
import matplotlib.pyplot as plt
from xgboost import XGBRegressor
import plotly.graph_objects as go
from sklearn.linear_model import BayesianRidge
from sklearn.tree import DecisionTreeRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.linear_model import LinearRegression, Lasso
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
"""### Data preprocessing"""
df = pd.read_csv('crypto-markets.csv')
df.head()
df.info()
df.describe()
df.isna().sum()
#Detect and handle outliers
z_scores = stats.zscore(df.select_dtypes(include=['float64', 'int64']))
abs_z_scores = np.abs(z_scores)
filtered_entries = (abs_z_scores < 3).all(axis=1)
df = df[filtered_entries]
#Scale numerical features
scaler = StandardScaler()
df[df.select_dtypes(include=['float64', 'int64']).columns] = scaler.fit_transform(df.select_dtypes(include=['float64', 'int64']))
df
"""### visualzation"""
fig = go.Figure(data=[go.Scatter(y=df['close'])])
fig.update_layout(title='Crypto Close price.', title_font_size=15, yaxis_title='Price')
fig.show()
fig = go.Figure(data=[go.Scatter(x=df['name'], y=df['close'], name='Closing Price')])
fig.update_layout(title='Closing Price Trend', xaxis_title='name', yaxis_title='Closing Price')
fig.show()
fig = go.Figure(data=[go.Scatter(x=df['date'], y=df['close'], name='Closing Price')])
fig.update_layout(title='Closing Price Trend Over Time', xaxis_title='Date', yaxis_title='Closing Price')
fig.show()
plt.figure(figsize=(10, 6))
sns.histplot(df['close'], bins=50, kde=True)
plt.title('Distribution of Closing Prices')
plt.xlabel('Closing Price')
plt.ylabel('Frequency')
plt.show()
df_positive_ranknow = df[df['ranknow'] > 0]
ranknow_name = df_positive_ranknow.groupby('name')['ranknow'].sum()
sample_data = ranknow_name.sample(20)
colors = sns.color_palette('magma', len(sample_data))
fig = go.Figure(data=[go.Pie(labels=sample_data.index, values=sample_data.values, hole=0.5)])
fig.update_layout(title='Proportion of ranknow by Name')
fig.show()
"""### Feature selection
Correlation Coefficient
"""
numerical_features = df.select_dtypes(include=['float64', 'int64']).columns
corr_matrix = df[numerical_features].corr()
plt.figure(figsize=(12, 8))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
"""Lasso Regression"""
numerical_features = df.select_dtypes(include=['float64', 'int64']).columns
X = df[numerical_features].drop(columns=['close'])
y = df['close']
lasso = Lasso(alpha=0.01)
lasso.fit(X, y)
model = SelectFromModel(lasso, prefit=True)
selected_features = X.columns[model.get_support()]
print('Selected features by Lasso:', selected_features)
"""### Models"""
features = ['open', 'high', 'low', 'volume', 'market', 'spread']
X = df[features]
y = df['close']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
"""### Linear Regression"""
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
LRmae = mean_absolute_error(y_test, y_pred)
LRmse = mean_squared_error(y_test, y_pred)
LRrmse = np.sqrt(LRmse)
LRr2 = r2_score(y_test, y_pred)
print('Mean Absolute Error:', LRmae)
print('Mean Squared Error:', LRmse)
print('Root Mean Squared Error:', LRrmse)
print('R-squared:', LRr2)
results_df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
results_df = results_df.sort_index()
results_df = results_df.merge(df[['date']], left_index=True, right_index=True)
results_df = results_df.sort_values('date')
sns.set(style="whitegrid")
plt.figure(figsize=(12, 6))
sns.lineplot(data=results_df, x=df['date'], y=results_df['Predicted'], label='Predicted Price', color='blue')
plt.xlabel('Date')
plt.ylabel('Predicted')
plt.title('Predicted Price Over Time')
plt.show()
results_df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
results_df = results_df.sort_index()
results_df = results_df.merge(df[['date']], left_index=True, right_index=True)
results_df = results_df.sort_values('date')
plt.figure(figsize=(14, 8))
plt.plot(results_df['date'], results_df['Actual'], label='Actual Prices', color='black', linestyle='-')
plt.plot(results_df['date'], results_df['Predicted'], label='Linear Regression Predicted Prices', linestyle='--', color='blue')
plt.title('Actual vs Predicted Prices (Linear Regression) - Time Series')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
"""### Decision Tree"""
tree_model = DecisionTreeRegressor(random_state=42)
tree_model.fit(X_train, y_train)
y_pred = tree_model.predict(X_test)
DTmae = mean_absolute_error(y_test, y_pred)
DTmse = mean_squared_error(y_test, y_pred)
DTrmse = np.sqrt(DTmse)
DTr2 = r2_score(y_test, y_pred)
print('Mean Absolute Error:', DTmae)
print('Mean Squared Error:', DTmse)
print('Root Mean Squared Error:', DTrmse)
print('R-squared:', DTr2)
results_df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
results_df = results_df.sort_index()
results_df = results_df.merge(df[['date']], left_index=True, right_index=True)
results_df = results_df.sort_values('date')
plt.figure(figsize=(14, 8))
plt.plot(results_df['date'], results_df['Actual'], label='Actual Prices', color='black', linestyle='-')
plt.plot(results_df['date'], results_df['Predicted'], label='Decision Tree Predicted Prices', linestyle='--', color='blue')
plt.title('Actual vs Predicted Prices (Decision Tree) - Time Series')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
"""### Random Forest"""
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
y_pred = rf_model.predict(X_test)
RFmae = mean_absolute_error(y_test, y_pred)
RFmse = mean_squared_error(y_test, y_pred)
RFrmse = np.sqrt(RFmse)
RFr2 = r2_score(y_test, y_pred)
print('Mean Absolute Error:', RFmae)
print('Mean Squared Error:', RFmse)
print('Root Mean Squared Error:', RFrmse)
print('R-squared:', RFr2)
sample_size = 500
sample_indices = np.random.choice(len(y_test), sample_size, replace=False)
y_test_sample = y_test.iloc[sample_indices]
y_pred_sample = y_pred[sample_indices]
plt.figure(figsize=(14, 8))
plt.plot(y_test_sample.index, y_test_sample, label='Actual Prices', color='black', linestyle='-')
plt.plot(y_test_sample.index, y_pred_sample, label='Predicted Prices', linestyle='--', color='blue')
plt.title('Actual vs Predicted Prices')
plt.xlabel('Index')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
results_df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
results_df = results_df.sort_index()
results_df = results_df.merge(df[['date']], left_index=True, right_index=True)
results_df = results_df.sort_values('date')
plt.figure(figsize=(14, 8))
plt.plot(results_df['date'], results_df['Actual'], label='Actual Prices', color='black', linestyle='-')
plt.plot(results_df['date'], results_df['Predicted'], label='Random Forest Predicted Prices', linestyle='--', color='blue')
plt.title('Actual vs Predicted Prices (Random Forest) - Time Series')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
"""### Bayesian Ridge Regression"""
bayesian_model = BayesianRidge()
bayesian_model.fit(X_train, y_train)
y_pred = bayesian_model.predict(X_test)
BRmae = mean_absolute_error(y_test, y_pred)
BRmse = mean_squared_error(y_test, y_pred)
BRrmse = np.sqrt(BRmse)
BRr2 = r2_score(y_test, y_pred)
print('Mean Absolute Error:', BRmae)
print('Mean Squared Error:', BRmse)
print('Root Mean Squared Error:', BRrmse)
print('R-squared:', BRr2)
results_df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
results_df = results_df.sort_index()
results_df = results_df.merge(df[['date']], left_index=True, right_index=True)
results_df = results_df.sort_values('date')
plt.figure(figsize=(14, 8))
plt.plot(results_df['date'], results_df['Actual'], label='Actual Prices', color='black', linestyle='-')
plt.plot(results_df['date'], results_df['Predicted'], label='Bayesian Ridge Regression Predicted Prices', linestyle='--', color='blue')
plt.title('Actual vs Predicted Prices (Bayesian Ridge Regression) - Time Series')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
"""SVR"""
svr_model = SVR(kernel='rbf')
svr_model.fit(X_train, y_train)
y_pred = svr_model.predict(X_test)
SVRmae = mean_absolute_error(y_test, y_pred)
SVRmse = mean_squared_error(y_test, y_pred)
SVRrmse = np.sqrt(SVRmse)
SVRr2 = r2_score(y_test, y_pred)
print('Mean Absolute Error:', SVRmae)
print('Mean Squared Error:', SVRmse)
print('Root Mean Squared Error:', SVRrmse)
print('R-squared:', SVRr2)
"""### Gradient Boosting"""
gb_model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, random_state=42)
gb_model.fit(X_train, y_train)
y_pred = gb_model.predict(X_test)
GBmae = mean_absolute_error(y_test, y_pred)
GBmse = mean_squared_error(y_test, y_pred)
GBrmse = np.sqrt(GBmse)
GBr2 = r2_score(y_test, y_pred)
print('Mean Absolute Error:', GBmae)
print('Mean Squared Error:', GBmse)
print('Root Mean Squared Error:', GBrmse)
print('R-squared:', GBr2)
results_df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
results_df = results_df.sort_index()
results_df = results_df.merge(df[['date']], left_index=True, right_index=True)
results_df = results_df.sort_values('date')
plt.figure(figsize=(14, 8))
plt.plot(results_df['date'], results_df['Actual'], label='Actual Prices', color='black', linestyle='-')
plt.plot(results_df['date'], results_df['Predicted'], label='Gradient Boosting Regressor Predicted Prices', linestyle='--', color='blue')
plt.title('Actual vs Predicted Prices (Gradient Boosting Regressor) - Time Series')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
"""### XGBoost Regressor"""
xgb_model = XGBRegressor(n_estimators=100, learning_rate=0.1, random_state=42)
xgb_model.fit(X_train, y_train)
y_pred = xgb_model.predict(X_test)
XGmae = mean_absolute_error(y_test, y_pred)
XGmse = mean_squared_error(y_test, y_pred)
XGrmse = np.sqrt(XGmse)
XGr2 = r2_score(y_test, y_pred)
print('Mean Absolute Error:', XGmae)
print('Mean Squared Error:', XGmse)
print('Root Mean Squared Error:', XGrmse)
print('R-squared:', XGr2)
results_df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
results_df = results_df.sort_index()
results_df = results_df.merge(df[['date']], left_index=True, right_index=True)
results_df = results_df.sort_values('date')
plt.figure(figsize=(14, 8))
plt.plot(results_df['date'], results_df['Actual'], label='Actual Prices', color='black', linestyle='-')
plt.plot(results_df['date'], results_df['Predicted'], label='XGBoost Regressor Predicted Prices', linestyle='--', color='blue')
plt.title('Actual vs Predicted Prices (XGBoost Regressor) - Time Series')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
"""### Results"""
metrics = {
'Model': ['Linear Regression', 'Decision Tree', 'Random Forest', 'Bayesian Regression', 'Gradient Boosting', 'XGBoost'],
'MAE': [LRmae, DTmae, RFmae, BRmae, GBmae, XGmae],
'MSE': [LRmse, DTmse, RFmse, BRmse, GBmse, XGmse],
'RMSE': [LRrmse, DTrmse, RFrmse, BRrmse, GBrmse, XGrmse],
'R-squared': [LRr2, DTr2, RFr2, BRr2, GBr2, XGr2]
}
plt.figure(figsize=(14, 10))
# MAE plot
plt.subplot(2, 2, 1)
plt.plot(metrics['Model'], metrics['MAE'], marker='o', linestyle='-', color='b', label='MAE')
plt.title('Mean Absolute Error (MAE) Comparison')
plt.xlabel('Models')
plt.ylabel('MAE')
plt.xticks(rotation=45)
plt.grid(True)
plt.legend()
# MSE plot
plt.subplot(2, 2, 2)
plt.plot(metrics['Model'], metrics['MSE'], marker='o', linestyle='-', color='r', label='MSE')
plt.title('Mean Squared Error (MSE) Comparison')
plt.xlabel('Models')
plt.ylabel('MSE')
plt.xticks(rotation=45)
plt.grid(True)
plt.legend()
# RMSE plot
plt.subplot(2, 2, 3)
plt.plot(metrics['Model'], metrics['RMSE'], marker='o', linestyle='-', color='g', label='RMSE')
plt.title('Root Mean Squared Error (RMSE) Comparison')
plt.xlabel('Models')
plt.ylabel('RMSE')
plt.xticks(rotation=45)
plt.grid(True)
plt.legend()
# R-squared plot
plt.subplot(2, 2, 4)
plt.plot(metrics['Model'], metrics['R-squared'], marker='o', linestyle='-', color='m', label='R-squared')
plt.title('R-squared Comparison')
plt.xlabel('Models')
plt.ylabel('R-squared')
plt.xticks(rotation=45)
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()