Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.ensemble import RandomForestClassifier
- # Define the blood test results for six persons
- person1_results = [80, 90, 100, 110, 120, 0]
- person2_results = [95, 105, 90, 115, 100, 1]
- person3_results = [110, 95, 85, 75, 120, 0]
- person4_results = [75, 80, 85, 90, 95, 1]
- person5_results = [105, 115, 100, 105, 110, 0]
- person6_results = [90, 110, 115, 95, 120, 1]
- # Combine the results into a list
- all_results = [person1_results, person2_results, person3_results, person4_results, person5_results, person6_results]
- # Extract the last column (0 or 1) as labels
- labels = [results[-1] for results in all_results]
- # Remove the last column from the dataset
- data = [results[:-1] for results in all_results]
- # Define the number of rows for testing and learning
- num_testing_rows = 4
- # Split the data into training and testing datasets
- X_train, X_test, y_train, y_test = data[:-num_testing_rows], data[-num_testing_rows:], labels[:-num_testing_rows], labels[-num_testing_rows:]
- # Train a machine learning model (Random Forest classifier)
- clf = RandomForestClassifier()
- clf.fit(X_train, y_train)
- # Use the trained model to predict the last column for the testing dataset
- y_pred = clf.predict(X_test)
- # Set colors for gray, green, and red
- gray_color = (128, 128, 128) # Gray as (128, 128, 128) in RGB
- green_color = (0, 128, 0) # Green as (0, 128, 0) in RGB
- red_color = (255, 0, 0) # Red as (255, 0, 0) in RGB
- # Create an empty array for the first image (based on given data)
- image1 = np.zeros((len(data), len(data[0]) + 1, 3), dtype=np.uint8)
- # Populate image1 with gray and green colors based on the labels in the last column
- for i, label in enumerate(labels):
- for j in range(len(all_results[0])):
- pixel_value = int(np.interp(all_results[i][j], [min(all_results[i]), max(all_results[i])], [0, 255]))
- image1[i, j] = np.array([pixel_value] * 3)
- if label == 0:
- image1[i, -1] = np.array([green_color])
- elif label == 1:
- image1[i, -1] = np.array([red_color])
- # Use the trained model to predict the labels for the test data
- y_pred = clf.predict(X_test)
- # Create an empty array for the second image (based on testing)
- image2 = np.zeros((len(X_test), len(all_results[0]), 3), dtype=np.uint8)
- # Populate image2 with gray and red/green colors based on the predicted labels
- for i, label in enumerate(y_pred):
- for j in range(len(all_results[0])):
- pixel_value = int(np.interp(all_results[i][j], [min(all_results[i]), max(all_results[i])], [0, 255]))
- image2[i, j] = np.array([pixel_value] * 3)
- if label == 0:
- image2[i, -1] = np.array([green_color])
- elif label == 1:
- image2[i, -1] = np.array([red_color])
- # Display both images side by side
- plt.figure(figsize=(12, 5))
- plt.subplot(1, 2, 1)
- plt.imshow(image1)
- plt.title("Learning Data")
- plt.axis("off")
- plt.subplot(1, 2, 2)
- plt.imshow(image2)
- plt.title("Testing Data")
- plt.axis("off")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement