GeezerVison is showing github.com — Added Random Forest Classifier (#1738) · jusing-es/Python@10fc90c
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +# Random Forest Classifier Example |
| 2 | + |
| 3 | +from sklearn.datasets import load_iris |
| 4 | +from sklearn.model_selection import train_test_split |
| 5 | +from sklearn.ensemble import RandomForestClassifier |
| 6 | +from sklearn.metrics import plot_confusion_matrix |
| 7 | +import matplotlib.pyplot as plt |
| 8 | + |
| 9 | + |
| 10 | +def main(): |
| 11 | + |
| 12 | + """ |
| 13 | + Random Tree Classifier Example using sklearn function. |
| 14 | + Iris type dataset is used to demonstrate algorithm. |
| 15 | + """ |
| 16 | + |
| 17 | + # Load Iris house price dataset |
| 18 | + iris = load_iris() |
| 19 | + |
| 20 | + # Split dataset into train and test data |
| 21 | + X = iris["data"] # features |
| 22 | + Y = iris["target"] |
| 23 | + x_train, x_test, y_train, y_test = train_test_split( |
| 24 | + X, Y, test_size=0.3, random_state=1 |
| 25 | + ) |
| 26 | + |
| 27 | + # Random Forest Classifier |
| 28 | + rand_for = RandomForestClassifier(random_state=42, n_estimators=100) |
| 29 | + rand_for.fit(x_train, y_train) |
| 30 | + |
| 31 | + # Display Confusion Matrix of Classifier |
| 32 | + plot_confusion_matrix( |
| 33 | + rand_for, |
| 34 | + x_test, |
| 35 | + y_test, |
| 36 | + display_labels=iris["target_names"], |
| 37 | + cmap="Blues", |
| 38 | + normalize="true", |
| 39 | + ) |
| 40 | + plt.title("Normalized Confusion Matrix - IRIS Dataset") |
| 41 | + plt.show() |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + main() |