import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score import yfinance as yf
data = yf.download('BTC-USD', start='2015-01-01', end='2023-01-01')
data['7_day_MA'] = data['Close'].rolling(window=7).mean() data['30_day_MA'] = data['Close'].rolling(window=30).mean()
data = data.dropna()
features = ['Open', 'High', 'Low', 'Volume', '7_day_MA', '30_day_MA'] target = 'Close'
X = data[features] y = data[target]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)
model = RandomForestRegressor(n_estimators=100, random_state=42) model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mae = mean_absolute_error(y_test, y_pred) mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred)
y_pred = model.predict(X_test)
mae = mean_absolute_error(y_test, y_pred) mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred)
print(f"Mean Absolute Error (MAE): {mae}") print(f"Mean Squared Error (MSE): {mse}") print(f"R-squared (R²): {r2}")
plt.figure(figsize=(12, 6)) plt.plot(y_test.index, y_test, label='Actual', color='blue') plt.plot(y_test.index, y_pred, label='Predicted', color='red') plt.legend() plt.title('Bitcoin Price Prediction: Actual vs Predicted') plt.xlabel('Date') plt.ylabel('Price (USD)') plt.show()