How to implement a random forest model in Python?

In the first example, we are going to implement a random forest model for a classification problem using the famous Iris dataset.
# Step 1: Import necessary libraries
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Step 2: Load the Iris dataset
data = load_iris()
X = data.data
y = data.target

# Step 3: Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 4: Create a Random Forest classifier and fit the model
rf = RandomForestClassifier()
rf.fit(X_train, y_train)

# Step 5: Make predictions on the test set and calculate accuracy
predictions = rf.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy)

In the second example, we are going to implement a random forest model for a regression problem using the Boston housing dataset.
# Step 1: Import necessary libraries
from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# Step 2: Load the Boston housing dataset
data = load_boston()
X = data.data
y = data.target

# Step 3: Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 4: Create a Random Forest regressor and fit the model
rf = RandomForestRegressor()
rf.fit(X_train, y_train)

# Step 5: Make predictions on the test set and calculate mean squared error
predictions = rf.predict(X_test)
mse = mean_squared_error(y_test, predictions)
print("Mean Squared Error:", mse)

These examples demonstrate how to implement random forest models in Python for both classification and regression tasks using different datasets. By following these steps, you can easily build and evaluate random forest models for your own machine learning projects.

Comments

Popular posts from this blog

What are the different types of optimization algorithms used in deep learning?

What are the different evaluation metrics used in machine learning?

What is the difference between a module and a package in Python?