-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrypto_Price_Predictor.py
65 lines (55 loc) · 2.05 KB
/
Crypto_Price_Predictor.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
import os
import pandas as pd
import numpy as np
import joblib
import matplotlib.pyplot as plt
# Directory containing the dataset
data_dir = "Dataset"
model_dir = "Models"
# Function to preprocess data
def preprocess_data(df):
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
df.dropna(subset=['Date'], inplace=True)
df.set_index('Date', inplace=True)
df = df[['Open', 'High', 'Low', 'Close', 'Volume', 'Marketcap']]
df.fillna(method='ffill', inplace=True)
df.replace(0, np.nan, inplace=True) # Replace 0 values with NaN
df.dropna(inplace=True) # Drop rows with NaN values
return df
# Load datasets
def load_datasets(data_dir):
datasets = {}
for filename in os.listdir(data_dir):
if filename.endswith('.csv'):
coin_name = filename.split('_')[1].split('.')[0]
df = pd.read_csv(os.path.join(data_dir, filename))
datasets[coin_name] = preprocess_data(df)
return datasets
# Predict future prices
def predict_future_prices(model, df):
X = df[['Open', 'High', 'Low', 'Close', 'Volume', 'Marketcap']]
future_prices = model.predict(X)
return future_prices
# Main function
def main(coin_name):
model_filename = os.path.join(model_dir, f'{coin_name}_model.pkl')
if not os.path.exists(model_filename):
print(f'Model for {coin_name} not found!')
return
model = joblib.load(model_filename)
datasets = load_datasets(data_dir)
if coin_name not in datasets:
print(f'Dataset for {coin_name} not found!')
return
df = datasets[coin_name]
future_prices = predict_future_prices(model, df)
plt.figure(figsize=(14, 7))
plt.plot(df.index, future_prices, label='Predicted Future Prices')
plt.xlabel('Date')
plt.ylabel('Price')
plt.title(f'Predicted Future Prices for {coin_name}')
plt.legend()
plt.show()
if __name__ == "__main__":
coin_name = input("Enter the cryptocurrency name (e.g., Bitcoin, Ethereum): ")
main(coin_name)