Class: Rumale::EvaluationMeasure::Purity
- Inherits:
-
Object
- Object
- Rumale::EvaluationMeasure::Purity
- Includes:
- Base::Evaluator
- Defined in:
- rumale-evaluation_measure/lib/rumale/evaluation_measure/purity.rb
Overview
Purity is a class that calculates the purity of cluatering results.
Reference
-
Manning, C D., Raghavan, P., and Schutze, H., “Introduction to Information Retrieval,” Cambridge University Press., 2008.
Instance Method Summary collapse
-
#score(y_true, y_pred) ⇒ Float
Calculate purity.
Instance Method Details
#score(y_true, y_pred) ⇒ Float
Calculate purity
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/purity.rb', line 25 def score(y_true, y_pred) # initiazlie some variables. purity = 0 n_samples = y_pred.size class_ids = y_true.to_a.uniq cluster_ids = y_pred.to_a.uniq # calculate purity. cluster_ids.each do |k| pr_sample_ids = y_pred.eq(k).where.to_a purity += class_ids.map { |j| (pr_sample_ids & y_true.eq(j).where.to_a).size }.max end purity.fdiv(n_samples) end |