from sklearn.ensemble import RandomForestClassifier
时间: 2024-02-16 21:32:00 浏览: 107
Random Forest Classifier is a machine learning algorithm that belongs to the ensemble learning method. It is a collection of decision trees where each tree is built using a random subset of the features and the data. The algorithm then combines the predictions of each individual tree to make a final prediction. In scikit-learn, you can use the `RandomForestClassifier` class to implement this algorithm. Here is an example code snippet:
```
from sklearn.ensemble import RandomForestClassifier
# Create a Random Forest Classifier with 100 trees
rf_classifier = RandomForestClassifier(n_estimators=100)
# Train the model on the training data
rf_classifier.fit(X_train, y_train)
# Make predictions on the test data
y_pred = rf_classifier.predict(X_test)
# Evaluate the model performance
accuracy = rf_classifier.score(X_test, y_test)
```
In this example, `X_train` and `y_train` are the training data features and labels, `X_test` and `y_test` are the test data features and labels, and `n_estimators` is the number of trees in the forest. The `score()` method returns the mean accuracy on the given test data and labels.
阅读全文