You can download this code by clicking the button below.
This code is now available for download.
This function trains a random forest classifier on the given data and calculates the accuracy on the test set.
Technology Stack : Scikit-learn, NumPy
Code Type : The type of code
Code Difficulty :
import numpy as np
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
def random_forest_classification(X, y, test_size=0.3, random_state=42):
"""
This function generates a random forest classifier model and trains it on the given data.
"""
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=random_state)
# Create a random forest classifier
clf = RandomForestClassifier(n_estimators=100, random_state=random_state)
# Train the classifier
clf.fit(X_train, y_train)
# Predict the labels for the test set
y_pred = clf.predict(X_test)
# Calculate the accuracy of the classifier
accuracy = clf.score(X_test, y_test)
return accuracy
# Example usage:
# X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=42)
# accuracy = random_forest_classification(X, y)
# print(f"Accuracy: {accuracy}")