相关疑难解决方法(0)

将自定义函数放入 Sklearn 管道中

在我的分类方案中,有几个步骤,包括:

  1. SMOTE(合成少数过采样技术)
  2. 特征选择的 Fisher 标准
  3. 标准化(Z-score 标准化)
  4. SVC(支持向量分类器)

在上述方案中要调整的主要参数是百分位数 (2.) 和 SVC (4.) 的超参数,我想通过网格搜索进行调整。

当前的解决方案构建了一个“部分”管道,包括方案中的第 3 步和第 4 步,并将方案clf = Pipeline([('normal',preprocessing.StandardScaler()),('svc',svm.SVC(class_weight='auto'))]) 分解为两部分:

  1. 调整特征的百分位数以保持通过第一次网格搜索

    skf = StratifiedKFold(y)
    for train_ind, test_ind in skf:
        X_train, X_test, y_train, y_test = X[train_ind], X[test_ind], y[train_ind], y[test_ind]
        # SMOTE synthesizes the training data (we want to keep test data intact)
        X_train, y_train = SMOTE(X_train, y_train)
        for percentile in percentiles:
            # Fisher returns the indices of the selected features specified by the parameter 'percentile'
            selected_ind = Fisher(X_train, …
    Run Code Online (Sandbox Code Playgroud)

pipeline machine-learning feature-selection scikit-learn cross-validation

10
推荐指数
2
解决办法
2万
查看次数

使用 Imblearn 管道和 GridSearchCV 进行交叉验证

我正在尝试使用Pipeline来自的类imblearn并GridSearchCV获得用于对不平衡数据集进行分类的最佳参数。由于每答案提到这里,我要离开了验证集的重采样,只有重新采样训练集,其中imblearn的Pipeline似乎是在做。但是,在实施已接受的解决方案时出现错误。请让我知道我做错了什么。下面是我的实现:

def imb_pipeline(clf, X, y, params):

    model = Pipeline([
        ('sampling', SMOTE()),
        ('classification', clf)
    ])

    score={'AUC':'roc_auc', 
           'RECALL':'recall',
           'PRECISION':'precision',
           'F1':'f1'}

    gcv = GridSearchCV(estimator=model, param_grid=params, cv=5, scoring=score, n_jobs=12, refit='F1',
                       return_train_score=True)
    gcv.fit(X, y)

    return gcv

for param, classifier in zip(params, classifiers):
    print("Working on {}...".format(classifier[0]))
    clf = imb_pipeline(classifier[1], X_scaled, y, param) 
    print("Best parameter for {} is {}".format(classifier[0], clf.best_params_))
    print("Best `F1` for {} is {}".format(classifier[0], clf.best_score_))
    print('-'*50)
    print('\n')
Run Code Online (Sandbox Code Playgroud)

参数:

[{'penalty': ('l1', 'l2'), 'C': (0.01, …
Run Code Online (Sandbox Code Playgroud)

pipeline python-3.x scikit-learn imblearn

7
推荐指数
1
解决办法
5362
查看次数

sklearn中带有数据标签的定制变压器Mixin

我正在做一个小项目,试图在我的数据不平衡的情况下应用SMOTE“综合少数族裔过采样技术”。

我为SMOTE功能创建了一个定制的TransformerMixin ..

class smote(BaseEstimator, TransformerMixin):
    def fit(self, X, y=None):
        print(X.shape, ' ', type(X)) # (57, 28)   <class 'numpy.ndarray'>
        print(len(y), ' ', type)     #    57      <class 'list'>
        smote = SMOTE(kind='regular', n_jobs=-1)
        X, y = smote.fit_sample(X, y)

        return X

    def transform(self, X):
        return X
Run Code Online (Sandbox Code Playgroud)
model = Pipeline([
        ('posFeat1', featureVECTOR()),
        ('sca1', StandardScaler()),
        ('smote', smote()),
        ('classification', SGDClassifier(loss='hinge', max_iter=1, random_state = 38, tol = None))
    ])
    model.fit(train_df, train_df['label'].values.tolist())
    predicted = model.predict(test_df)
Run Code Online (Sandbox Code Playgroud)

我在FIT函数上实现了SMOTE,因为我不希望将其应用于测试数据。

不幸的是,我得到了这个错误:

     model.fit(train_df, train_df['label'].values.tolist())
  File "C:\Python35\lib\site-packages\sklearn\pipeline.py", line 248, in fit
    Xt, fit_params = …
Run Code Online (Sandbox Code Playgroud)

python pipeline scikit-learn

3
推荐指数
1
解决办法
1168
查看次数