If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Here's a Python code example using scikit-learn to implement a Random Forest classifier for a simple classification task:
pythonCopy codeimport numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Generate synthetic dataset
X, y = make_classification(n_samples=100, n_features=2, n_informative=2, n_redundant=0, random_state=42)
# 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)
# Create and fit the Random Forest classifier
classifier = RandomForestClassifier(n_estimators=100, random_state=42)
classifier.fit(X_train, y_train)
# Make predictions on the test set
y_pred = classifier.predict(X_test)
# Calculate accuracy on the test set
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# Visualize the decision boundary (optional)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1))
Z = classifier.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Random Forest Classifier')
plt.show()
In this implementation, we used scikit-learn to create and train a Random Forest classifier. The process is as follows:
We imported the necessary libraries, including numpy, matplotlib.pyplot, make_classification to generate synthetic data, train_test_split from sklearn.model_selection to split the data into training and testing sets, RandomForestClassifier from sklearn.ensemble to create the Random Forest classifier, and accuracy_score from sklearn.metrics to calculate the accuracy of the model.
We generated a synthetic dataset with make_classification consisting of two informative features and two classes.
We split the dataset into training and testing sets using train_test_split.
We created an instance of RandomForestClassifier as classifier with n_estimators=100, which specifies the number of decision trees in the forest.
We fitted the classifier with the training data using classifier.fit(X_train, y_train).
We made predictions on the test set using classifier.predict(X_test).
We calculated the accuracy of the model on the test set using accuracy_score.
Finally, we optionally visualized the decision boundary of the Random Forest classifier using matplotlib. The decision boundary shows how the classifier separates the two classes in the feature space.
Keep in mind that this is a basic implementation of a Random Forest classifier for a synthetic dataset. In practice, you may need to tune hyperparameters, handle missing data, and preprocess the features for real-world datasets. Additionally, Random Forest can also be used for regression tasks using RandomForestRegressor from scikit-learn.
Start the conversation!
Be the first to share your thoughts
Quick answers to common questions about our courses, quizzes, and learning platform
Didn't find what you're looking for?
Contact Support