lib/svmkit/preprocessing/min_max_scaler.rb in svmkit-0.1.3 vs lib/svmkit/preprocessing/min_max_scaler.rb in svmkit-0.2.0
- old
+ new
@@ -12,81 +12,74 @@
# new_testing_samples = normalizer.transform(testing_samples)
class MinMaxScaler
include Base::BaseEstimator
include Base::Transformer
- # @!visibility private
- DEFAULT_PARAMS = {
- feature_range: [0.0, 1.0]
- }.freeze
-
# Return the vector consists of the minimum value for each feature.
- # @return [NMatrix] (shape: [1, n_features])
+ # @return [Numo::DFloat] (shape: [n_features])
attr_reader :min_vec
# Return the vector consists of the maximum value for each feature.
- # @return [NMatrix] (shape: [1, n_features])
+ # @return [Numo::DFloat] (shape: [n_features])
attr_reader :max_vec
# Creates a new normalizer for scaling each feature to a given range.
#
- # @overload new(feature_range: [0.0, 1.0]) -> MinMaxScaler
- #
- # @param params [Hash] The parameters for MinMaxScaler.
- # @option params [Array<Float>] :feature_range ([0.0, 1.0]) The desired range of samples.
- def initialize(params = {})
- @params = DEFAULT_PARAMS.merge(Hash[params.map { |k, v| [k.to_sym, v] }])
+ # @param feature_range [Array<Float>] The desired range of samples.
+ def initialize(feature_range: [0.0, 1.0])
+ self.params = {}
+ self.params[:feature_range] = feature_range
@min_vec = nil
@max_vec = nil
end
# Calculate the minimum and maximum value of each feature for scaling.
#
# @overload fit(x) -> MinMaxScaler
#
- # @param x [NMatrix] (shape: [n_samples, n_features]) The samples to calculate the minimum and maximum values.
+ # @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)
@min_vec = x.min(0)
@max_vec = x.max(0)
self
end
# Calculate the minimum and maximum values, and then normalize samples to feature_range.
#
- # @overload fit_transform(x) -> NMatrix
+ # @overload fit_transform(x) -> Numo::DFloat
#
- # @param x [NMatrix] (shape: [n_samples, n_features]) The samples to calculate the minimum and maximum values.
- # @return [NMatrix] The scaled samples.
+ # @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)
fit(x).transform(x)
end
# Perform scaling the given samples according to feature_range.
#
- # @param x [NMatrix] (shape: [n_samples, n_features]) The samples to be scaled.
- # @return [NMatrix] The scaled samples.
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be scaled.
+ # @return [Numo::DFloat] The scaled samples.
def transform(x)
n_samples, = x.shape
dif_vec = @max_vec - @min_vec
- nx = (x - @min_vec.repeat(n_samples, 0)) / dif_vec.repeat(n_samples, 0)
+ 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
# Dump marshal data.
# @return [Hash] The marshal data about MinMaxScaler.
def marshal_dump
{ params: @params,
- min_vec: Utils.dump_nmatrix(@min_vec),
- max_vec: Utils.dump_nmatrix(@max_vec) }
+ min_vec: @min_vec,
+ max_vec: @max_vec }
end
# Load marshal data.
# @return [nil]
def marshal_load(obj)
@params = obj[:params]
- @min_vec = Utils.restore_nmatrix(obj[:min_vec])
- @max_vec = Utils.restore_nmatrix(obj[:max_vec])
+ @min_vec = obj[:min_vec]
+ @max_vec = obj[:max_vec]
nil
end
end
end
end