rumale 0.18.1 → 0.18.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -1
  3. data/CHANGELOG.md +16 -4
  4. data/lib/rumale.rb +6 -1
  5. data/lib/rumale/clustering/dbscan.rb +0 -17
  6. data/lib/rumale/clustering/gaussian_mixture.rb +0 -21
  7. data/lib/rumale/clustering/hdbscan.rb +0 -15
  8. data/lib/rumale/clustering/k_means.rb +0 -17
  9. data/lib/rumale/clustering/k_medoids.rb +0 -19
  10. data/lib/rumale/clustering/power_iteration.rb +0 -19
  11. data/lib/rumale/clustering/single_linkage.rb +0 -17
  12. data/lib/rumale/clustering/spectral_clustering.rb +0 -17
  13. data/lib/rumale/evaluation_measure/function.rb +34 -0
  14. data/lib/rumale/kernel_approximation/rbf.rb +0 -19
  15. data/lib/rumale/kernel_machine/kernel_pca.rb +0 -21
  16. data/lib/rumale/kernel_machine/kernel_ridge.rb +0 -15
  17. data/lib/rumale/kernel_machine/kernel_svc.rb +0 -21
  18. data/lib/rumale/naive_bayes/base_naive_bayes.rb +47 -0
  19. data/lib/rumale/naive_bayes/bernoulli_nb.rb +82 -0
  20. data/lib/rumale/naive_bayes/complement_nb.rb +85 -0
  21. data/lib/rumale/naive_bayes/gaussian_nb.rb +69 -0
  22. data/lib/rumale/naive_bayes/multinomial_nb.rb +74 -0
  23. data/lib/rumale/naive_bayes/negation_nb.rb +71 -0
  24. data/lib/rumale/nearest_neighbors/k_neighbors_classifier.rb +0 -19
  25. data/lib/rumale/nearest_neighbors/k_neighbors_regressor.rb +0 -17
  26. data/lib/rumale/neural_network/adam.rb +0 -19
  27. data/lib/rumale/preprocessing/bin_discretizer.rb +0 -15
  28. data/lib/rumale/preprocessing/label_binarizer.rb +0 -15
  29. data/lib/rumale/preprocessing/label_encoder.rb +0 -15
  30. data/lib/rumale/preprocessing/max_abs_scaler.rb +0 -15
  31. data/lib/rumale/preprocessing/min_max_scaler.rb +0 -17
  32. data/lib/rumale/preprocessing/one_hot_encoder.rb +0 -19
  33. data/lib/rumale/preprocessing/ordinal_encoder.rb +0 -13
  34. data/lib/rumale/preprocessing/standard_scaler.rb +0 -15
  35. data/lib/rumale/version.rb +1 -1
  36. metadata +8 -3
  37. data/lib/rumale/naive_bayes/naive_bayes.rb +0 -250
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b344bc829bda6e2b5f60baba450f3c38d5f90ebf0c0cfccd02d2894189d540e
4
- data.tar.gz: 91fcce138ced31e94363b6f137bd66b6e1637d1fb5d03a1b1b531a3e1d2a3502
3
+ metadata.gz: 4028734b509ce8a05301fe152bdcc2a26a0e318e692a866d3bc1669ca7e5f859
4
+ data.tar.gz: c2dfc3614f786b59ce72a96adc2ca16b140a72e42f4851cb2cba8cebb3cab8dc
5
5
  SHA512:
6
- metadata.gz: 2d2176c440222ba9b1265ce97b575649670c4067da4ddd51529b219ba63d8e15852227843474ae40c8af328d7c366b5ee49f63dac5982c0e1db96157315dd256
7
- data.tar.gz: 2dc2d4d16ed22e837e603cd2b7c50a06e3c57d3c5882f47df671ea096aadbab6cae8c0ca50d940f9545d59163fa3034a667d80df84f600900b2c29c7b519dded
6
+ metadata.gz: 53b09b0eb4f783e5c9980023ad90749cee0ad7b77de590b0622a97bbc4899d96fdd6b3181a287026c547925d0938af925754c81a6eca5e9aca5746fb65699632
7
+ data.tar.gz: 5f45554a4d2fb0486a2c84a317c8faf1ca98a001c175380e12efcaba9ea1a909c337b9674f6d094ca25df2f9fdb372b7e460631a4fa99d19bce68e10200dc213
@@ -1,4 +1,3 @@
1
- sudo: false
2
1
  os: linux
3
2
  dist: xenial
4
3
  language: ruby
@@ -1,12 +1,24 @@
1
+ # 0.18.2
2
+ - Change file composition of naive bayes classifiers.
3
+ - Add classifier class for [ComplementNaiveBayes](https://yoshoku.github.io/rumale/doc/Rumale/NaiveBayes/ComplementNB.html).
4
+ - Add classifier class for [NegationNaiveBayes](https://yoshoku.github.io/rumale/doc/Rumale/NaiveBayes/NegationNB.html).
5
+ - Add [module function](https://yoshoku.github.io/rumale/doc/Rumale/EvaluationMeasure.html#confusion_matrix-class_method) for calculating confusion matrix.
6
+ - Delete unneeded marshal dump and load methods.
7
+ - [Clustering](https://yoshoku.github.io/rumale/doc/Rumale/Clustering.html),
8
+ [KernelApproximation](https://yoshoku.github.io/rumale/doc/Rumale/KernelApproximation.html),
9
+ [KernelMachine](https://yoshoku.github.io/rumale/doc/Rumale/KernelMachine.html),
10
+ [NearestNeighbors](https://yoshoku.github.io/rumale/doc/Rumale/NearestNeighbors.html),
11
+ [Preprocessing](https://yoshoku.github.io/rumale/doc/Rumale/Preprocessing.html).
12
+
1
13
  # 0.18.1
2
14
  - Add [module function](https://yoshoku.github.io/rumale/doc/Rumale/EvaluationMeasure.html#classification_report-class_method) for generating summary of classification performance.
3
15
  - Delete marshal dump and load methods for documentation.
4
16
  The marshal methods are written in estimator classes for indicating on API documentation that the learned model can be saved with Marshal.
5
17
  Even without these methods, Marshal can save the learned model, so they are deleted sequentially.
6
- - [Manifold](https://yoshoku.github.io/rumale/doc/Rumale/Manifold.html)
7
- - [NaiveBayes](https://yoshoku.github.io/rumale/doc/Rumale/NaiveBayes.html)
8
- - [PolynomialModel](https://yoshoku.github.io/rumale/doc/Rumale/PolynomialModel.html)
9
- - [Decomposition](https://yoshoku.github.io/doc/Rumale/Decomposition.html)
18
+ - [Manifold](https://yoshoku.github.io/rumale/doc/Rumale/Manifold.html),
19
+ [NaiveBayes](https://yoshoku.github.io/rumale/doc/Rumale/NaiveBayes.html),
20
+ [PolynomialModel](https://yoshoku.github.io/rumale/doc/Rumale/PolynomialModel.html),
21
+ [Decomposition](https://yoshoku.github.io/doc/Rumale/Decomposition.html).
10
22
 
11
23
  # 0.18.0
12
24
  - Add transformer class for [FisherDiscriminantAnalysis](https://yoshoku.github.io/rumale/doc/Rumale/MetricLearning/FisherDiscriminantAnalysis.html).
@@ -47,7 +47,12 @@ require 'rumale/multiclass/one_vs_rest_classifier'
47
47
  require 'rumale/nearest_neighbors/vp_tree'
48
48
  require 'rumale/nearest_neighbors/k_neighbors_classifier'
49
49
  require 'rumale/nearest_neighbors/k_neighbors_regressor'
50
- require 'rumale/naive_bayes/naive_bayes'
50
+ require 'rumale/naive_bayes/base_naive_bayes'
51
+ require 'rumale/naive_bayes/bernoulli_nb'
52
+ require 'rumale/naive_bayes/complement_nb'
53
+ require 'rumale/naive_bayes/gaussian_nb'
54
+ require 'rumale/naive_bayes/multinomial_nb'
55
+ require 'rumale/naive_bayes/negation_nb'
51
56
  require 'rumale/tree/node'
52
57
  require 'rumale/tree/base_decision_tree'
53
58
  require 'rumale/tree/decision_tree_classifier'
@@ -70,23 +70,6 @@ module Rumale
70
70
  labels
71
71
  end
72
72
 
73
- # Dump marshal data.
74
- # @return [Hash] The marshal data.
75
- def marshal_dump
76
- { params: @params,
77
- core_sample_ids: @core_sample_ids,
78
- labels: @labels }
79
- end
80
-
81
- # Load marshal data.
82
- # @return [nil]
83
- def marshal_load(obj)
84
- @params = obj[:params]
85
- @core_sample_ids = obj[:core_sample_ids]
86
- @labels = obj[:labels]
87
- nil
88
- end
89
-
90
73
  private
91
74
 
92
75
  def partial_fit(x)
@@ -114,27 +114,6 @@ module Rumale
114
114
  fit(x).predict(x)
115
115
  end
116
116
 
117
- # Dump marshal data.
118
- # @return [Hash] The marshal data.
119
- def marshal_dump
120
- { params: @params,
121
- n_iter: @n_iter,
122
- weights: @weights,
123
- means: @means,
124
- covariances: @covariances }
125
- end
126
-
127
- # Load marshal data.
128
- # @return [nil]
129
- def marshal_load(obj)
130
- @params = obj[:params]
131
- @n_iter = obj[:n_iter]
132
- @weights = obj[:weights]
133
- @means = obj[:means]
134
- @covariances = obj[:covariances]
135
- nil
136
- end
137
-
138
117
  private
139
118
 
140
119
  def assign_cluster(memberships)
@@ -71,21 +71,6 @@ module Rumale
71
71
  @labels = partial_fit(distance_mat)
72
72
  end
73
73
 
74
- # Dump marshal data.
75
- # @return [Hash] The marshal data.
76
- def marshal_dump
77
- { params: @params,
78
- labels: @labels }
79
- end
80
-
81
- # Load marshal data.
82
- # @return [nil]
83
- def marshal_load(obj)
84
- @params = obj[:params]
85
- @labels = obj[:labels]
86
- nil
87
- end
88
-
89
74
  private
90
75
 
91
76
  # @!visibility private
@@ -92,23 +92,6 @@ module Rumale
92
92
  predict(x)
93
93
  end
94
94
 
95
- # Dump marshal data.
96
- # @return [Hash] The marshal data.
97
- def marshal_dump
98
- { params: @params,
99
- cluster_centers: @cluster_centers,
100
- rng: @rng }
101
- end
102
-
103
- # Load marshal data.
104
- # @return [nil]
105
- def marshal_load(obj)
106
- @params = obj[:params]
107
- @cluster_centers = obj[:cluster_centers]
108
- @rng = obj[:rng]
109
- nil
110
- end
111
-
112
95
  private
113
96
 
114
97
  def assign_cluster(x)
@@ -111,25 +111,6 @@ module Rumale
111
111
  end
112
112
  end
113
113
 
114
- # Dump marshal data.
115
- # @return [Hash] The marshal data.
116
- def marshal_dump
117
- { params: @params,
118
- medoid_ids: @medoid_ids,
119
- cluster_centers: @cluster_centers,
120
- rng: @rng }
121
- end
122
-
123
- # Load marshal data.
124
- # @return [nil]
125
- def marshal_load(obj)
126
- @params = obj[:params]
127
- @medoid_ids = obj[:medoid_ids]
128
- @cluster_centers = obj[:cluster_centers]
129
- @rng = obj[:rng]
130
- nil
131
- end
132
-
133
114
  private
134
115
 
135
116
  def assign_cluster(distances_to_medoids)
@@ -89,25 +89,6 @@ module Rumale
89
89
  @labels = line_kmeans_clustering(@embedding)
90
90
  end
91
91
 
92
- # Dump marshal data.
93
- # @return [Hash] The marshal data.
94
- def marshal_dump
95
- { params: @params,
96
- embedding: @embedding,
97
- labels: @labels,
98
- n_iter: @n_iter }
99
- end
100
-
101
- # Load marshal data.
102
- # @return [nil]
103
- def marshal_load(obj)
104
- @params = obj[:params]
105
- @embedding = obj[:embedding]
106
- @labels = obj[:labels]
107
- @n_iter = obj[:n_iter]
108
- nil
109
- end
110
-
111
92
  private
112
93
 
113
94
  def embedded_space(affinity_mat, max_iter, tol)
@@ -70,23 +70,6 @@ module Rumale
70
70
  @labels = partial_fit(distance_mat)
71
71
  end
72
72
 
73
- # Dump marshal data.
74
- # @return [Hash] The marshal data.
75
- def marshal_dump
76
- { params: @params,
77
- labels: @labels,
78
- hierarchy: @hierarchy }
79
- end
80
-
81
- # Load marshal data.
82
- # @return [nil]
83
- def marshal_load(obj)
84
- @params = obj[:params]
85
- @labels = obj[:labels]
86
- @hierarchy = obj[:hierarchy]
87
- nil
88
- end
89
-
90
73
  private
91
74
 
92
75
  # @!visibility private
@@ -92,23 +92,6 @@ module Rumale
92
92
  @labels = kmeans_clustering(normalized_embedding)
93
93
  end
94
94
 
95
- # Dump marshal data.
96
- # @return [Hash] The marshal data.
97
- def marshal_dump
98
- { params: @params,
99
- embedding: @embedding,
100
- labels: @labels }
101
- end
102
-
103
- # Load marshal data.
104
- # @return [nil]
105
- def marshal_load(obj)
106
- @params = obj[:params]
107
- @embedding = obj[:embedding]
108
- @labels = obj[:labels]
109
- nil
110
- end
111
-
112
95
  private
113
96
 
114
97
  def embedded_space(affinity_mat, n_clusters)
@@ -8,6 +8,40 @@ module Rumale
8
8
  module EvaluationMeasure
9
9
  module_function
10
10
 
11
+ # Calculate confusion matrix for evaluating classification performance.
12
+ #
13
+ # @example
14
+ # y_true = Numo::Int32[2, 0, 2, 2, 0, 1]
15
+ # y_pred = Numo::Int32[0, 0, 2, 2, 0, 2]
16
+ # p confusion_matrix(y_true, y_pred)
17
+ #
18
+ # # Numo::Int32#shape=[3,3]
19
+ # # [[2, 0, 0],
20
+ # # [0, 0, 1],
21
+ # # [1, 0, 2]]
22
+ #
23
+ # @param y_true [Numo::Int32] (shape: [n_samples]) The ground truth labels.
24
+ # @param y_pred [Numo::Int32] (shape: [n_samples]) The predicted labels.
25
+ # @return [Numo::Int32] (shape: [n_classes, n_classes]) The confusion matrix.
26
+ def confusion_matrix(y_true, y_pred)
27
+ y_true = Rumale::Validation.check_convert_label_array(y_true)
28
+ y_pred = Rumale::Validation.check_convert_label_array(y_pred)
29
+
30
+ labels = y_true.to_a.uniq.sort
31
+ n_labels = labels.size
32
+
33
+ conf_mat = Numo::Int32.zeros(n_labels, n_labels)
34
+
35
+ labels.each_with_index do |lbl_a, i|
36
+ y_p = y_pred[y_true.eq(lbl_a)]
37
+ labels.each_with_index do |lbl_b, j|
38
+ conf_mat[i, j] = y_p.eq(lbl_b).count
39
+ end
40
+ end
41
+
42
+ conf_mat
43
+ end
44
+
11
45
  # Output a summary of classification performance for each class.
12
46
  #
13
47
  # @example
@@ -97,25 +97,6 @@ module Rumale
97
97
  projection = x.dot(@random_mat) + @random_vec.tile(n_samples, 1)
98
98
  Numo::NMath.sin(projection) * ((2.0 / @params[:n_components])**0.5)
99
99
  end
100
-
101
- # Dump marshal data.
102
- # @return [Hash] The marshal data about RBF.
103
- def marshal_dump
104
- { params: @params,
105
- random_mat: @random_mat,
106
- random_vec: @random_vec,
107
- rng: @rng }
108
- end
109
-
110
- # Load marshal data.
111
- # @return [nil]
112
- def marshal_load(obj)
113
- @params = obj[:params]
114
- @random_mat = obj[:random_mat]
115
- @random_vec = obj[:random_vec]
116
- @rng = obj[:rng]
117
- nil
118
- end
119
100
  end
120
101
  end
121
102
  end
@@ -91,27 +91,6 @@ module Rumale
91
91
  transformed = centered_kernel_mat.dot(transform_mat)
92
92
  @params[:n_components] == 1 ? transformed[true, 0].dup : transformed
93
93
  end
94
-
95
- # Dump marshal data.
96
- # @return [Hash] The marshal data.
97
- def marshal_dump
98
- { params: @params,
99
- row_mean: @row_mean,
100
- all_mean: @all_mean,
101
- alphas: @alphas,
102
- lambdas: @lambdas }
103
- end
104
-
105
- # Load marshal data.
106
- # @return [nil]
107
- def marshal_load(obj)
108
- @params = obj[:params]
109
- @row_mean = obj[:row_mean]
110
- @all_mean = obj[:all_mean]
111
- @alphas = obj[:alphas]
112
- @lambdas = obj[:lambdas]
113
- nil
114
- end
115
94
  end
116
95
  end
117
96
  end
@@ -75,21 +75,6 @@ module Rumale
75
75
  x = check_convert_sample_array(x)
76
76
  x.dot(@weight_vec)
77
77
  end
78
-
79
- # Dump marshal data.
80
- # @return [Hash] The marshal data.
81
- def marshal_dump
82
- { params: @params,
83
- weight_vec: @weight_vec }
84
- end
85
-
86
- # Load marshal data.
87
- # @return [nil]
88
- def marshal_load(obj)
89
- @params = obj[:params]
90
- @weight_vec = obj[:weight_vec]
91
- nil
92
- end
93
78
  end
94
79
  end
95
80
  end
@@ -161,27 +161,6 @@ module Rumale
161
161
  probs
162
162
  end
163
163
 
164
- # Dump marshal data.
165
- # @return [Hash] The marshal data about KernelSVC.
166
- def marshal_dump
167
- { params: @params,
168
- weight_vec: @weight_vec,
169
- prob_param: @prob_param,
170
- classes: @classes,
171
- rng: @rng }
172
- end
173
-
174
- # Load marshal data.
175
- # @return [nil]
176
- def marshal_load(obj)
177
- @params = obj[:params]
178
- @weight_vec = obj[:weight_vec]
179
- @prob_param = obj[:prob_param]
180
- @classes = obj[:classes]
181
- @rng = obj[:rng]
182
- nil
183
- end
184
-
185
164
  private
186
165
 
187
166
  def partial_fit(x, bin_y)
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rumale/base/base_estimator'
4
+ require 'rumale/base/classifier'
5
+
6
+ module Rumale
7
+ # This module consists of the classes that implement naive bayes models.
8
+ module NaiveBayes
9
+ # BaseNaiveBayes is a class that has methods for common processes of naive bayes classifier.
10
+ # This class is used internally.
11
+ class BaseNaiveBayes
12
+ include Base::BaseEstimator
13
+ include Base::Classifier
14
+
15
+ # Predict class labels for samples.
16
+ #
17
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
18
+ # @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
19
+ def predict(x)
20
+ x = check_convert_sample_array(x)
21
+ n_samples = x.shape.first
22
+ decision_values = decision_function(x)
23
+ Numo::Int32.asarray(Array.new(n_samples) { |n| @classes[decision_values[n, true].max_index] })
24
+ end
25
+
26
+ # Predict log-probability for samples.
27
+ #
28
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the log-probailities.
29
+ # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted log-probability of each class per sample.
30
+ def predict_log_proba(x)
31
+ x = check_convert_sample_array(x)
32
+ n_samples, = x.shape
33
+ log_likelihoods = decision_function(x)
34
+ log_likelihoods - Numo::NMath.log(Numo::NMath.exp(log_likelihoods).sum(1)).reshape(n_samples, 1)
35
+ end
36
+
37
+ # Predict probability for samples.
38
+ #
39
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities.
40
+ # @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
41
+ def predict_proba(x)
42
+ x = check_convert_sample_array(x)
43
+ Numo::NMath.exp(predict_log_proba(x)).abs
44
+ end
45
+ end
46
+ end
47
+ end