Guest User

Untitled

a guest
Feb 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. def _make_nn_sparse_coded_signal(n_samples, n_components, n_features,
  2. n_nonzero_coefs, random_state=None):
  3. """Generate a signal as a sparse combination of dictionary elements.
  4. Returns a matrix Y = DX, such as D is (n_features, n_components),
  5. X is (n_components, n_samples) and each column of X has exactly
  6. n_nonzero_coefs non-zero elements.
  7. Read more in the :ref:`User Guide <sample_generators>`.
  8. Parameters
  9. ----------
  10. n_samples : int
  11. number of samples to generate
  12. n_components : int,
  13. number of components in the dictionary
  14. n_features : int
  15. number of features of the dataset to generate
  16. n_nonzero_coefs : int
  17. number of active (non-zero) coefficients in each sample
  18. random_state : int, RandomState instance or None, optional (default=None)
  19. If int, random_state is the seed used by the random number generator;
  20. If RandomState instance, random_state is the random number generator;
  21. If None, the random number generator is the RandomState instance used
  22. by `np.random`.
  23. Returns
  24. -------
  25. data : array of shape [n_features, n_samples]
  26. The encoded signal (Y).
  27. dictionary : array of shape [n_features, n_components]
  28. The dictionary with normalized components (D).
  29. code : array of shape [n_components, n_samples]
  30. The sparse code such that each column of this matrix has exactly
  31. n_nonzero_coefs non-zero items (X).
  32. """
  33. generator = check_random_state(random_state)
  34. # generate dictionary
  35. D = abs(generator.rand(n_features, n_components))
  36. D /= np.sqrt(np.sum((D ** 2), axis=0))
  37.  
  38. # generate code
  39. X = np.zeros((n_components, n_samples))
  40. for i in range(n_samples):
  41. idx = np.arange(n_components)
  42. generator.shuffle(idx)
  43. idx = idx[:n_nonzero_coefs]
  44. X[idx, i] = abs(generator.rand(n_nonzero_coefs))
  45.  
  46.  
  47. # encode signal
  48. Y = np.dot(D, X)
  49.  
  50. return map(np.squeeze, (Y, D, X))
Add Comment
Please, Sign In to add comment