Scikit Learn Learn InlyationForest:如何使用不同的参数拟合多个数据框(不使用GridSearchCV)
我有一个五个单独的pandas数据框架,我将其放在字典中。 我想在Scikit-Learn中运行五个单独的隔离式模型 每个模型的参数集。但是,我不想隔离每个型号。
所以我的问题是,如何运行这些模型并获得决策功能, 对所有数据范围的所有数据范围的预测。我这样做的尝试在下面。
# parameters for each of the five models in a list. The index position in the list
# represents a dataset's parameters, from dataset0 through dataset4
n_estimators = [150, 200, 125, 125, 125]
max_samples = [0.70, 0.70, 0.80, 0.70, 0.70]
max_features = [1, 4, 2, 2, 3]
contamination = [0.05, 0.06, 0.05, 0.07, 0.05]
# numeric columns
num_columns = list(subset_features[1:])
# column transformer
num_transformer = Pipeline([('impute', IterativeImputer()), ('scale', StandardScaler())])
ct = ColumnTransformer([('num_pipeline', num_transformer, num_columns)])
clf = Pipeline([('ct', ct),
('iforest', IsolationForest(n_estimators=n_estimators[i],
max_samples=max_samples[i],
max_features=max_features[i],
contamination=contamination[i],
n_jobs=4,
random_state=None))])
clf_res = {}
for i, df in enumerate(dfs.values()):
print('starting idx:', i)
clf_res[i] = clf.fit(df)
我遇到的问题是,它不是通过不同的参数集迭代 数据范围从迭代变为迭代。请参阅下文:
{0: Pipeline(steps=[('ct',
ColumnTransformer(transformers=[('numeric_pipeline',
Pipeline(steps=[('impute',
IterativeImputer()),
('scale',
StandardScaler())]),
['V1', 'V2', 'V3',
'V4'])])),
('iforest',
IsolationForest(contamination=0.05, max_features=1,
max_samples=0.7, n_estimators=125,
n_jobs=4))]),
1: Pipeline(steps=[('ct',
ColumnTransformer(transformers=[('numeric_pipeline',
Pipeline(steps=[('impute',
IterativeImputer()),
('scale',
StandardScaler())]),
['V1', 'V2', 'V3',
'V4'])])),
('iforest',
IsolationForest(contamination=0.05, max_features=1,
max_samples=0.7, n_estimators=125,
n_jobs=4))])
因此,我想要的是,随着数据范围的变化,参数将会发生变化。
I have a five separate pandas dataframes that I've put inside a dictionary.
I want to run five separate IsolationForest models in scikit-learn with different
sets of parameters for each model. However, I don't want to run each model separtely.
So my question is, how can I run these models and get the decision functions and
predictions for all dataframes in on go. My attempt at doing so is below.
# parameters for each of the five models in a list. The index position in the list
# represents a dataset's parameters, from dataset0 through dataset4
n_estimators = [150, 200, 125, 125, 125]
max_samples = [0.70, 0.70, 0.80, 0.70, 0.70]
max_features = [1, 4, 2, 2, 3]
contamination = [0.05, 0.06, 0.05, 0.07, 0.05]
# numeric columns
num_columns = list(subset_features[1:])
# column transformer
num_transformer = Pipeline([('impute', IterativeImputer()), ('scale', StandardScaler())])
ct = ColumnTransformer([('num_pipeline', num_transformer, num_columns)])
clf = Pipeline([('ct', ct),
('iforest', IsolationForest(n_estimators=n_estimators[i],
max_samples=max_samples[i],
max_features=max_features[i],
contamination=contamination[i],
n_jobs=4,
random_state=None))])
clf_res = {}
for i, df in enumerate(dfs.values()):
print('starting idx:', i)
clf_res[i] = clf.fit(df)
The issue I have is that it is not iterating through the different sets of parameters as the
dataframes change from iteration to iteration. See below:
{0: Pipeline(steps=[('ct',
ColumnTransformer(transformers=[('numeric_pipeline',
Pipeline(steps=[('impute',
IterativeImputer()),
('scale',
StandardScaler())]),
['V1', 'V2', 'V3',
'V4'])])),
('iforest',
IsolationForest(contamination=0.05, max_features=1,
max_samples=0.7, n_estimators=125,
n_jobs=4))]),
1: Pipeline(steps=[('ct',
ColumnTransformer(transformers=[('numeric_pipeline',
Pipeline(steps=[('impute',
IterativeImputer()),
('scale',
StandardScaler())]),
['V1', 'V2', 'V3',
'V4'])])),
('iforest',
IsolationForest(contamination=0.05, max_features=1,
max_samples=0.7, n_estimators=125,
n_jobs=4))])
So what I want is that the parameters will change as the dataframes changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过仔细审查我的代码以及一些谷歌搜索后,我发现了我的代码出了问题。我将在这里为未来可能有类似问题的其他任何人分享。
我对代码的唯一更改是将
CLF Pipeline
带入for循环。正确输出的样本在下面
After careful review of my code, and a little bit of googling, I found out what was wrong with my code. I am sharing it here for anyone else who may have similar problems in the future.
The only change I made to my code was to bring
clf pipeline
into the for loop.Sample of correct output is below