Пример #1
0
    def test_single_matrix_hard_voting(self):
        """
        Determine if KNN->ChannelClassifier(hard voting) in a pipecaster
        pipeline gives identical predictions to sklearn KNN on training data.
        """
        X, y = make_classification(n_samples=100,
                                   n_features=20,
                                   n_informative=10,
                                   class_sep=5,
                                   random_state=42)

        # control
        clf = KNeighborsClassifier(n_neighbors=5, weights='uniform')
        clf.fit(X, y)
        clf_predictions = clf.predict(X)

        # implementation 1
        mclf = MultichannelPipeline(n_channels=1)
        base_clf = KNeighborsClassifier(n_neighbors=5, weights='uniform')
        base_clf = transform_wrappers.SingleChannel(base_clf,
                                                    transform_method='predict')
        mclf.add_layer(base_clf)
        mclf.add_layer(MultichannelPredictor(HardVotingMetaClassifier()))
        mclf.fit([X], y)
        mclf_predictions = mclf.predict([X])
        self.assertTrue(
            np.array_equal(clf_predictions, mclf_predictions),
            'hard voting metaclassifier did not reproduce sklearn '
            'result on single matrix prediction task')

        # implementation 2
        mclf = MultichannelPipeline(n_channels=1)
        base_clf = KNeighborsClassifier(n_neighbors=5, weights='uniform')
        mclf.add_layer(
            ChannelEnsemble(base_clf,
                            HardVotingMetaClassifier(),
                            base_transform_methods='predict'))
        mclf.fit([X], y)
        mclf_predictions = mclf.predict([X])
        self.assertTrue(
            np.array_equal(clf_predictions, mclf_predictions),
            'hard voting metaclassifier did not reproduce sklearn '
            'result on single matrix prediction task')
Пример #2
0
    def test_single_matrix_mean_voting(self, seed=42):
        """
        Determine if KNN->ChannelRegressor(mean voting) in a pipecaster
        pipeline gives identical predictions to sklearn KNN on training data
        """
        X, y = make_regression(n_samples=100,
                               n_features=20,
                               n_informative=10,
                               random_state=seed)

        # control
        rgr = KNeighborsRegressor(n_neighbors=5, weights='uniform')
        rgr.fit(X, y)
        rgr_predictions = rgr.predict(X)

        # implementation 1
        mrgr = MultichannelPipeline(n_channels=1)
        rgr = transform_wrappers.SingleChannel(
            KNeighborsRegressor(n_neighbors=5, weights='uniform'))
        mrgr.add_layer(rgr, pipe_processes=n_cpus)
        mrgr.add_layer(MultichannelPredictor(AggregatingMetaRegressor(
            np.mean)))
        mrgr.fit([X], y)
        mrgr_predictions = mrgr.predict([X])
        self.assertTrue(
            np.array_equal(rgr_predictions, mrgr_predictions),
            'mean voting ChannelRegressor failed to reproduce '
            'sklearn result on single matrix prediction task')

        # implementation 2
        mrgr = MultichannelPipeline(n_channels=1)
        base_rgr = KNeighborsRegressor(n_neighbors=5, weights='uniform')
        mrgr.add_layer(
            ChannelEnsemble(base_rgr,
                            AggregatingMetaRegressor(np.mean),
                            base_processes='max'))
        mrgr.fit([X], y)
        mrgr_predictions = mrgr.predict([X])
        self.assertTrue(
            np.array_equal(rgr_predictions, mrgr_predictions),
            'mean voting ChannelRegressor failed to reproduce '
            'sklearn result on single matrix prediction task')