示例#1
0
    def test_knn_functional_response(self):
        knnr = KNeighborsRegressor(n_neighbors=1)

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

        res = knnr.predict(self.X)
        np.testing.assert_array_almost_equal(res.data_matrix,
                                             self.X.data_matrix)
示例#2
0
    def test_functional_response_basis(self):
        knnr = KNeighborsRegressor(weights='distance', n_neighbors=5)
        response = self.X.to_basis(Fourier(domain_range=(-1, 1), n_basis=10))
        knnr.fit(self.X, response)

        res = knnr.predict(self.X)
        np.testing.assert_array_almost_equal(res.coefficients,
                                             response.coefficients)
示例#3
0
    def test_knn_functional_response_sklearn(self):
        # Check sklearn metric
        knnr = KNeighborsRegressor(n_neighbors=1, metric='euclidean',
                                   multivariate_metric=True)
        knnr.fit(self.X, self.X)

        res = knnr.predict(self.X)
        np.testing.assert_array_almost_equal(res.data_matrix,
                                             self.X.data_matrix)
示例#4
0
    def test_knn_functional_response_precomputed(self):
        knnr = KNeighborsRegressor(n_neighbors=4, weights='distance',
                                   metric='precomputed')
        d = pairwise_distance(lp_distance)
        distances = d(self.X[:4], self.X[:4])

        knnr.fit(distances, self.X[:4])

        res = knnr.predict(distances)
        np.testing.assert_array_almost_equal(res.data_matrix,
                                             self.X[:4].data_matrix)
示例#5
0
    def test_functional_response_custom_weights(self):

        def weights(weights):

            return np.array([w == 0 for w in weights], dtype=float)

        knnr = KNeighborsRegressor(weights=weights, n_neighbors=5)
        response = self.X.to_basis(Fourier(domain_range=(-1, 1), n_basis=10))
        knnr.fit(self.X, response)

        res = knnr.predict(self.X)
        np.testing.assert_array_almost_equal(res.coefficients,
                                             response.coefficients)
示例#6
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)
示例#7
0
    def test_functional_regression_distance_weights(self):

        knnr = KNeighborsRegressor(
            weights='distance', n_neighbors=10)
        knnr.fit(self.X[:10], self.X[:10])
        res = knnr.predict(self.X[11])

        d = pairwise_distance(lp_distance)
        distances = d(self.X[:10], self.X[11]).flatten()

        weights = 1 / distances
        weights /= weights.sum()

        response = self.X[:10].mean(weights=weights)
        np.testing.assert_array_almost_equal(res.data_matrix,
                                             response.data_matrix)
示例#8
0
# the response we will use a mean of the response, weighted by their distance
# to the test sample.
#

knn = KNeighborsRegressor(n_neighbors=5, weights='distance')
knn.fit(X_train, y_train)

##############################################################################
#
# We can predict values for the test partition using
# :meth:`~skfda.ml.regression.KNeighborsFunctionalRegressor.predict`. The
# following figure shows the real precipitation curves, in dashed line, and
# the predicted ones.
#

y_pred = knn.predict(X_test)

# Plot prediction
fig = y_pred.plot()
fig.axes[0].set_prop_cycle(None)  # Reset colors
y_test.plot(fig=fig, linestyle='--')

##############################################################################
#
# We can quantify how much variability it is explained by the model
# using the
# :meth:`~skfda.ml.regression.KNeighborsFunctionalRegressor.score` method,
# which computes the value
#
# .. math::
#    1 - \frac{\sum_{i=1}^{n}\int (y_i(t) - \hat{y}_i(t))^2dt}