rumale 0.18.4 → 0.18.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4ec0f04029a88ea7950bb74bf9e0bd970cf8b546253da4ef4545244ad0adf68
4
- data.tar.gz: ed927fb58359fe49d4834593ec2034535f5ac8d37c773d6cb5a53227c4777f61
3
+ metadata.gz: 90ea96f8e47e9710da8f9e3d2c0d88b45e5ec86f349fb4dc1d9058867750c88c
4
+ data.tar.gz: dde564e3cb15d6abbbb35c2629d1b9c53c79a456f29a60a5cfe7780aa06dd47b
5
5
  SHA512:
6
- metadata.gz: 58727ddc9c5c6f9c12ac8231f57295988795bf07ec6f748543fff924a4d8085472a210789992ba2a3cc4827300823d141313da1fd20a10be3b5970b40871e4e3
7
- data.tar.gz: cc1af7ff552a9fe516e0588081ad6645cb03d4c8dd774492ea35c3e1a28eece7383dc88604c41e1cc1417f5d05af1324a7b97c121cb7ee76bcc697e4173195be
6
+ metadata.gz: aaaad994a319b406686962301ad28a67e9d92b3fd15930f9792eb0e6249342d98b08a822737771f19501928036cfdf64a41dede62e9a3fe114aefa13a77f72c6
7
+ data.tar.gz: f71fe52af3418d4ff74641a6f651fda783eca32450c36023de276ed41f0e326e035f3996f9433e2514569f8bcb496276bc582d0b507a5c603c90164ad388fad4
@@ -15,10 +15,26 @@ AllCops:
15
15
  Style/Documentation:
16
16
  Enabled: false
17
17
 
18
+ Style/HashEachMethods:
19
+ Enabled: true
20
+
21
+ Style/HashTransformKeys:
22
+ Enabled: true
23
+
24
+ Style/HashTransformValues:
25
+ Enabled: true
26
+
27
+ Lint/RaiseException:
28
+ Enabled: true
29
+
30
+ Lint/StructNewOverride:
31
+ Enabled: true
32
+
18
33
  Layout/LineLength:
19
34
  Max: 145
20
35
  IgnoredPatterns: ['(\A|\s)#']
21
36
 
37
+
22
38
  Metrics/ModuleLength:
23
39
  Max: 200
24
40
 
@@ -1,3 +1,7 @@
1
+ # 0.18.5
2
+ - Add functions for calculation of cosine similarity and distance to [Rumale::PairwiseMetric](https://yoshoku.github.io/rumale/doc/Rumale/PairwiseMetric.html).
3
+ - Refactor some codes with Rubocop.
4
+
1
5
  # 0.18.4
2
6
  - Add transformer class for [KernelFDA](https://yoshoku.github.io/rumale/doc/Rumale/KernelMachine/KernelFDA.html).
3
7
  - Refactor [KernelPCA](https://yoshoku.github.io/rumale/doc/Rumale/KernelMachine/KernelPCA.html).
@@ -134,6 +134,7 @@ module Rumale
134
134
  res
135
135
  end
136
136
 
137
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
137
138
  def condense_tree(hierarchy, min_cluster_size)
138
139
  n_edges = hierarchy.size
139
140
  root = 2 * n_edges
@@ -262,6 +263,7 @@ module Rumale
262
263
  end
263
264
  res
264
265
  end
266
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
265
267
  end
266
268
  end
267
269
  end
@@ -125,12 +125,10 @@ module Rumale
125
125
  n_outputs = @estimators.first.is_a?(Array) ? @estimators.size : 1
126
126
  if n_outputs > 1
127
127
  multivar_predict(x)
128
+ elsif enable_parallel?
129
+ parallel_map(@params[:n_estimators]) { |n| @estimators[n].predict(x) }.reduce(&:+) + @base_predictions
128
130
  else
129
- if enable_parallel?
130
- parallel_map(@params[:n_estimators]) { |n| @estimators[n].predict(x) }.reduce(&:+) + @base_predictions
131
- else
132
- @estimators.map { |tree| tree.predict(x) }.reduce(&:+) + @base_predictions
133
- end
131
+ @estimators.map { |tree| tree.predict(x) }.reduce(&:+) + @base_predictions
134
132
  end
135
133
  end
136
134
 
@@ -42,6 +42,8 @@ module Rumale
42
42
  conf_mat
43
43
  end
44
44
 
45
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
46
+
45
47
  # Output a summary of classification performance for each class.
46
48
  #
47
49
  # @example
@@ -69,7 +71,8 @@ module Rumale
69
71
  y_true = Rumale::Validation.check_convert_label_array(y_true)
70
72
  y_pred = Rumale::Validation.check_convert_label_array(y_pred)
71
73
  # calculate each evaluation measure.
72
- supports = y_true.bincount
74
+ classes = y_true.to_a.uniq.sort
75
+ supports = Numo::Int32.asarray(classes.map { |label| y_true.eq(label).count })
73
76
  precisions = Rumale::EvaluationMeasure::PrecisionRecall.precision_each_class(y_true, y_pred)
74
77
  recalls = Rumale::EvaluationMeasure::PrecisionRecall.recall_each_class(y_true, y_pred)
75
78
  fscores = Rumale::EvaluationMeasure::PrecisionRecall.f_score_each_class(y_true, y_pred)
@@ -83,7 +86,7 @@ module Rumale
83
86
  weighted_recall = (Numo::DFloat.cast(recalls) * weights).sum
84
87
  weighted_fscore = (Numo::DFloat.cast(fscores) * weights).sum
85
88
  # output reults.
86
- target_name ||= y_true.to_a.uniq.sort.map(&:to_s)
89
+ target_name ||= classes.map(&:to_s)
87
90
  if output_hash
88
91
  res = {}
89
92
  target_name.each_with_index do |label, n|
@@ -107,9 +110,8 @@ module Rumale
107
110
  fscore: weighted_fscore,
108
111
  support: sum_supports
109
112
  }
110
- res
111
113
  else
112
- width = ['weighted avg'.size, target_name.map(&:size).max].max
114
+ width = [12, target_name.map(&:size).max].max # 12 is 'weighted avg'.size
113
115
  res = +''
114
116
  res << "#{' ' * width} precision recall f1-score support\n"
115
117
  res << "\n"
@@ -136,8 +138,9 @@ module Rumale
136
138
  fscore_str = format('%#10s', format('%.2f', weighted_fscore))
137
139
  res << format("%##{width}s ", 'weighted avg')
138
140
  res << "#{precision_str}#{recall_str}#{fscore_str}#{supports_str}\n"
139
- res
140
141
  end
142
+ res
141
143
  end
144
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
142
145
  end
143
146
  end
@@ -81,7 +81,7 @@ module Rumale
81
81
  within_mat = centered_kernel_mat.dot(centered_kernel_mat.transpose) + @params[:reg_param] * Numo::DFloat.eye(n_samples)
82
82
 
83
83
  # calculate projection matrix.
84
- eig_vals, eig_vecs = Numo::Linalg.eigh(
84
+ _, eig_vecs = Numo::Linalg.eigh(
85
85
  between_mat, within_mat,
86
86
  vals_range: (n_samples - n_components)...n_samples
87
87
  )
@@ -84,14 +84,12 @@ module Rumale
84
84
  else
85
85
  node
86
86
  end
87
+ elsif dist + tau >= node.threshold
88
+ node.right.n_samples < k ? node : search(q, node.right, k, tau)
89
+ elsif dist - tau <= node.threshold
90
+ node.left.n_samples < k ? node : search(q, node.left, k, tau)
87
91
  else
88
- if dist + tau >= node.threshold
89
- node.right.n_samples < k ? node : search(q, node.right, k, tau)
90
- elsif dist - tau <= node.threshold
91
- node.left.n_samples < k ? node : search(q, node.left, k, tau)
92
- else
93
- node
94
- end
92
+ node
95
93
  end
96
94
  # :nocov:
97
95
  end
@@ -54,6 +54,39 @@ module Rumale
54
54
  err_mat.class.maximum(err_mat, 0)
55
55
  end
56
56
 
57
+ # Calculate the pairwise cosine simlarities between x and y.
58
+ #
59
+ # @param x [Numo::DFloat] (shape: [n_samples_x, n_features])
60
+ # @param y [Numo::DFloat] (shape: [n_samples_y, n_features])
61
+ # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)
62
+ def cosine_similarity(x, y = nil)
63
+ y_not_given = y.nil?
64
+ x = Rumale::Validation.check_convert_sample_array(x)
65
+ y = Rumale::Validation.check_convert_sample_array(y) unless y_not_given
66
+ x_norm = Numo::NMath.sqrt((x**2).sum(1))
67
+ x_norm[x_norm.eq(0)] = 1
68
+ x /= x_norm.expand_dims(1)
69
+ if y_not_given
70
+ x.dot(x.transpose)
71
+ else
72
+ y_norm = Numo::NMath.sqrt((y**2).sum(1))
73
+ y_norm[y_norm.eq(0)] = 1
74
+ y /= y_norm.expand_dims(1)
75
+ x.dot(y.transpose)
76
+ end
77
+ end
78
+
79
+ # Calculate the pairwise cosine distances between x and y.
80
+ #
81
+ # @param x [Numo::DFloat] (shape: [n_samples_x, n_features])
82
+ # @param y [Numo::DFloat] (shape: [n_samples_y, n_features])
83
+ # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)
84
+ def cosine_distance(x, y = nil)
85
+ dist_mat = 1 - cosine_similarity(x, y)
86
+ dist_mat[dist_mat.diag_indices] = 0 if y.nil?
87
+ dist_mat.clip(0, 2)
88
+ end
89
+
57
90
  # Calculate the rbf kernel between x and y.
58
91
  #
59
92
  # @param x [Numo::DFloat] (shape: [n_samples_x, n_features])
@@ -3,5 +3,5 @@
3
3
  # Rumale is a machine learning library in Ruby.
4
4
  module Rumale
5
5
  # The version of Rumale you are using.
6
- VERSION = '0.18.4'
6
+ VERSION = '0.18.5'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rumale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.4
4
+ version: 0.18.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-11 00:00:00.000000000 Z
11
+ date: 2020-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -180,8 +180,6 @@ files:
180
180
  - LICENSE.txt
181
181
  - README.md
182
182
  - Rakefile
183
- - bin/console
184
- - bin/setup
185
183
  - ext/rumale/extconf.rb
186
184
  - ext/rumale/rumale.c
187
185
  - ext/rumale/rumale.h
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'rumale'
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require 'pry'
11
- # Pry.start
12
-
13
- require 'irb'
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here