Class: SVMKit::NearestNeighbors::KNeighborsClassifier
- Inherits:
-
Object
- Object
- SVMKit::NearestNeighbors::KNeighborsClassifier
- Includes:
- Base::BaseEstimator, Base::Classifier
- Defined in:
- lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb
Overview
KNeighborsClassifier is a class that implements the classifier with the k-nearest neighbors rule. The current implementation uses the Euclidean distance for finding the neighbors.
Instance Attribute Summary collapse
-
#classes ⇒ Numo::Int32
readonly
Return the class labels.
-
#labels ⇒ Numo::Int32
readonly
Return the labels of the prototypes.
-
#prototypes ⇒ Numo::DFloat
readonly
Return the prototypes for the nearest neighbor classifier.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ KNeighborsClassifier
Fit the model with given training data.
-
#initialize(n_neighbors: 5) ⇒ KNeighborsClassifier
constructor
Create a new classifier with the nearest neighbor rule.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
Methods included from Base::Classifier
Constructor Details
#initialize(n_neighbors: 5) ⇒ KNeighborsClassifier
Create a new classifier with the nearest neighbor rule.
38 39 40 41 42 43 44 45 46 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 38 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 @labels = nil @classes = nil end |
Instance Attribute Details
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
33 34 35 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 33 def classes @classes end |
#labels ⇒ Numo::Int32 (readonly)
Return the labels of the prototypes
29 30 31 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 29 def labels @labels end |
#prototypes ⇒ Numo::DFloat (readonly)
Return the prototypes for the nearest neighbor classifier.
25 26 27 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 25 def prototypes @prototypes end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 67 def decision_function(x) SVMKit::Validation.check_sample_array(x) distance_matrix = PairwiseMetric.euclidean_distance(x, @prototypes) n_samples, n_prototypes = distance_matrix.shape n_classes = @classes.size n_neighbors = [@params[:n_neighbors], n_prototypes].min scores = Numo::DFloat.zeros(n_samples, n_classes) n_samples.times do |m| neighbor_ids = distance_matrix[m, true].to_a.each_with_index.sort.map(&:last)[0...n_neighbors] neighbor_ids.each { |n| scores[m, @classes.to_a.index(@labels[n])] += 1.0 } end scores end |
#fit(x, y) ⇒ KNeighborsClassifier
Fit the model with given training data.
53 54 55 56 57 58 59 60 61 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 53 def fit(x, y) SVMKit::Validation.check_sample_array(x) SVMKit::Validation.check_label_array(y) SVMKit::Validation.check_sample_label_size(x, y) @prototypes = Numo::DFloat.asarray(x.to_a) @labels = Numo::Int32.asarray(y.to_a) @classes = Numo::Int32.asarray(y.to_a.uniq.sort) self end |
#marshal_dump ⇒ Hash
Dump marshal data.
94 95 96 97 98 99 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 94 def marshal_dump { params: @params, prototypes: @prototypes, labels: @labels, classes: @classes } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
103 104 105 106 107 108 109 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 103 def marshal_load(obj) @params = obj[:params] @prototypes = obj[:prototypes] @labels = obj[:labels] @classes = obj[:classes] nil end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
85 86 87 88 89 90 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 85 def predict(x) SVMKit::Validation.check_sample_array(x) n_samples = x.shape.first decision_values = decision_function(x) Numo::Int32.asarray(Array.new(n_samples) { |n| @classes[decision_values[n, true].max_index] }) end |