Skip to content

Lingaraj08/Cryptopriceoprediction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Cryptopriceoprediction

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

Download Bitcoin price data from Yahoo Finance

data = yf.download('BTC-USD', start='2015-01-01', end='2023-01-01')

Feature Engineering: Moving Averages

data['7_day_MA'] = data['Close'].rolling(window=7).mean() data['30_day_MA'] = data['Close'].rolling(window=30).mean()

Handle missing data by dropping rows with NaN values

data = data.dropna()

Feature selection: Open, High, Low, Volume, Moving Averages

features = ['Open', 'High', 'Low', 'Volume', '7_day_MA', '30_day_MA'] target = 'Close'

X = data[features] y = data[target]

Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)

Random Forest Regressor Model

model = RandomForestRegressor(n_estimators=100, random_state=42) model.fit(X_train, y_train)

Predict the closing price

y_pred = model.predict(X_test)

Model Evaluation

mae = mean_absolute_error(y_test, y_pred) mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred)

Predict the closing price

y_pred = model.predict(X_test)

Model Evaluation

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}")

Plot Actual vs Predicted Prices

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()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published