Apply Random Forest Regressor and check score
时间: 2024-03-12 21:42:57 浏览: 68
Random Forest Regressor is a machine learning algorithm that can be used to model nonlinear relationships between input features and target variable. It is an ensemble learning method that constructs multiple decision trees and aggregates their predictions to make a final prediction.
To apply Random Forest Regressor to your data, you can use the RandomForestRegressor class from the scikit-learn library. First, split your data into training and test sets, and then instantiate the RandomForestRegressor class with your preferred hyperparameters such as number of trees, maximum depth of trees, and so on. Finally, fit the model on the training data and evaluate its performance on the test data.
Here is an example code:
```
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Instantiate Random Forest Regressor with 100 trees and maximum depth of 5
rf = RandomForestRegressor(n_estimators=100, max_depth=5, random_state=42)
# Fit model on training data
rf.fit(X_train, y_train)
# Predict on test data
y_pred = rf.predict(X_test)
# Evaluate model performance using R-squared score
r2 = r2_score(y_test, y_pred)
print("R-squared score:", r2)
```
You can try different hyperparameters to find the combination that gives the best performance on your data.
阅读全文