我正在使用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:inestimated_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:ineach' /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:inserializable_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:inas_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:incheck_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:inblock 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:inas_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:incheck_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:inencode' /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:inblock (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:inblock 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:inblock (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:inblock (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:incatch' /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:inblock 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:inroute!' /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:inblock 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:ininvoke' /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:inblock 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:incatch' /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:incall!' /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:incall' /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:incall' /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:incall' /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:incall' /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:incall' /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:inprocess_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:inmain_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上创建了一个问题,其中讨论了如何处理问题。但据我所知,基于此讨论,宝石永远不会有任何变化。