A random forest classifier helps us make better decisions by combining many simple decision trees. It reduces mistakes by averaging their answers.
0
0
Random forest classifier in ML Python
Introduction
When you want to classify emails as spam or not spam.
When predicting if a patient has a disease based on medical data.
When sorting images into categories like cats, dogs, or birds.
When deciding if a loan application should be approved or denied.
When you want a model that works well without much tuning.
Syntax
ML Python
from sklearn.ensemble import RandomForestClassifier model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train, y_train) predictions = model.predict(X_test)
n_estimators sets how many trees the forest has.
random_state makes results repeatable by fixing randomness.
Examples
This creates a forest with 50 trees and trains it on data.
ML Python
model = RandomForestClassifier(n_estimators=50)
model.fit(X_train, y_train)This limits each tree to 5 levels deep to avoid overfitting.
ML Python
model = RandomForestClassifier(n_estimators=200, max_depth=5) model.fit(X_train, y_train)
This gets the predicted classes for new data.
ML Python
predictions = model.predict(X_test)
Sample Program
This program trains a random forest on the iris flower data and shows how well it predicts the flower types.
ML Python
from sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # Load example data iris = load_iris() X, y = iris.data, iris.target # Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Create and train the random forest model model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train, y_train) # Predict on test data predictions = model.predict(X_test) # Calculate accuracy accuracy = accuracy_score(y_test, predictions) print(f"Accuracy: {accuracy:.2f}")
OutputSuccess
Important Notes
Random forests work well even if some trees make mistakes because they vote together.
More trees usually improve accuracy but take longer to train.
Setting random_state helps you get the same results every time you run the code.
Summary
Random forest combines many decision trees to improve prediction.
It is easy to use and works well on many problems.
Adjusting the number of trees and tree depth helps control performance.