Class: SVMKit::NearestNeighbors::KNeighborsRegressor
- Inherits:
-
Object
- Object
- SVMKit::NearestNeighbors::KNeighborsRegressor
- Includes:
- Base::BaseEstimator, Base::Regressor
- Defined in:
- lib/svmkit/nearest_neighbors/k_neighbors_regressor.rb
Overview
KNeighborsRegressor is a class that implements the regressor with the k-nearest neighbors rule. The current implementation uses the Euclidean distance for finding the neighbors.
Instance Attribute Summary collapse
-
#prototypes ⇒ Numo::DFloat
readonly
Return the prototypes for the nearest neighbor regressor.
-
#values ⇒ Numo::DFloat
readonly
Return the values of the prototypes.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#fit(x, y) ⇒ KNeighborsRegressor
Fit the model with given training data.
-
#initialize(n_neighbors: 5) ⇒ KNeighborsRegressor
constructor
Create a new regressor with the nearest neighbor rule.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#predict(x) ⇒ Numo::DFloat
Predict values for samples.
Methods included from Base::Regressor
Constructor Details
#initialize(n_neighbors: 5) ⇒ KNeighborsRegressor
Create a new regressor with the nearest neighbor rule.
33 34 35 36 37 38 39 40 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_regressor.rb', line 33 def initialize(n_neighbors: 5) SVMKit::Validation.check_params_integer(n_neighbors: n_neighbors) SVMKit::Validation.check_params_positive(n_neighbors: n_neighbors) @params = {} @params[:n_neighbors] = n_neighbors @prototypes = nil @values = nil end |
Instance Attribute Details
#prototypes ⇒ Numo::DFloat (readonly)
Return the prototypes for the nearest neighbor regressor.
24 25 26 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_regressor.rb', line 24 def prototypes @prototypes end |
#values ⇒ Numo::DFloat (readonly)
Return the values of the prototypes
28 29 30 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_regressor.rb', line 28 def values @values end |
Instance Method Details
#fit(x, y) ⇒ KNeighborsRegressor
Fit the model with given training data.
47 48 49 50 51 52 53 54 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_regressor.rb', line 47 def fit(x, y) SVMKit::Validation.check_sample_array(x) SVMKit::Validation.check_tvalue_array(y) SVMKit::Validation.check_sample_tvalue_size(x, y) @prototypes = x.dup @values = y.dup self end |
#marshal_dump ⇒ Hash
Dump marshal data.
78 79 80 81 82 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_regressor.rb', line 78 def marshal_dump { params: @params, prototypes: @prototypes, values: @values } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
86 87 88 89 90 91 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_regressor.rb', line 86 def marshal_load(obj) @params = obj[:params] @prototypes = obj[:prototypes] @values = obj[:values] nil end |
#predict(x) ⇒ Numo::DFloat
Predict values for samples.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_regressor.rb', line 60 def predict(x) SVMKit::Validation.check_sample_array(x) # Initialize some variables. n_samples, = x.shape n_prototypes, n_outputs = @values.shape n_neighbors = [@params[:n_neighbors], n_prototypes].min # Calculate distance matrix. distance_matrix = PairwiseMetric.euclidean_distance(x, @prototypes) # Predict values for the given samples. predicted_values = Array.new(n_samples) do |n| neighbor_ids = distance_matrix[n, true].to_a.each_with_index.sort.map(&:last)[0...n_neighbors] n_outputs.nil? ? @values[neighbor_ids].mean : @values[neighbor_ids, true].mean(0).to_a end Numo::DFloat[*predicted_values] end |