Learn practical skills, build real-world projects, and advance your career

boruta-example

Use the "Run" button to execute the code.

!pip install boruta
Collecting boruta Downloading Boruta-0.3-py3-none-any.whl (56 kB) |████████████████████████████████| 56 kB 3.6 MB/s Requirement already satisfied: scikit-learn>=0.17.1 in /opt/conda/lib/python3.9/site-packages (from boruta) (1.0.1) Requirement already satisfied: numpy>=1.10.4 in /opt/conda/lib/python3.9/site-packages (from boruta) (1.20.3) Requirement already satisfied: scipy>=0.17.0 in /opt/conda/lib/python3.9/site-packages (from boruta) (1.7.2) Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.9/site-packages (from scikit-learn>=0.17.1->boruta) (3.0.0) Requirement already satisfied: joblib>=0.11 in /opt/conda/lib/python3.9/site-packages (from scikit-learn>=0.17.1->boruta) (1.1.0) Installing collected packages: boruta Successfully installed boruta-0.3
from sklearn.ensemble import RandomForestClassifier
from boruta import BorutaPy
from sklearn.datasets import load_iris

# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target

# Create a random forest classifier with 100 trees
rf = RandomForestClassifier(n_estimators=100, random_state=42)

# Create a Boruta feature selector
selector = BorutaPy(rf, random_state=42)

# Fit the selector to the data
selector.fit(X, y)

# Print the selected features
BorutaPy(estimator=RandomForestClassifier(n_estimators=1000,
                                          random_state=RandomState(MT19937) at 0x7F040DA3E540),
         random_state=RandomState(MT19937) at 0x7F040DA3E540)
 
selected_features = iris.feature_names[selector.support_]
print(selected_features)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_45/2551231490.py in <module> ----> 1 selected_features = iris.feature_names[selector.support_] 2 print(selected_features) TypeError: only integer scalar arrays can be converted to a scalar index