parameters: (n_components, n_features, density='auto',
                           random_state=None)
docstring: Generalized Achlioptas random sparse matrix for random projection
  
      Setting density to 1 / 3 will yield the original matrix by Dimitris
      Achlioptas while setting a lower value will yield the generalization
      by Ping Li et al.
  
      If we note :math:`s = 1 / density`, the components of the random matrix are
      drawn from:
  
        - -sqrt(s) / sqrt(n_components)   with probability 1 / 2s
        -  0                              with probability 1 - 1 / s
        - +sqrt(s) / sqrt(n_components)   with probability 1 / 2s
  
      Read more in the :ref:`User Guide <sparse_random_matrix>`.
  
      Parameters
      ----------
      n_components : int,
          Dimensionality of the target projection space.
  
      n_features : int,
          Dimensionality of the original source space.
  
      density : float in range ]0, 1] or 'auto', optional (default='auto')
          Ratio of non-zero component in the random projection matrix.
  
          If density = 'auto', the value is set to the minimum density
          as recommended by Ping Li et al.: 1 / sqrt(n_features).
  
          Use density = 1 / 3.0 if you want to reproduce the results from
          Achlioptas, 2001.
  
      random_state : integer, RandomState instance or None (default=None)
          Control the pseudo random number generator used to generate the
          matrix at fit time.
  
      Returns
      -------
      components: numpy array or CSR matrix with shape [n_components, n_features]
          The generated Gaussian random matrix.
  
      See Also
      --------
      SparseRandomProjection
      gaussian_random_matrix
  
      References
      ----------
  
      .. [1] Ping Li, T. Hastie and K. W. Church, 2006,
             "Very Sparse Random Projections".
             http://web.stanford.edu/~hastie/Papers/Ping/KDD06_rp.pdf
  
      .. [2] D. Achlioptas, 2001, "Database-friendly random projections",
             http://www.cs.ucsc.edu/~optas/papers/jl.pdf
docstring_summary: Generalized Achlioptas random sparse matrix for random projection
docstring_tokens: Generalized Achlioptas random sparse matrix for random projection
function: def sparse_random_matrix(n_components, n_features, density='auto',
                           random_state=None):
      """Generalized Achlioptas random sparse matrix for random projection
  
      Setting density to 1 / 3 will yield the original matrix by Dimitris
      Achlioptas while setting a lower value will yield the generalization
      by Ping Li et al.
  
      If we note :math:`s = 1 / density`, the components of the random matrix are
      drawn from:
  
        - -sqrt(s) / sqrt(n_components)   with probability 1 / 2s
        -  0                              with probability 1 - 1 / s
        - +sqrt(s) / sqrt(n_components)   with probability 1 / 2s
  
      Read more in the :ref:`User Guide <sparse_random_matrix>`.
  
      Parameters
      ----------
      n_components : int,
          Dimensionality of the target projection space.
  
      n_features : int,
          Dimensionality of the original source space.
  
      density : float in range ]0, 1] or 'auto', optional (default='auto')
          Ratio of non-zero component in the random projection matrix.
  
          If density = 'auto', the value is set to the minimum density
          as recommended by Ping Li et al.: 1 / sqrt(n_features).
  
          Use density = 1 / 3.0 if you want to reproduce the results from
          Achlioptas, 2001.
  
      random_state : integer, RandomState instance or None (default=None)
          Control the pseudo random number generator used to generate the
          matrix at fit time.
  
      Returns
      -------
      components: numpy array or CSR matrix with shape [n_components, n_features]
          The generated Gaussian random matrix.
  
      See Also
      --------
      SparseRandomProjection
      gaussian_random_matrix
  
      References
      ----------
  
      .. [1] Ping Li, T. Hastie and K. W. Church, 2006,
             "Very Sparse Random Projections".
             http://web.stanford.edu/~hastie/Papers/Ping/KDD06_rp.pdf
  
      .. [2] D. Achlioptas, 2001, "Database-friendly random projections",
             http://www.cs.ucsc.edu/~optas/papers/jl.pdf
  
      """
      _check_input_size(n_components, n_features)
      density = _check_density(density, n_features)
      rng = check_random_state(random_state)
  
      if density == 1:
          # skip index generation if totally dense
          components = rng.binomial(1, 0.5, (n_components, n_features)) * 2 - 1
          return 1 / np.sqrt(n_components) * components
  
      else:
          # Generate location of non zero elements
          indices = []
          offset = 0
          indptr = [offset]
          for i in xrange(n_components):
              # find the indices of the non-zero components for row i
              n_nonzero_i = rng.binomial(n_features, density)
              indices_i = sample_without_replacement(n_features, n_nonzero_i,
                                                     random_state=rng)
              indices.append(indices_i)
              offset += n_nonzero_i
              indptr.append(offset)
  
          indices = np.concatenate(indices)
  
          # Among non zero components the probability of the sign is 50%/50%
          data = rng.binomial(1, 0.5, size=np.size(indices)) * 2 - 1
  
          # build the CSR structure by concatenating the rows
          components = sp.csr_matrix((data, indices, indptr),
                                     shape=(n_components, n_features))
  
          return np.sqrt(1 / density) / np.sqrt(n_components) * components
function_tokens: def sparse_random_matrix ( n_components , n_features , density = 'auto' , random_state = None ) : _check_input_size ( n_components , n_features ) density = _check_density ( density , n_features ) rng = check_random_state ( random_state ) if density == 1 : # skip index generation if totally dense components = rng . binomial ( 1 , 0.5 , ( n_components , n_features ) ) * 2 - 1 return 1 / np . sqrt ( n_components ) * components else : # Generate location of non zero elements indices = [ ] offset = 0 indptr = [ offset ] for i in xrange ( n_components ) : # find the indices of the non-zero components for row i n_nonzero_i = rng . binomial ( n_features , density ) indices_i = sample_without_replacement ( n_features , n_nonzero_i , random_state = rng ) indices . append ( indices_i ) offset += n_nonzero_i indptr . append ( offset ) indices = np . concatenate ( indices ) # Among non zero components the probability of the sign is 50%/50% data = rng . binomial ( 1 , 0.5 , size = np . size ( indices ) ) * 2 - 1 # build the CSR structure by concatenating the rows components = sp . csr_matrix ( ( data , indices , indptr ) , shape = ( n_components , n_features ) ) return np . sqrt ( 1 / density ) / np . sqrt ( n_components ) * components