如何计算多对多模型的特征向量?

时间:2018-05-30 12:09:23

标签: ruby-on-rails ruby rails-activerecord

我有一对多表示依赖图的模型。如何计算该图的特征向量?

有一个关系模型:

class Relation < ApplicationRecord
    belongs_to :dependency, class_name: "Article"
    belongs_to :dependent, class_name: "Article"
end

和关系表:

create_table "relations", force: :cascade do |t|
    t.integer "dependency_id"
    t.integer "dependent_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "user_id"
    t.index ["dependency_id", "dependent_id", "user_id"], name: "index_relations_on_dependency_id_and_dependent_id_and_user_id", unique: true
    t.index ["dependency_id"], name: "index_relations_on_dependency_id"
    t.index ["dependent_id"], name: "index_relations_on_dependent_id"
    t.index ["user_id"], name: "index_relations_on_user_id"
end

2 个答案:

答案 0 :(得分:1)

使用Ruby的Matrix :: EigenvalueDecomposition类可能吗?

m = Matrix[ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
Matrix::EigenvalueDecomposition.new(m).eigenvalues
  => [16.116843969807057, -1.1168439698070447, -8.046297179356069e-16] 

Matrix::EigenvalueDecomposition.new(m).eigenvectors
  => [Vector[0.23197068724628633, 0.5253220933012341, 0.8186734993561815], Vector[0.8169642040610365, 0.09018835790853766, -0.6365874882439635], Vector[0.40824829046386324, -0.8164965809277259, 0.40824829046386263]] 

请参阅 https://docs.ruby-lang.org/en/2.5.0/Matrix/EigenvalueDecomposition.html

答案 1 :(得分:0)

使用rankable_graph

“宝石主要用C语言编写,带有漂亮的Ruby包装。它的目的是用于大而不是巨大的图形,因为使用map-reduce分布式解决方案可以更好地处理它们。”