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
iris = load_iris()
X = iris.data
y = iris.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(n_estimators=100)
rf.fit(X_train, y_train)

# Step 5: Make predictions on the test set
predictions = rf.predict(X_test)

# Step 6: Evaluate the model by calculating the accuracy
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
boston = load_boston()
X = boston.data
y = boston.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(n_estimators=100)
rf.fit(X_train, y_train)

# Step 5: Make predictions on the test set
predictions = rf.predict(X_test)

# Step 6: Evaluate the model by calculating the mean squared error
mse = mean_squared_error(y_test, predictions)
print("Mean Squared Error: ", mse)

Comments

Popular posts from this blog

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

What are the different evaluation metrics used in machine learning?

Sorting Algorithms in Python? - with practical example