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.
37 38 39 40 41 42 43 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 37 def initialize(n_neighbors: 5) @params = {} @params[:n_neighbors] = n_neighbors @prototypes = nil @labels = nil @classes = nil end |
Instance Attribute Details
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
32 33 34 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 32 def classes @classes end |
#labels ⇒ Numo::Int32 (readonly)
Return the labels of the prototypes
28 29 30 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 28 def labels @labels end |
#prototypes ⇒ Numo::DFloat (readonly)
Return the prototypes for the nearest neighbor classifier.
24 25 26 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 24 def prototypes @prototypes end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 61 def decision_function(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.
50 51 52 53 54 55 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 50 def fit(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.
86 87 88 89 90 91 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 86 def marshal_dump { params: @params, prototypes: @prototypes, labels: @labels, classes: @classes } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
95 96 97 98 99 100 101 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 95 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.
78 79 80 81 82 |
# File 'lib/svmkit/nearest_neighbors/k_neighbors_classifier.rb', line 78 def predict(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 |