Advertisement
max2201111

JupyterN

Oct 19th, 2023
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | Software | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import tensorflow as tf
  4. from tensorflow.keras.models import Sequential
  5. from tensorflow.keras.layers import SimpleRNN, Dense
  6.  
  7. # Data preparation in mmol/L
  8. 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])
  9.  
  10. # Data normalization
  11. glucose_normalized = (glucose_values_mmol - np.min(glucose_values_mmol)) / (np.max(glucose_values_mmol) - np.min(glucose_values_mmol))
  12.  
  13. # Prepare training data
  14. X = glucose_normalized[:-1]
  15. y = glucose_normalized[1:]
  16.  
  17. # Create the model
  18. model = Sequential([
  19.     SimpleRNN(units=1, input_shape=(1, 1), activation='linear'),
  20.     Dense(units=1, activation='linear')
  21. ])
  22.  
  23. # Compile the model
  24. model.compile(optimizer='adam', loss='mean_squared_error')
  25.  
  26. # Reshape the data for training
  27. X = X.reshape(-1, 1, 1)
  28. y = y.reshape(-1, 1)
  29.  
  30. # Train the model
  31. model.fit(X, y, epochs=100)
  32.  
  33. # Predict based on previous values
  34. X_test = X  # Using the same data for prediction
  35. y_pred_normalized = model.predict(X_test)
  36. y_pred = y_pred_normalized * (np.max(glucose_values_mmol) - np.min(glucose_values_mmol)) + np.min(glucose_values_mmol)
  37.  
  38. # Display the results
  39. x_values = np.arange(1, len(glucose_values_mmol))
  40. x_values_pred = np.arange(2, len(glucose_values_mmol) + 1)
  41.  
  42. plt.plot(x_values, glucose_values_mmol[:-1], marker='o', label='Actual values')
  43. plt.plot(x_values_pred, y_pred, marker='o', label='Prediction')
  44. plt.legend()
  45. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement