lib/svmkit/ensemble/random_forest_classifier.rb in svmkit-0.2.7 vs lib/svmkit/ensemble/random_forest_classifier.rb in svmkit-0.2.8
- old
+ new
@@ -48,31 +48,38 @@
# If nil is given, split process considers all features.
# @param random_seed [Integer] The seed value using to initialize the random generator.
# It is used to randomly determine the order of features when deciding spliting point.
def initialize(n_estimators: 10, criterion: 'gini', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1,
max_features: nil, random_seed: nil)
+ SVMKit::Validation.check_params_type_or_nil(Integer, max_depth: max_depth, max_leaf_nodes: max_leaf_nodes,
+ max_features: max_features, random_seed: random_seed)
+ SVMKit::Validation.check_params_integer(n_estimators: n_estimators, min_samples_leaf: min_samples_leaf)
+ SVMKit::Validation.check_params_string(criterion: criterion)
+
@params = {}
@params[:n_estimators] = n_estimators
@params[:criterion] = criterion
@params[:max_depth] = max_depth
@params[:max_leaf_nodes] = max_leaf_nodes
@params[:min_samples_leaf] = min_samples_leaf
@params[:max_features] = max_features
@params[:random_seed] = random_seed
@params[:random_seed] ||= srand
- @rng = Random.new(@params[:random_seed])
@estimators = nil
@classes = nil
@feature_importances = nil
+ @rng = Random.new(@params[:random_seed])
end
# Fit the model with given training data.
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
# @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
# @return [RandomForestClassifier] The learned classifier itself.
def fit(x, y)
+ SVMKit::Validation.check_sample_array(x)
+ SVMKit::Validation.check_label_array(y)
# Initialize some variables.
n_samples, n_features = x.shape
@params[:max_features] = n_features unless @params[:max_features].is_a?(Integer)
@params[:max_features] = [[1, @params[:max_features]].max, Math.sqrt(n_features).to_i].min
@classes = Numo::Int32.asarray(y.to_a.uniq.sort)
@@ -96,10 +103,11 @@
# Predict class labels for samples.
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
# @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
def predict(x)
+ SVMKit::Validation.check_sample_array(x)
n_samples, = x.shape
n_classes = @classes.size
classes_arr = @classes.to_a
ballot_box = Numo::DFloat.zeros(n_samples, n_classes)
@estimators.each do |tree|
@@ -115,10 +123,11 @@
# Predict probability for samples.
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities.
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
def predict_proba(x)
+ SVMKit::Validation.check_sample_array(x)
n_samples, = x.shape
n_classes = @classes.size
classes_arr = @classes.to_a
ballot_box = Numo::DFloat.zeros(n_samples, n_classes)
@estimators.each do |tree|
@@ -134,9 +143,10 @@
# Return the index of the leaf that each sample reached.
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
# @return [Numo::Int32] (shape: [n_samples, n_estimators]) Leaf index for sample.
def apply(x)
+ SVMKit::Validation.check_sample_array(x)
Numo::Int32[*Array.new(@params[:n_estimators]) { |n| @estimators[n].apply(x) }].transpose
end
# Dump marshal data.
# @return [Hash] The marshal data about RandomForestClassifier