Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import tensorflow as tf
- from tensorflow.keras.models import Sequential
- from tensorflow.keras.layers import SimpleRNN, Dense
- # Data preparation in mmol/L
- glucose_values_mmol = np.array([5.0, 4.4, 5.3, 4.2, 3.9, 4.7, 5.3, 4.7, 4.4, 3.9, 4.2])
- # Data normalization
- glucose_normalized = (glucose_values_mmol - np.min(glucose_values_mmol)) / (np.max(glucose_values_mmol) - np.min(glucose_values_mmol))
- # Prepare training data
- X = glucose_normalized[:-1]
- y = glucose_normalized[1:]
- # Create the model
- model = Sequential([
- SimpleRNN(units=1, input_shape=(1, 1), activation='linear'),
- Dense(units=1, activation='linear')
- ])
- # Compile the model
- model.compile(optimizer='adam', loss='mean_squared_error')
- # Reshape the data for training
- X = X.reshape(-1, 1, 1)
- y = y.reshape(-1, 1)
- # Train the model
- model.fit(X, y, epochs=100)
- # Predict based on previous values
- X_test = X # Using the same data for prediction
- y_pred_normalized = model.predict(X_test)
- y_pred = y_pred_normalized * (np.max(glucose_values_mmol) - np.min(glucose_values_mmol)) + np.min(glucose_values_mmol)
- # Display the results
- x_values = np.arange(1, len(glucose_values_mmol))
- x_values_pred = np.arange(2, len(glucose_values_mmol) + 1)
- plt.plot(x_values, glucose_values_mmol[:-1], marker='o', label='Actual values')
- plt.plot(x_values_pred, y_pred, marker='o', label='Prediction')
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement