Class: IsoTree::IsolationForest

Inherits:
Object
  • Object
show all
Defined in:
lib/isotree/isolation_forest.rb

Instance Method Summary collapse

Constructor Details

#initialize(sample_size: nil, ntrees: 500, ndim: 3, ntry: 3, prob_pick_avg_gain: 0, prob_pick_pooled_gain: 0, prob_split_avg_gain: 0, prob_split_pooled_gain: 0, min_gain: 0, all_perm: false, coef_by_prop: false, sample_with_replacement: false, penalize_range: true, weigh_by_kurtosis: false, min_imp_obs: 3, random_seed: 1, nthreads: -1) ⇒ IsolationForest

Returns a new instance of IsolationForest.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/isotree/isolation_forest.rb', line 3

def initialize(
  sample_size: nil, ntrees: 500, ndim: 3, ntry: 3,
  prob_pick_avg_gain: 0, prob_pick_pooled_gain: 0,
  prob_split_avg_gain: 0, prob_split_pooled_gain: 0,
  min_gain: 0, all_perm: false, coef_by_prop: false,
  sample_with_replacement: false, penalize_range: true,
  weigh_by_kurtosis: false, min_imp_obs: 3, random_seed: 1, nthreads: -1
)

  @sample_size = sample_size
  @ntrees = ntrees
  @ndim = ndim
  @ntry = ntry
  @prob_pick_avg_gain = prob_pick_avg_gain
  @prob_pick_pooled_gain = prob_pick_pooled_gain
  @prob_split_avg_gain = prob_split_avg_gain
  @prob_split_pooled_gain = prob_split_pooled_gain
  @min_gain = min_gain
  @all_perm = all_perm
  @coef_by_prop = coef_by_prop
  @sample_with_replacement = sample_with_replacement
  @penalize_range = penalize_range
  @weigh_by_kurtosis = weigh_by_kurtosis
  @min_imp_obs = min_imp_obs
  @random_seed = random_seed

  # etc module returns virtual cores
  nthreads = Etc.nprocessors if nthreads < 0
  @nthreads = nthreads
end

Instance Method Details

#fit(x) ⇒ Object



34
35
36
37
38
39
# File 'lib/isotree/isolation_forest.rb', line 34

def fit(x)
  options = data_options(x).merge(fit_options)
  options[:sample_size] ||= options[:nrows]
  @ncols = options[:ncols]
  @ext_iso_forest = Ext.fit_iforest(options)
end

#predict(x) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/isotree/isolation_forest.rb', line 41

def predict(x)
  raise "Not fit" unless @ext_iso_forest
  options = data_options(x).merge(nthreads: @nthreads)
  if options[:ncols] != @ncols
    raise ArgumentError, "Input must have #{@ncols} columns for this model"
  end
  Ext.predict_iforest(@ext_iso_forest, options)
end