Advertisement
max2201111

GPU glucose

Oct 21st, 2023
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | Science | 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 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 a custom RNN-like model
  18. model = Sequential()
  19. model.add(Dense(units=1, input_shape=(1,)))
  20.  
  21. # Compile the model
  22. model.compile(optimizer='adam', loss='mean_squared_error')
  23.  
  24. # Reshape the data for training
  25. X = X.reshape(-1, 1)
  26. y = y.reshape(-1, 1)
  27.  
  28. # Train the model
  29. model.fit(X, y, epochs=100)
  30.  
  31. # Predict based on previous values
  32. y_pred_normalized = model.predict(X)
  33.  
  34. # Convert predictions back to the original scale
  35. y_pred = y_pred_normalized * (np.max(glucose_values_mmol) - np.min(glucose_values_mmol)) + np.min(glucose_values_mmol)
  36.  
  37. # Display the results
  38. x_values = np.arange(1, len(glucose_values_mmol))
  39.  
  40. plt.plot(x_values, glucose_values_mmol[:-1], marker='o', label='Actual values')
  41. plt.plot(x_values, y_pred, marker='o', label='Prediction')
  42. plt.legend()
  43. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement