Debian Codegraph @

docs/features/sparse.rst

raw
1 .. _features.sparse:
2
3 Sparse Matrices
4 ===============
5
6 apricot has built-in support for sparse matrices. This support makes it possible to summarize massive data sets whose dense representations are not feasible to work with, e.g. sparse similarity graphs when using graph-based functions or word counts for text data and feature-based functions. Using a sparse matrix will always (except when ties exist) give the same result that using a dense matrix populated with 0s would, but operating on sparse matrices can be significantly faster process.
7
8 Here is an example of using a feature-based function on a very sparse feature matrix. Note that each row can be thought of as an example with binary feature values.
9
10 .. code-block:: python
11
12 from apricot import FeatureBasedSelection
13 from scipy.sparse import csr_matrix
14
15 X = numpy.random.randint(2, size=(10000, 100), p=[0.99, 0.01])
16 X_sparse = csr_matrix(X)
17
18 selector = FeatureBasedSelection(100, 'sqrt')
19 selector.fit(X_sparse)
20
21 Here is an example of using a graph-based function on a very sparse similarity matrix. Note that X is a pre-computed similarity matrix and must be specified as such to the function.
22
23 .. code-block:: python
24
25 from apricot import FacilityLocationSelection
26 from scipy.sparse import csr_matrix
27
28 X = numpy.random.randint(10, size=(1000, 1000), p=[0.99, 0.01])
29 X = numpy.abs((X + X.T) / 2)
30 X_sparse = csr_matrix(X)
31
32 selector = FacilityLocationSelection(100, 'precomputed')
33 selector.fit(X_sparse)