class_docstr: Sparse random projector.
  
  This transformer reduces the dimensionality of inputs by projecting them onto a sparse random
  projection matrix.
  
  Ping Li et al. recommend using a minimum density of `1 / sqrt(n_features)`. The transformer
  is not aware of how many features will be seen, so the user must specify the density manually.
  
  Parameters
  ----------
  n_components
      Number of components to project the data onto.
  density
      Density of the random projection matrix. The density is defined as the ratio of non-zero
      components in the matrix. It is equal to `1 - sparsity`.
  seed
      Random seed for reproducibility.
  
  Examples
  --------
  
  >>> from river import datasets
  >>> from river import evaluate
  >>> from river import linear_model
  >>> from river import metrics
  >>> from river import preprocessing
  
  >>> dataset = datasets.TrumpApproval()
  >>> model = preprocessing.SparseRandomProjector(
  ...     n_components=3,
  ...     seed=42
  ... )
  
  >>> for x, y in dataset:
  ...     x = model.transform_one(x)
  ...     print(x)
  ...     break
  {0: 92.89572746525327, 1: 1344540.5692342375, 2: 0}
  
  >>> model = (
  ...     preprocessing.SparseRandomProjector(
  ...         n_components=5,
  ...         seed=42
  ...     ) |
  ...     preprocessing.StandardScaler() |
  ...     linear_model.LinearRegression()
  ... )
  >>> evaluate.progressive_val_score(dataset, model, metrics.MAE())
  MAE: 1.292572
  
  References
  ----------
  [^1]: D. Achlioptas. 2003. Database-friendly random projections: Johnson-Lindenstrauss with binary coins. Journal of Computer and System Sciences 66 (2003) 671-687
  [^2]: Ping Li, Trevor J. Hastie, and Kenneth W. Church. 2006. Very sparse random projections. In Proceedings of the 12th ACM SIGKDD international conference on Knowledge discovery and data mining (KDD'06). ACM, New York, NY, USA, 287-296.
class_docstr_tok: ["Sparse","random","projector",".","This","transformer","reduces","the","dimensionality","of","inputs","by","projecting","them","onto","a","sparse","random","projection","matrix",".","Ping","Li","et","al",".","recommend","using","a","minimum","density","of","`","1","\/","sqrt","(","n_features",")","`",".","The","transformer","is","not","aware","of","how","many","features","will","be","seen",",","so","the","user","must","specify","the","density","manually",".","Parameters","--","--","--","--","--","n_components","Number","of","components","to","project","the","data","onto",".","density","Density","of","the","random","projection","matrix",".","The","density","is","defined","as","the","ratio","of","non-zero","components","in","the","matrix",".","It","is","equal","to","`","1","-","sparsity","`",".","seed","Random","seed","for","reproducibility",".","Examples","--","--","--","--",">",">",">","from","river","import","datasets",">",">",">","from","river","import","evaluate",">",">",">","from","river","import","linear_model",">",">",">","from","river","import","metrics",">",">",">","from","river","import","preprocessing",">",">",">","dataset","=","datasets.TrumpApproval","(",")",">",">",">","model","=","preprocessing.SparseRandomProjector","(","...","n_components=3",",","...","seed=42","...",")",">",">",">","for","x",",","y","in","dataset",":","...","x","=","model.transform_one","(","x",")","...","print","(","x",")","...","break","{","0",":","92.89572746525327",",","1",":","1344540.5692342375",",","2",":","0","}",">",">",">","model","=","(","...","preprocessing.SparseRandomProjector","(","...","n_components=5",",","...","seed=42","...",")","|","...","preprocessing.StandardScaler","(",")","|","...","linear_model.LinearRegression","(",")","...",")",">",">",">","evaluate.progressive_val_score","(","dataset",",","model",",","metrics.MAE","(",")",")","MAE",":","1.292572","References","--","--","--","--","--","[","^1","]",":","D.","Achlioptas",".","2003",".","Database-friendly","random","projections",":","Johnson-Lindenstrauss","with","binary","coins",".","Journal","of","Computer","and","System","Sciences","66","(","2003",")","671-687","[","^2","]",":","Ping","Li",",","Trevor","J.","Hastie",",","and","Kenneth","W.","Church",".","2006",".","Very","sparse","random","projections",".","In","Proceedings","of","the","12th","ACM","SIGKDD","international","conference","on","Knowledge","discovery","and","data","mining","(","KDD'06",")",".","ACM",",","New","York",",","NY",",","USA",",","287-296","."]
code: def _rand_weights_for_feature(self):
      weights = {}
      for j in range(self.n_components):
          if self._rng.random() < self.density:
              w = (1 / (self.density * self.n_components)) ** 0.5
              # Flip a coin to decide the sign
              weights[j] = w if self._rng.random() < 0.5 else -w
      return weights
  
file_path: river/river/preprocessing/random_projection.py
imports: from __future__ import annotations
  import collections
  import random
  from river import base
label_desc: Use image node_id 2 for calling the SparseRandomProjector obj's underlying member method code with example usage: obj._rand_weights_for_feature() and returns: weights