Subskrybuj nasz Newsletter i Odbierz Natychmiastowe 20% Zniżki!

# Zelle 1: Importieren der benötigten Bibliotheken import pandas as pd import yfinance as yf import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, LSTM # Zelle 2: Datenbeschaffung auf stündlicher Basis btc_data = yf.download('BTC-USD', interval='1h', start='2023-01-01', end='2023-12-31') btc_data.dropna(inplace=True) # Normalisierung der Daten scaler = MinMaxScaler(feature_range=(0, 1)) btc_data['Close'] = scaler.fit_transform(btc_data['Close'].values.reshape(-1, 1)) # Zelle 3: Merkmalserstellung und Datenaufbereitung def create_dataset(data, time_step=1): dataX, dataY = [], [] for i in range(len(data) - time_step - 1): a = data[i:(i + time_step), 0] dataX.append(a) dataY.append(data[i + time_step, 0]) return np.array(dataX), np.array(dataY) close_data = btc_data['Close'].values close_data = close_data.reshape(-1, 1) training_size = int(len(close_data) * 0.8) test_size = len(close_data) - training_size train_data, test_data = close_data[0:training_size, :], close_data[training_size:len(close_data), :] time_step = 24 X_train, y_train = create_dataset(train_data, time_step) X_test, y_test = create_dataset(test_data, time_step) X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1) X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1) # Zelle 4: LSTM-Modell erstellen und trainieren model = Sequential() model.add(LSTM(50, return_sequences=True, input_shape=(time_step, 1))) model.add(LSTM(50, return_sequences=False)) model.add(Dense(25)) model.add(Dense(1)) model.compile(optimizer='adam', loss='mean_squared_error') model.fit(X_train, y_train, batch_size=1, epochs=1) # Zelle 5: Vorhersagen und Bewertung train_predict = model.predict(X_train) test_predict = model.predict(X_test) train_predict = scaler.inverse_transform(train_predict) test_predict = scaler.inverse_transform(test_predict) y_train = scaler.inverse_transform([y_train]) y_test = scaler.inverse_transform([y_test]) # Plot der Ergebnisse plt.figure(figsize=(12, 6)) plt.plot(scaler.inverse_transform(close_data), label='Original Daten') plt.plot(range(time_step, len(train_predict) + time_step), train_predict, label='Trainingsdaten Vorhersage') plt.plot(range(len(train_predict) + (time_step * 2) + 1, len(close_data) - 1), test_predict, label='Testdaten Vorhersage') plt.title('Bitcoin Preis Vorhersage mit LSTM (Stündlich)') plt.xlabel('Datum') plt.ylabel('Preis (USD)') plt.legend() plt.show() # Zelle 6: Echtzeit-Vorhersage def predict_next_hours(data, model, scaler, time_step, hours): last_data = data[-time_step:] last_data_scaled = scaler.transform(last_data) X_input = last_data_scaled.reshape(1, -1, 1) predictions = [] for _ in range(hours): pred = model.predict(X_input) predictions.append(pred[0, 0]) X_input = np.append(X_input[:, 1:, :], [[pred]], axis=1) predictions = scaler.inverse_transform(np.array(predictions).reshape(-1, 1)) return predictions # Vorhersage für die nächsten 6 Stunden next_6_hours_prediction = predict_next_hours(close_data, model, scaler, time_step, 6) print("Vorhersage für die nächsten 6 Stunden:") print(next_6_hours_prediction)
>