Class: OutlierTree::Result

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_outputs:, df:, numeric_columns:, categorical_columns:, categories:) ⇒ Result

Returns a new instance of Result.



5
6
7
8
9
10
11
# File 'lib/outliertree/result.rb', line 5

def initialize(model_outputs:, df:, numeric_columns:, categorical_columns:, categories:)
  @model_outputs = model_outputs
  @df = df
  @numeric_columns = numeric_columns
  @categorical_columns = categorical_columns
  @categories = categories
end

Instance Attribute Details

#dfObject (readonly)

Returns the value of attribute df.



3
4
5
# File 'lib/outliertree/result.rb', line 3

def df
  @df
end

#model_outputsObject (readonly)

Returns the value of attribute model_outputs.



3
4
5
# File 'lib/outliertree/result.rb', line 3

def model_outputs
  @model_outputs
end

Instance Method Details

#processObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/outliertree/result.rb', line 13

def process
  outliers = []
  model_outputs.outlier_scores_final.each_with_index do |score, row|
    if score < 1
      outl_col = model_outputs.outlier_columns_final[row]
      outl_clust = model_outputs.outlier_clusters_final[row]
      outl_tree = model_outputs.outlier_trees_final[row]

      # column and value
      if outl_col < @numeric_columns.size
        column = @numeric_columns[outl_col]
        value = df[column][row]
        _decimals = model_outputs.outlier_decimals_distr[row]
      else
        column = @categorical_columns[outl_col - @numeric_columns.size]
        value = df[column][row]
      end

      # group statistics
      group_statistics = {}
      if outl_col < @numeric_columns.size
        cluster = model_outputs.all_clusters(outl_col, outl_clust)
        if value >= cluster.upper_lim
          group_statistics[:upper_thr] = cluster.display_lim_high
          group_statistics[:pct_below] = cluster.perc_below
        else
          group_statistics[:lower_thr] = cluster.display_lim_low
          group_statistics[:pct_above] = cluster.perc_above
        end
        group_statistics[:mean] = cluster.display_mean
        group_statistics[:sd] = cluster.display_sd
        group_statistics[:n_obs] = cluster.cluster_size
      else
        # TODO categorical stats
      end

      # conditions
      conditions = []
      if cluster.column_type != :no_type
        conditions << add_condition(row, cluster.split_type, cluster)
      end

      # add conditions from tree branches
      curr_tree = outl_tree
      loop do
        break if curr_tree == 0

        tree = model_outputs.all_trees(outl_col, curr_tree)
        break if tree.parent_branch == :sub_trees

        parent_tree = tree.parent
        parent_cluster = model_outputs.all_trees(outl_col, parent_tree)

        if parent_cluster.all_branches.size > 0
          raise "Branch not supported yet. Please report an issue."
        else
          conditions << add_condition(row, tree.parent_branch, parent_cluster)
        end

        curr_tree = parent_tree
      end

      clean_conditions(conditions)

      outliers << {
        index: row,
        explanation: create_explanation(column, value, conditions, group_statistics),
        column: column,
        value: value,
        conditions: conditions,
        group_statistics: group_statistics,
        # leave out for simplicity
        # score: score,
        # tree_depth: model_outputs.outlier_depth_final[row],
        # has_na_branch: model_outputs.all_clusters(outl_col, outl_clust).has_na_branch
      }
    end
  end
  outliers
end