Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 27 additions & 32 deletions sklearn/neighbors/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,20 +444,19 @@ def _fit(self, X, y=None):
self.n_samples_fit_ = X.data.shape[0]
return self

if self.effective_metric_ == 'precomputed':
if self.metric == 'precomputed':
X = _check_precomputed(X)
# Precomputed matrix X must be squared
if X.shape[0] != X.shape[1]:
raise ValueError("Precomputed matrix must be square."
" Input is a {}x{} matrix."
.format(X.shape[0], X.shape[1]))
self.n_features_in_ = X.shape[1]

n_samples = X.shape[0]
if n_samples == 0:
raise ValueError("n_samples must be greater than 0")

# Precomputed matrix X must be squared
if self.metric == 'precomputed' and X.shape[0] != X.shape[1]:
raise ValueError("Precomputed matrix must be a square matrix."
" Input is a {}x{} matrix."
.format(X.shape[0], X.shape[1]))

if issparse(X):
if self.algorithm not in ('auto', 'brute'):
warnings.warn("cannot use tree with sparse input: "
Expand Down Expand Up @@ -514,14 +513,12 @@ def _fit(self, X, y=None):
if self.n_neighbors <= 0:
raise ValueError(
"Expected n_neighbors > 0. Got %d" %
self.n_neighbors
)
else:
if not isinstance(self.n_neighbors, numbers.Integral):
raise TypeError(
"n_neighbors does not take %s value, "
"enter integer value" %
type(self.n_neighbors))
self.n_neighbors)
elif not isinstance(self.n_neighbors, numbers.Integral):
raise TypeError(
"n_neighbors does not take %s value, "
"enter integer value" %
type(self.n_neighbors))

return self

Expand Down Expand Up @@ -654,18 +651,16 @@ class from an array representing our data set and ask who's
elif n_neighbors <= 0:
raise ValueError(
"Expected n_neighbors > 0. Got %d" %
n_neighbors
)
else:
if not isinstance(n_neighbors, numbers.Integral):
raise TypeError(
"n_neighbors does not take %s value, "
"enter integer value" %
type(n_neighbors))
n_neighbors)
elif not isinstance(n_neighbors, numbers.Integral):
raise TypeError(
"n_neighbors does not take %s value, "
"enter integer value" %
type(n_neighbors))

if X is not None:
query_is_train = False
if self.effective_metric_ == 'precomputed':
if self.metric == 'precomputed':
X = _check_precomputed(X)
else:
X = self._validate_data(X, accept_sparse='csr', reset=False)
Expand All @@ -687,7 +682,7 @@ class from an array representing our data set and ask who's
n_jobs = effective_n_jobs(self.n_jobs)
chunked_results = None
if (self._fit_method == 'brute' and
self.effective_metric_ == 'precomputed' and issparse(X)):
self.metric == 'precomputed' and issparse(X)):
results = _kneighbors_from_graph(
X, n_neighbors=n_neighbors,
return_distance=return_distance)
Expand Down Expand Up @@ -793,8 +788,8 @@ def kneighbors_graph(self, X=None, n_neighbors=None,
Returns
-------
A : sparse-matrix of shape (n_queries, n_samples_fit)
`n_samples_fit` is the number of samples in the fitted data
`A[i, j]` is assigned the weight of edge that connects `i` to `j`.
`n_samples_fit` is the number of samples in the fitted data.
`A[i, j]` gives the weight of the edge connecting `i` to `j`.
The matrix is of CSR format.

Examples
Expand Down Expand Up @@ -980,7 +975,7 @@ class from an array representing our data set and ask who's

if X is not None:
query_is_train = False
if self.effective_metric_ == 'precomputed':
if self.metric == 'precomputed':
X = _check_precomputed(X)
else:
X = self._validate_data(X, accept_sparse='csr', reset=False)
Expand All @@ -992,7 +987,7 @@ class from an array representing our data set and ask who's
radius = self.radius

if (self._fit_method == 'brute' and
self.effective_metric_ == 'precomputed' and issparse(X)):
self.metric == 'precomputed' and issparse(X)):
results = _radius_neighbors_from_graph(
X, radius=radius, return_distance=return_distance)

Expand Down Expand Up @@ -1116,9 +1111,9 @@ def radius_neighbors_graph(self, X=None, radius=None, mode='connectivity',
Returns
-------
A : sparse-matrix of shape (n_queries, n_samples_fit)
`n_samples_fit` is the number of samples in the fitted data
`A[i, j]` is assigned the weight of edge that connects `i` to `j`.
The matrix if of format CSR.
`n_samples_fit` is the number of samples in the fitted data.
`A[i, j]` gives the weight of the edge connecting `i` to `j`.
The matrix is of CSR format.

Examples
--------
Expand Down