lib/svmkit/preprocessing/min_max_scaler.rb in svmkit-0.2.7 vs lib/svmkit/preprocessing/min_max_scaler.rb in svmkit-0.2.8
- old
+ new
@@ -26,10 +26,11 @@
# Creates a new normalizer for scaling each feature to a given range.
#
# @param feature_range [Array<Float>] The desired range of samples.
def initialize(feature_range: [0.0, 1.0])
+ SVMKit::Validation.check_params_type(Array, feature_range: feature_range)
@params = {}
@params[:feature_range] = feature_range
@min_vec = nil
@max_vec = nil
end
@@ -39,10 +40,11 @@
# @overload fit(x) -> MinMaxScaler
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate the minimum and maximum values.
# @return [MinMaxScaler]
def fit(x, _y = nil)
+ SVMKit::Validation.check_sample_array(x)
@min_vec = x.min(0)
@max_vec = x.max(0)
self
end
@@ -51,17 +53,19 @@
# @overload fit_transform(x) -> Numo::DFloat
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to calculate the minimum and maximum values.
# @return [Numo::DFloat] The scaled samples.
def fit_transform(x, _y = nil)
+ SVMKit::Validation.check_sample_array(x)
fit(x).transform(x)
end
# Perform scaling the given samples according to feature_range.
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be scaled.
# @return [Numo::DFloat] The scaled samples.
def transform(x)
+ SVMKit::Validation.check_sample_array(x)
n_samples, = x.shape
dif_vec = @max_vec - @min_vec
nx = (x - @min_vec.tile(n_samples, 1)) / dif_vec.tile(n_samples, 1)
nx * (@params[:feature_range][1] - @params[:feature_range][0]) + @params[:feature_range][0]
end