Class: MLRuby::KNearestNeighbors::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/ML_Ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(items, features = [], n_neighbors = 3) ⇒ Model

Returns a new instance of Model.



65
66
67
68
69
# File 'lib/ML_Ruby.rb', line 65

def initialize(items, features=[], n_neighbors=3)
  @items = items.to_json
  @features = features
  @n_neighbors = n_neighbors
end

Instance Method Details

#similar_with(id) ⇒ Object



70
71
72
73
74
# File 'lib/ML_Ruby.rb', line 70

def similar_with(id)
  script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/k_nearest_neighbors.py"
  result = `#{MLRuby::PYTHON_PATH} #{script_path} '#{@items}' '#{@features}' '#{id}' '#{@n_neighbors}'`
  JSON.parse(result.gsub(/'([^']+)'/, '"\1"'))
end