Skip to content

Commit 10fc90c

Browse files
authored
Added Random Forest Classifier (TheAlgorithms#1738)
* Added Random Forest Regressor * Updated file to standard * Added Random Forest Classifier (Iris dataset) and a Confusion Matrix for result visualization
1 parent 5e3eb12 commit 10fc90c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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()

0 commit comments

Comments
 (0)