pull/652/head
Soubeer Koley 2024-05-27 19:23:51 +05:30
rodzic 9e1a20de44
commit d138b0e625
1 zmienionych plików z 12 dodań i 10 usunięć

Wyświetl plik

@ -61,7 +61,7 @@ Hyperparameter tuning can significantly improve the performance of a Random Fore
#### Classification Example #### Classification Example
Below is a simple example of using Random Forest for a classification task with the Iris dataset. Below is a simple example of using Random Forest for a classification task with the Iris dataset.
''' ```
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from sklearn.datasets import load_iris from sklearn.datasets import load_iris
@ -69,6 +69,7 @@ from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report from sklearn.metrics import accuracy_score, classification_report
# Load dataset # Load dataset
iris = load_iris() iris = load_iris()
X, y = iris.data, iris.target X, y = iris.data, iris.target
@ -90,13 +91,13 @@ accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%") print(f"Accuracy: {accuracy * 100:.2f}%")
print("Classification Report:\n", classification_report(y_test, y_pred)) print("Classification Report:\n", classification_report(y_test, y_pred))
''' ```
#### Feature Importance #### Feature Importance
Random Forest provides a way to measure the importance of each feature in making predictions. Random Forest provides a way to measure the importance of each feature in making predictions.
''' ```
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
# Get feature importances # Get feature importances
@ -115,11 +116,11 @@ plt.bar(range(X.shape[1]), importances[indices], align='center')
plt.xticks(range(X.shape[1]), indices) plt.xticks(range(X.shape[1]), indices)
plt.xlim([-1, X.shape[1]]) plt.xlim([-1, X.shape[1]])
plt.show() plt.show()
''' ```
#### Hyperparameter Tuning #### Hyperparameter Tuning
Using Grid Search for hyperparameter tuning. Using Grid Search for hyperparameter tuning.
''' ```
from sklearn.model_selection import GridSearchCV from sklearn.model_selection import GridSearchCV
# Define the parameter grid # Define the parameter grid
@ -138,11 +139,11 @@ grid_search.fit(X_train, y_train)
# Print the best parameters # Print the best parameters
print("Best parameters found: ", grid_search.best_params_) print("Best parameters found: ", grid_search.best_params_)
Regression Example ```
#### Regression Example
Below is a simple example of using Random Forest for a regression task with the Boston housing dataset. Below is a simple example of using Random Forest for a regression task with the Boston housing dataset.
python ```
Copy code
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from sklearn.datasets import load_boston from sklearn.datasets import load_boston
@ -171,10 +172,11 @@ mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred) r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse:.2f}") print(f"Mean Squared Error: {mse:.2f}")
print(f"R^2 Score: {r2:.2f}") print(f"R^2 Score: {r2:.2f}")
Conclusion ```
### Conclusion
Random Forest is a powerful and flexible machine learning algorithm that can handle both classification and regression tasks. Its ability to create an ensemble of decision trees leads to robust and accurate models. However, it is important to be mindful of the computational cost associated with training multiple trees. Random Forest is a powerful and flexible machine learning algorithm that can handle both classification and regression tasks. Its ability to create an ensemble of decision trees leads to robust and accurate models. However, it is important to be mindful of the computational cost associated with training multiple trees.
References ### References
Scikit-learn Random Forest Documentation Scikit-learn Random Forest Documentation
Wikipedia: Random Forest Wikipedia: Random Forest
Machine Learning Mastery: Introduction to Random Forest Machine Learning Mastery: Introduction to Random Forest