giotto.graphs
.KNeighborsGraph¶
-
class
giotto.graphs.
KNeighborsGraph
(n_neighbors=4, metric='euclidean', p=2, metric_params={}, n_jobs=None)¶ Adjacency matrices of k-nearest neighbor graphs.
Given a two-dimensional array of row vectors seen as points in high-dimensional space, the corresponding kNN graph is a simple, undirected and unweighted graph with a vertex for every vector in the array, and an edge between two vertices whenever either the first corresponding vector is among the k nearest neighbors of the second, or vice-versa.
sklearn.neighbors.kneighbors_graph
is used to compute the adjacency matrices of kNN graphs.- Parameters
- n_neighborsint, optional, default:
4
Number of neighbors to use.
- metricstring or callable, default
'minkowski'
Metric to use for distance computation. Any metric from scikit-learn or
scipy.spatial.distance
can be used. If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string. Distance matrices are not supported. Valid values for metric are:from scikit-learn: [
'cityblock'
,'cosine'
,'euclidean'
,'l1'
,'l2'
,'manhattan'
]from
scipy.spatial.distance
: ['braycurtis'
,'canberra'
,'chebyshev'
,'correlation'
,'dice'
,'hamming'
,'jaccard'
,'kulsinski'
,'mahalanobis'
,'minkowski'
,'rogerstanimoto'
,'russellrao'
,'seuclidean'
,'sokalmichener'
,'sokalsneath'
,'sqeuclidean'
,'yule'
]
See the documentation for
scipy.spatial.distance
for details on these metrics.- metric_paramsdict, optional, default:
{}
Additional keyword arguments for the metric function.
- pint, optional, default:
2
Parameter for the Minkowski (i.e. \(\ell^p\)) metric from
sklearn.metrics.pairwise.pairwise_distances
. p = 1 is the Manhattan distance and p = 2 is the Euclidean distance.- metric_paramsdict, optional, default:
{}
Additional keyword arguments for the metric function.
- n_jobsint or None, optional, default:
None
The number of jobs to use for the computation.
None
means 1 unless in ajoblib.parallel_backend
context.-1
means using all processors.
- n_neighborsint, optional, default:
Examples
>>> import numpy as np >>> from giotto.graphs import KNeighborsGraph >>> X = np.array([[[0, 1, 3, 0, 0], ... [1, 0, 5, 0, 0], ... [3, 5, 0, 4, 0], ... [0, 0, 4, 0, 0]]]) >>> kng = KNeighborsGraph(n_neighbors=2) >>> Xg = kng.fit_transform(X) >>> print(Xg[0].toarray()) [[0. 1. 1. 1.] [1. 0. 0. 1.] [1. 0. 0. 1.] [1. 1. 1. 0.]]
Methods
fit
(self, X[, y])Do nothing and return the estimator unchanged.
fit_transform
(self, X[, y])Fit to data, then transform it.
get_params
(self[, deep])Get parameters for this estimator.
set_params
(self, \*\*params)Set the parameters of this estimator.
transform
(self, X[, y])Compute kNN graphs and return their adjacency matrices as sparse matrices.
-
__init__
(self, n_neighbors=4, metric='euclidean', p=2, metric_params={}, n_jobs=None)¶ Initialize self. See help(type(self)) for accurate signature.
-
fit
(self, X, y=None)¶ Do nothing and return the estimator unchanged.
This method is there to implement the usual scikit-learn API and hence work in pipelines.
- Parameters
- Xndarray, shape (n_samples, n_points, n_dimensions)
Input data. Each entry in X along axis 0 is an array of
n_points
row vectors inn_dimensions
-dimensional space.- yNone
There is no need for a target in a transformer, yet the pipeline API requires this parameter.
- Returns
- selfobject
-
fit_transform
(self, X, y=None, **fit_params)¶ Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters
- Xnumpy array of shape [n_samples, n_features]
Training set.
- ynumpy array of shape [n_samples]
Target values.
- Returns
- X_newnumpy array of shape [n_samples, n_features_new]
Transformed array.
-
get_params
(self, deep=True)¶ Get parameters for this estimator.
- Parameters
- deepboolean, optional
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
- paramsmapping of string to any
Parameter names mapped to their values.
-
set_params
(self, **params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.- Returns
- self
-
transform
(self, X, y=None)¶ Compute kNN graphs and return their adjacency matrices as sparse matrices.
- Parameters
- Xndarray, shape (n_samples, n_points, n_dimensions)
Input data. Each entry in X along axis 0 is an array of
n_points
row vectors inn_dimensions
-dimensional space.- yNone
There is no need for a target in a transformer, yet the pipeline API requires this parameter.
- Returns
- Xtndarray of sparse matrices in CSR format, shape (n_samples,)
Adjacency matrices of kNN graphs.