Exemplo n.º 1
0
    def test_score_scalar_response(self):

        neigh = KNeighborsRegressor()

        neigh.fit(self.X, self.modes_location)
        r = neigh.score(self.X, self.modes_location)
        np.testing.assert_almost_equal(r, 0.9975889963743335)
Exemplo n.º 2
0
    def test_score_functional_response(self):

        neigh = KNeighborsRegressor()

        y = 5 * self.X + 1
        neigh.fit(self.X, y)
        r = neigh.score(self.X, y)
        np.testing.assert_almost_equal(r, 0.962651178452408)

        # Weighted case and basis form
        y = y.to_basis(Fourier(domain_range=y.domain_range[0], n_basis=5))
        neigh.fit(self.X, y)

        r = neigh.score(self.X[:7], y[:7],
                        sample_weight=4 * [1. / 5] + 3 * [1. / 15])
        np.testing.assert_almost_equal(r, 0.9982527586114364)
Exemplo n.º 3
0
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}
#    {\sum_{i=1}^{n} \int (y_i(t)- \frac{1}{n}\sum_{i=1}^{n}y_i(t))^2dt}
#
# where :math:`y_i` are the real responses and :math:`\hat{y}_i` the
# predicted ones.

score = knn.score(X_test, y_test)
print(score)

##########################################################################
#
# More detailed information about the canadian weather dataset can be obtained
# in the following references.
#
#  * Ramsay, James O., and Silverman, Bernard W. (2006). Functional Data
#    Analysis, 2nd ed. , Springer, New York.
#
#  * Ramsay, James O., and Silverman, Bernard W. (2002). Applied Functional
#    Data Analysis, Springer, New York\n'
#