File size: 2,161 Bytes
f91ab19 29fb74a f91ab19 b920bb5 f91ab19 b920bb5 f91ab19 | 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 | import yfinance as yf
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error
from math import sqrt
import numpy as np
import pandas as pd
import streamlit as st
inp = st.text_input('fak u')
def predict_crypto_prices():
data = yf.download(inp, period='max')
# Create a new "lagged" dataframe for the model
data['Previous_Open'] = data['Open'].shift(1)
data['Previous_Close'] = data['Close'].shift(1)
data['Previous_Low'] = data['Low'].shift(1)
data['Previous_High'] = data['High'].shift(1)
# Drop the missing values created by shifting
data = data.dropna()
# Define inputs and outputs for each prediction
features = ['Previous_Open', 'Previous_Close', 'Previous_Low', 'Previous_High']
predictions = ['Open', 'Close', 'Low', 'High']
predicted_prices = pd.DataFrame(columns=predictions)
for feature, prediction in zip(features, predictions):
X = data[feature].values.reshape(-1, 1)
y = data[prediction].values
# Split the data into training and testing datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize XGBoost regressor model
model = XGBRegressor(n_estimators=100, learning_rate=0.1, objective='reg:squarederror')
# Train the model
model.fit(X_train, y_train)
# Make predictions
test_predictions = model.predict(X_test)
test_rmse = sqrt(mean_squared_error(y_test, test_predictions))
# Predict the future price using the most recent price
latest_price = data[prediction].iloc[-1]
# Reshape the input
latest_price_reshaped = np.array(latest_price).reshape(1, -1)
# Make prediction for the next day
predicted_price = model.predict(latest_price_reshaped)
predicted_prices[prediction] = predicted_price
return predicted_prices
predicted_prices = predict_crypto_prices()
if button('Predict'):
st.write(predicted_prices) |