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 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 a custom RNN-like model
- model = Sequential()
- model.add(Dense(units=1, input_shape=(1,)))
- # Compile the model
- model.compile(optimizer='adam', loss='mean_squared_error')
- # Reshape the data for training
- X = X.reshape(-1, 1)
- y = y.reshape(-1, 1)
- # Train the model
- model.fit(X, y, epochs=100)
- # Predict based on previous values
- y_pred_normalized = model.predict(X)
- # Convert predictions back to the original scale
- 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))
- plt.plot(x_values, glucose_values_mmol[:-1], marker='o', label='Actual values')
- plt.plot(x_values, y_pred, marker='o', label='Prediction')
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement