Module: Umappp

Defined in:
lib/umappp.rb,
lib/umappp/version.rb

Overview

Uniform Manifold Approximation and Projection

Constant Summary collapse

VERSION =
"0.2.3"

Class Method Summary collapse

Class Method Details

.default_parametersObject

View the default parameters defined within the Umappp C++ library structure.



14
15
16
17
# File 'lib/umappp.rb', line 14

def self.default_parameters
  # {method: :annoy, ndim: 2}.merge
  umappp_default_parameters
end

.run(embedding, method: :annoy, ndim: 2, **params) ⇒ Numo::SFloat

Runs the Uniform Manifold Approximation and Projection (UMAP) dimensional reduction technique.

Parameters:

  • embedding (Array, Numo::SFloat)
  • method (Symbol, String) (defaults to: :annoy)
  • ndim (Integer) (defaults to: 2)
  • tick (Integer)
  • local_connectivity (Numeric)
  • bandwidth (Numeric)
  • mix_ratio (Numeric)
  • spread (Numeric)
  • min_dist (Numeric)
  • a (Numeric)
  • b (Numeric)
  • repulsion_strength (Numeric)
  • initialize (Symbol, Umappp::InitMethod)
  • num_epochs (Integer)
  • learning_rate (Numeric)
  • negative_sample_rate (Numeric)
  • num_neighbors (Integer)
  • seed (Integer)
  • num_threads (Integer)
  • parallel_optimization (Boolean)

Returns:

  • (Numo::SFloat)

    the final embedding

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/umappp.rb', line 43

def self.run(embedding, method: :annoy, ndim: 2, **params)
  unless (u = (params.keys - default_parameters.keys)).empty?
    raise ArgumentError, "Unknown option(s): #{u.inspect}"
  end

  nnmethod = i[annoy vptree].index(method.to_sym)
  raise ArgumentError, "method must be :annoy or :vptree" if nnmethod.nil?

  # Allow initialize: to be specified as a Symbol or String and
  # convert it to the corresponding Umappp::InitMethod enum.
  if params.key?(:initialize)
    init_val = params[:initialize]

    if init_val.is_a?(Symbol) || init_val.is_a?(String)
      init_sym = init_val.to_sym
      mapping = {
        spectral: InitMethod::SPECTRAL,
        spectral_only: InitMethod::SPECTRAL_ONLY,
        random: InitMethod::RANDOM,
        none: InitMethod::NONE
      }

      mapped = mapping[init_sym]
      unless mapped
        raise ArgumentError,
              "initialize must be one of :spectral, :spectral_only, :random, :none or a Umappp::InitMethod"
      end

      params[:initialize] = mapped
    end
    # If it's already a Umappp::InitMethod, we just pass it through.
  end

  embedding2 = Numo::SFloat.cast(embedding)
  if embedding2.ndim != 2
    raise ArgumentError,
          "embedding must be a 2D array, got #{embedding2.ndim}D with shape #{embedding2.shape.inspect}"
  end
  raise ArgumentError, "embedding must not be empty" if embedding2.empty?

  umappp_run(params, embedding2, ndim, nnmethod)
end