【发布时间】:2020-07-27 10:04:47
【问题描述】:
我想知道我的 RF 模型中的功能名称。我读到heregs.best_estimator_.named_steps["stepname"].feature_importances_ 的输出会从我的数据中镜像我的列。但是,gs.best_estimator_.... 的长度是 10,我有 13 列。有些列并不重要。从周围的其他答案(answer1,answer2)来看,我必须在我的管道中声明一些东西。但我对声明什么感到困惑,因为这两个答案都涉及 PCA,而不是 RF。
这是我目前所拥有的。
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn import datasets
# use iris as example
iris = datasets.load_iris()
X = iris.drop(['sepal_length'],axis=1)
y = iris.sepal_length
cats_feats = ['species']
X_train, X_test, y_train, y_test = \
train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=13)
# Pipeline
categorical_transformer = Pipeline(steps=[
('onehot', OneHotEncoder(handle_unknown='ignore',sparse=False))
])
# Bundle any preprocessing
preprocessor = ColumnTransformer(
transformers=[
('cat', categorical_transformer, cat_feats)
])
rf = RandomForestRegressor(random_state = 13)
mymodel = Pipeline(steps = [('preprocessor', preprocessor),
('model', rf)
])
# For this example, I used default values. In reality I do use a dictionary of parameters
gs = GridSearchCV(mymodel
,n_jobs = -1
,cv = 5
)
gs.fit(X_train,y_train)
【问题讨论】:
标签: python scikit-learn feature-selection grid-search