NoMethodError - 使用decisiontree gem时未定义的方法'to_a'

时间:2013-11-18 09:35:22

标签: ruby decision-tree

我正在使用igrigoriks decisiontree gem并从数据库条目创建训练集,如下所示:

attributes = [ 'day', 'time' ]
trainingSet = []
if self.locations.count > 0
  self.locations.each{ |location| 
    entry = Array.new(3)
    day = location.created_at.strftime('%u').to_i
    time_category = (location.created_at.hour / 8).floor.to_i
    entry = [ day, time_category, location.longitude ]
    trainingSet.push(entry)
  }
end

然后创建一个这样的决策树:

decisionTree = DecisionTree::ID3Tree.new(attributes, trainingSet, 1, :discrete)

然后我尝试创建一个测试集并测试预测,如下所示:

test = [ 5, 1, 10]
decision = decisionTree.predict(test)

但是当我尝试这个时,我就得到了

undefined method `to_a' for 9.9907190386056:Float

有没有人知道我可能做错了什么或者能指出我正确的方向?

更新1
事实证明我的数据不适合测试,因为如果训练集中的所有条目都具有相同的属性值,它将无法工作。
对于只有目标特征变化的属性,我的测试都是[5,1]。 我知道我的很多数据看起来都是这样的,属性值相等,那么处理这个错误的方法是什么呢?

更新2
根据要求,这是回溯。

  

/Users/kasper/.rvm/gems/ruby-2.0.0-p247/bundler/gems/decisiontree-4da9dcf68eb0/lib/decisiontree/id3_tree.rb:166:in descend' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/bundler/gems/decisiontree-4da9dcf68eb0/lib/decisiontree/id3_tree.rb:131:in预测'   /Library/WebServer/Documents/sips/models/device.rb:59:in estimated_location' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/serialization.rb:39:in阻止在serializable_hash'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/serialization.rb:38:in each' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/serialization.rb:38:in inject'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/serialization.rb:38:in serializable_hash' /Library/WebServer/Documents/sips/models/device.rb:72:in serializable_hash'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-3.2.14/lib/active_model/serializers/json.rb:96:in as_json' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:55:in阻止as_json'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:77:in check_for_circular_references' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:54:in as_json'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:215:in block in as_json' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:215:in map'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:215:in as_json' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:47:in阻止编码'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:77:in check_for_circular_references' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:46:in encode'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/json/encoding.rb:31:in encode' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.14/lib/active_support/core_ext/object/to_json.rb:16:in to_json'   /Library/WebServer/Documents/sips/routes/api-devices.rb:55:in block (3 levels) in <class:App>' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1541:in call'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1541:in block in compile!' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:950:in []'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:950:in block (3 levels) in route!' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:966:in route_eval'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:950:in block (2 levels) in route!' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:987:in块in process_route'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:985:in catch' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:985:in process_route'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:948:in block in route!' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:947:in each'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:947:in route!' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1059:in阻止派遣!'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in block in invoke' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in catch'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in invoke' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1056:in发送!'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:882:in block in call!' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in阻止调用'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in catch' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in调用'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:882:in call!' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:870:in call'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/rack-protection-1.5.0/lib/rack/protection/xss_header.rb:18:in call' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/rack-protection-1.5.0/lib/rack/protection/path_traversal.rb:16:in call'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/rack-protection-1.5.0/lib/rack/protection/json_csrf.rb:18:in call' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in call'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in call' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/rack-protection-1.5.0/lib/rack/protection/frame_options.rb:31:in call'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/nulllogger.rb:9:in call' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/head.rb:11:in call'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:175:in call' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1949:in call'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.19/lib/phusion_passenger/rack/thread_handler_extension.rb:77:in process_request' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.19/lib/phusion_passenger/request_handler/thread_handler.rb:140:in accept_and_process_next_request'   /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.19/lib/phusion_passenger/request_handler/thread_handler.rb:108:in main_loop' /Users/kasper/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.19/lib/phusion_passenger/request_handler.rb:441:in阻止(3级) start_threads'

更新3 我在Github repo here上创建了一个问题,其中讨论了如何处理问题。但据我所知,基于此讨论,宝石永远不会有任何变化。

0 个答案:

没有答案