Advertisement
max2201111

GPU glucose2

Nov 7th, 2023
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | Science | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn.ensemble import RandomForestClassifier
  4.  
  5. # Define the blood test results for six persons
  6. person1_results = [80, 90, 100, 110, 120, 0]
  7. person2_results = [95, 105, 90, 115, 100, 1]
  8. person3_results = [110, 95, 85, 75, 120, 0]
  9. person4_results = [75, 80, 85, 90, 95, 1]
  10. person5_results = [105, 115, 100, 105, 110, 0]
  11. person6_results = [90, 110, 115, 95, 120, 1]
  12.  
  13. # Combine the results into a list
  14. all_results = [person1_results, person2_results, person3_results, person4_results, person5_results, person6_results]
  15.  
  16. # Extract the last column (0 or 1) as labels
  17. labels = [results[-1] for results in all_results]
  18.  
  19. # Remove the last column from the dataset
  20. data = [results[:-1] for results in all_results]
  21.  
  22. # Define the number of rows for testing and learning
  23. num_testing_rows = 4
  24.  
  25. # Split the data into training and testing datasets
  26. X_train, X_test, y_train, y_test = data[:-num_testing_rows], data[-num_testing_rows:], labels[:-num_testing_rows], labels[-num_testing_rows:]
  27.  
  28. # Train a machine learning model (Random Forest classifier)
  29. clf = RandomForestClassifier()
  30. clf.fit(X_train, y_train)
  31.  
  32. # Use the trained model to predict the last column for the testing dataset
  33. y_pred = clf.predict(X_test)
  34.  
  35. # Set colors for gray, green, and red
  36. gray_color = (128, 128, 128)  # Gray as (128, 128, 128) in RGB
  37. green_color = (0, 128, 0)  # Green as (0, 128, 0) in RGB
  38. red_color = (255, 0, 0)  # Red as (255, 0, 0) in RGB
  39.  
  40. # Create an empty array for the first image (based on given data)
  41. image1 = np.zeros((len(data), len(data[0]) + 1, 3), dtype=np.uint8)
  42.  
  43. # Populate image1 with gray and green colors based on the labels in the last column
  44. for i, label in enumerate(labels):
  45.     for j in range(len(all_results[0])):
  46.         pixel_value = int(np.interp(all_results[i][j], [min(all_results[i]), max(all_results[i])], [0, 255]))
  47.         image1[i, j] = np.array([pixel_value] * 3)
  48.     if label == 0:
  49.         image1[i, -1] = np.array([green_color])
  50.     elif label == 1:
  51.         image1[i, -1] = np.array([red_color])
  52.  
  53. # Use the trained model to predict the labels for the test data
  54. y_pred = clf.predict(X_test)
  55.  
  56. # Create an empty array for the second image (based on testing)
  57. image2 = np.zeros((len(X_test), len(all_results[0]), 3), dtype=np.uint8)
  58.  
  59. # Populate image2 with gray and red/green colors based on the predicted labels
  60. for i, label in enumerate(y_pred):
  61.     for j in range(len(all_results[0])):
  62.         pixel_value = int(np.interp(all_results[i][j], [min(all_results[i]), max(all_results[i])], [0, 255]))
  63.         image2[i, j] = np.array([pixel_value] * 3)
  64.     if label == 0:
  65.         image2[i, -1] = np.array([green_color])
  66.     elif label == 1:
  67.         image2[i, -1] = np.array([red_color])
  68.  
  69. # Display both images side by side
  70. plt.figure(figsize=(12, 5))
  71. plt.subplot(1, 2, 1)
  72. plt.imshow(image1)
  73. plt.title("Learning Data")
  74. plt.axis("off")
  75.  
  76. plt.subplot(1, 2, 2)
  77. plt.imshow(image2)
  78. plt.title("Testing Data")
  79. plt.axis("off")
  80.  
  81. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement