Data Science Asked by matt3526 on July 5, 2021
I have a random forest model built like this:
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
model = RandomForestClassifier(random_state=0)
X_train,X_test,y_train,y_test = train_test_split(X, y, test_size = 0.2, random_state=42, stratify=y)
model.fit(X_train, y_train)
Is there any method I can call (or attribute I can use) to see the column names used to make the model?
Thanks
It is impossible to retrieve column names from a trained Random forest classifier from my experience, there is also a previous answer for an identical question.
Answered by cmn on July 5, 2021
You can get all the Trees using
clf.estimators_
Then you can traverse the Tree using custom code.[Check here] Then you can take it to whatever format you want i.e. Array/DataFrame
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target
clf = RandomForestClassifier(max_depth=3, random_state=0)
clf.fit(X, y)
model = clf.estimators_[0] # Checking the first Tree
from sklearn.tree import export_text
tree_rules = export_text(model, feature_names=iris.feature_names)
print(tree_rules)
Answered by 10xAI on July 5, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP