Python sklearn.random_projection.GaussianRandomProjection() Examples

The following are 7 code examples of sklearn.random_projection.GaussianRandomProjection(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.random_projection , or try the search function .
Example #1
Source File: projections.py    From bonnetal with MIT License 6 votes vote down vote up
def random_projection(mat, dim=3):
  """
   Projects a matrix of dimensions m x oldim to m x dim using a random
   projection
  """
  # include has to be here for multiprocessing problems
  from sklearn import random_projection as rp

  if mat.is_cuda:
    device = torch.device("cuda")
  else:
    device = torch.device("cpu")

  # project
  m, oldim = mat.shape
  t = rp.GaussianRandomProjection()
  proj_mat = torch.tensor(t._make_random_matrix(dim, oldim))
  proj_mat = proj_mat.to(device)
  output = torch.matmul(mat.double(), proj_mat.t())

  # check new dim
  assert(output.shape[0] == m)
  assert(output.shape[1] == dim)

  return output 
Example #2
Source File: datasets.py    From ann-benchmarks with MIT License 6 votes vote down vote up
def transform_bag_of_words(filename, n_dimensions, out_fn):
    import gzip
    from scipy.sparse import lil_matrix
    from sklearn.feature_extraction.text import TfidfTransformer
    from sklearn import random_projection
    with gzip.open(filename, 'rb') as f:
        file_content = f.readlines()
        entries = int(file_content[0])
        words = int(file_content[1])
        file_content = file_content[3:]  # strip first three entries
        print("building matrix...")
        A = lil_matrix((entries, words))
        for e in file_content:
            doc, word, cnt = [int(v) for v in e.strip().split()]
            A[doc - 1, word - 1] = cnt
        print("normalizing matrix entries with tfidf...")
        B = TfidfTransformer().fit_transform(A)
        print("reducing dimensionality...")
        C = random_projection.GaussianRandomProjection(
            n_components=n_dimensions).fit_transform(B)
        X_train, X_test = train_test_split(C)
        write_output(numpy.array(X_train), numpy.array(
            X_test), out_fn, 'angular') 
Example #3
Source File: feature_extraction.py    From open-solution-value-prediction with MIT License 5 votes vote down vote up
def __init__(self, **kwargs):
        super().__init__()
        self.estimator = sk_rp.GaussianRandomProjection(**kwargs) 
Example #4
Source File: gaussian_random_projection.py    From lale with Apache License 2.0 5 votes vote down vote up
def __init__(self, n_components='auto', eps=0.1, random_state=None):
        self._hyperparams = {
            'n_components': n_components,
            'eps': eps,
            'random_state': random_state}
        self._wrapped_model = Op(**self._hyperparams) 
Example #5
Source File: test_sklearn_random_projection.py    From sklearn-onnx with MIT License 5 votes vote down vote up
def test_gaussian_random_projection_float32(self):
        rng = np.random.RandomState(42)
        pt = GaussianRandomProjection(n_components=4)
        X = rng.rand(10, 5)
        model = pt.fit(X)
        assert model.transform(X).shape[1] == 4
        model_onnx = convert_sklearn(
            model, "scikit-learn GaussianRandomProjection",
            [("inputs", FloatTensorType([None, X.shape[1]]))],
            target_opset=TARGET_OPSET)
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(X.astype(np.float32), model,
                            model_onnx, basename="GaussianRandomProjection") 
Example #6
Source File: test_sklearn_random_projection.py    From sklearn-onnx with MIT License 5 votes vote down vote up
def test_gaussian_random_projection_float64(self):
        rng = np.random.RandomState(42)
        pt = GaussianRandomProjection(n_components=4)
        X = rng.rand(10, 5).astype(np.float64)
        model = pt.fit(X)
        model_onnx = to_onnx(model, X[:1], dtype=np.float64,
                             target_opset=TARGET_OPSET)
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(X, model,
                            model_onnx, basename="GaussianRandomProjection64") 
Example #7
Source File: test_random_projection.py    From pandas-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_objectmapper(self):
        df = pdml.ModelFrame([])
        self.assertIs(df.random_projection.GaussianRandomProjection, rp.GaussianRandomProjection)
        self.assertIs(df.random_projection.SparseRandomProjection, rp.SparseRandomProjection)
        self.assertIs(df.random_projection.johnson_lindenstrauss_min_dim, rp.johnson_lindenstrauss_min_dim)