def test_kneighbors(self): """Test k neighbor searches for all k-neighbors estimators""" nn = NearestNeighbors() nn.fit(self.X) lof = LocalOutlierFactor(n_neighbors=5) lof.fit(self.X) knn = KNeighborsClassifier() knn.fit(self.X, self.y) knnr = KNeighborsRegressor() knnr.fit(self.X, self.modes_location) for neigh in [nn, knn, knnr, lof]: dist, links = neigh.kneighbors(self.X[:4]) np.testing.assert_array_equal(links, [[0, 7, 21, 23, 15], [1, 12, 19, 18, 17], [2, 17, 22, 27, 26], [3, 4, 9, 5, 25]]) graph = neigh.kneighbors_graph(self.X[:4]) dist_kneigh = lp_distance(self.X[0], self.X[7]) np.testing.assert_array_almost_equal(dist[0, 1], dist_kneigh) for i in range(30): self.assertEqual(graph[0, i] == 1.0, i in links[0]) self.assertEqual(graph[0, i] == 0.0, i not in links[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)
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)
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)
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)
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)
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)
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)
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)
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)
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.1, random_state=28) ############################################################################## # # We will try make a prediction using 5 neighbors and the :math:`\mathbb{L}^2` # distance. In this case, to calculate # 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
random_state=7) ############################################################################## # # Firstly we will try make a prediction with the default values of the # estimator, using 5 neighbors and the :math:`\mathbb{L}^2` distance. # # We can fit the :class:`~skfda.ml.regression.KNeighborsRegressor` in the # same way than the # sklearn estimators. This estimator is an extension of the sklearn # :class:`~sklearn.neighbors.KNeighborsRegressor`, but accepting a # :class:`~skfda.representation.grid.FDataGrid` as input instead of an array with # multivariate data. # knn = KNeighborsRegressor(weights='distance') knn.fit(X_train, y_train) ############################################################################## # # We can predict values for the test partition using # :meth:`~skfda.ml.regression.KNeighborsScalarRegressor.predict`. # pred = knn.predict(X_test) print(pred) ############################################################################## # # The following figure compares the real precipitations with the predicted # values.