Example #1
0
    def test_radius_outlier_functional_response(self):
        knnr = RadiusNeighborsRegressor(radius=0.001)
        knnr.fit(self.X[3:6], self.X[3:6])

        # No value given
        with np.testing.assert_raises(ValueError):
            knnr.predict(self.X[:10])

        # Test response
        knnr = RadiusNeighborsRegressor(radius=0.001,
                                        outlier_response=self.X[0])
        knnr.fit(self.X[:6], self.X[:6])

        res = knnr.predict(self.X[:7])
        np.testing.assert_array_almost_equal(self.X[0].data_matrix,
                                             res[6].data_matrix)
Example #2
0
    def test_radius_functional_response(self):
        knnr = RadiusNeighborsRegressor(metric=lp_distance,
                                        weights='distance',
                                        regressor=l2_mean)

        knnr.fit(self.X, self.X)

        res = knnr.predict(self.X)
        np.testing.assert_array_almost_equal(res.data_matrix,
                                             self.X.data_matrix)
Example #3
0
    def test_predict_regressor(self):
        """Test scalar regression, predics mode location"""

        # Dummy test, with weight = distance, only the sample with distance 0
        # will be returned, obtaining the exact location
        knnr = KNeighborsRegressor(weights='distance')
        rnnr = RadiusNeighborsRegressor(weights='distance', radius=.1)

        knnr.fit(self.X, self.modes_location)
        rnnr.fit(self.X, self.modes_location)

        np.testing.assert_array_almost_equal(knnr.predict(self.X),
                                             self.modes_location)
        np.testing.assert_array_almost_equal(rnnr.predict(self.X),
                                             self.modes_location)