spark-connect 0.4.0 → 0.5.0
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 +4 -4
- data/CHANGELOG.md +39 -1
- data/README.md +5 -3
- data/lib/spark_connect/arrow.rb +5 -1
- data/lib/spark_connect/client.rb +3 -1
- data/lib/spark_connect/data_frame.rb +2 -1
- data/lib/spark_connect/ml/base.rb +433 -0
- data/lib/spark_connect/ml/classification.rb +444 -0
- data/lib/spark_connect/ml/clustering.rb +296 -0
- data/lib/spark_connect/ml/evaluation.rb +120 -0
- data/lib/spark_connect/ml/feature.rb +563 -0
- data/lib/spark_connect/ml/feature_transformers.rb +200 -0
- data/lib/spark_connect/ml/fpm.rb +58 -0
- data/lib/spark_connect/ml/functions.rb +47 -0
- data/lib/spark_connect/ml/linalg.rb +336 -0
- data/lib/spark_connect/ml/one_vs_rest.rb +157 -0
- data/lib/spark_connect/ml/params.rb +378 -0
- data/lib/spark_connect/ml/persistence.rb +247 -0
- data/lib/spark_connect/ml/pipeline.rb +115 -0
- data/lib/spark_connect/ml/recommendation.rb +56 -0
- data/lib/spark_connect/ml/regression.rb +289 -0
- data/lib/spark_connect/ml/rpc.rb +244 -0
- data/lib/spark_connect/ml/serialize.rb +211 -0
- data/lib/spark_connect/ml/stat.rb +127 -0
- data/lib/spark_connect/ml/tuning.rb +501 -0
- data/lib/spark_connect/ml.rb +62 -0
- data/lib/spark_connect/version.rb +1 -1
- data/lib/spark_connect.rb +1 -0
- metadata +22 -2
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SparkConnect
|
|
4
|
+
module ML
|
|
5
|
+
# Clustering operators (`org.apache.spark.ml.clustering`).
|
|
6
|
+
#
|
|
7
|
+
# Defaults come from the Scala `setDefault` calls and `Has*` traits of Spark 4.3.0.
|
|
8
|
+
# The `seed` defaults are `this.getClass.getName.hashCode` of the Scala class.
|
|
9
|
+
module Clustering
|
|
10
|
+
# Summary shared by every clustering model (`ClusteringSummary`).
|
|
11
|
+
class ClusteringSummary < Summary
|
|
12
|
+
attribute :predictions, :dataframe
|
|
13
|
+
attribute :prediction_col, :string, spark_name: "predictionCol"
|
|
14
|
+
attribute :features_col, :string, spark_name: "featuresCol"
|
|
15
|
+
attribute :k, :int
|
|
16
|
+
attribute :num_iter, :int, spark_name: "numIter"
|
|
17
|
+
attribute :cluster, :dataframe
|
|
18
|
+
attribute :cluster_sizes, :any, spark_name: "clusterSizes"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Training summary of a {KMeansModel}.
|
|
22
|
+
class KMeansSummary < ClusteringSummary
|
|
23
|
+
attribute :training_cost, :double, spark_name: "trainingCost"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Training summary of a {BisectingKMeansModel}.
|
|
27
|
+
class BisectingKMeansSummary < ClusteringSummary
|
|
28
|
+
attribute :training_cost, :double, spark_name: "trainingCost"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Training summary of a {GaussianMixtureModel}.
|
|
32
|
+
class GaussianMixtureSummary < ClusteringSummary
|
|
33
|
+
attribute :probability, :dataframe
|
|
34
|
+
attribute :probability_col, :string, spark_name: "probabilityCol"
|
|
35
|
+
attribute :log_likelihood, :double, spark_name: "logLikelihood"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# K-means clustering.
|
|
39
|
+
class KMeans < Estimator
|
|
40
|
+
operator "org.apache.spark.ml.clustering.KMeans"
|
|
41
|
+
model -> { KMeansModel }
|
|
42
|
+
|
|
43
|
+
param :k, :int, default: 2, doc: "the number of clusters to create (> 1)"
|
|
44
|
+
param :init_mode, :string, default: "k-means||", doc: "initialization algorithm: random or k-means||"
|
|
45
|
+
param :init_steps, :int, default: 2, doc: "number of steps for k-means|| initialization (> 0)"
|
|
46
|
+
param :max_iter, :int, default: 20, doc: "maximum number of iterations (>= 0)"
|
|
47
|
+
param :tol, :double, default: 1e-4, doc: "convergence tolerance for iterative algorithms (>= 0)"
|
|
48
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed"
|
|
49
|
+
param :distance_measure, :string, default: "euclidean", doc: "distance measure: euclidean or cosine"
|
|
50
|
+
param :weight_col, :string, doc: "weight column name; unset means every instance has weight 1.0"
|
|
51
|
+
param :solver, :string, default: "auto", doc: "the solver algorithm for optimization"
|
|
52
|
+
param :max_block_size_in_mb, :double, default: 0.0, spark_name: "maxBlockSizeInMB",
|
|
53
|
+
doc: "maximum memory in MB for stacking input data into blocks"
|
|
54
|
+
param :intermediate_storage_level, :string, default: "MEMORY_AND_DISK",
|
|
55
|
+
doc: "StorageLevel for intermediate datasets"
|
|
56
|
+
param :features_col, :string, default: "features", doc: "features column name"
|
|
57
|
+
param :prediction_col, :string, default: "prediction", doc: "prediction column name"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# A fitted {KMeans}: the cluster centers and the nearest-center prediction.
|
|
61
|
+
class KMeansModel < Model
|
|
62
|
+
operator "org.apache.spark.ml.clustering.KMeansModel"
|
|
63
|
+
summary_class -> { KMeansSummary }
|
|
64
|
+
|
|
65
|
+
param :k, :int, default: 2, doc: "the number of clusters to create (> 1)"
|
|
66
|
+
param :init_mode, :string, default: "k-means||", doc: "initialization algorithm: random or k-means||"
|
|
67
|
+
param :init_steps, :int, default: 2, doc: "number of steps for k-means|| initialization (> 0)"
|
|
68
|
+
param :max_iter, :int, default: 20, doc: "maximum number of iterations (>= 0)"
|
|
69
|
+
param :tol, :double, default: 1e-4, doc: "convergence tolerance for iterative algorithms (>= 0)"
|
|
70
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed"
|
|
71
|
+
param :distance_measure, :string, default: "euclidean", doc: "distance measure: euclidean or cosine"
|
|
72
|
+
param :weight_col, :string, doc: "weight column name; unset means every instance has weight 1.0"
|
|
73
|
+
param :solver, :string, default: "auto", doc: "the solver algorithm for optimization"
|
|
74
|
+
param :max_block_size_in_mb, :double, default: 0.0, spark_name: "maxBlockSizeInMB",
|
|
75
|
+
doc: "maximum memory in MB for stacking input data into blocks"
|
|
76
|
+
param :intermediate_storage_level, :string, default: "MEMORY_AND_DISK",
|
|
77
|
+
doc: "StorageLevel for intermediate datasets"
|
|
78
|
+
param :features_col, :string, default: "features", doc: "features column name"
|
|
79
|
+
param :prediction_col, :string, default: "prediction", doc: "prediction column name"
|
|
80
|
+
|
|
81
|
+
attribute :cluster_center_matrix, :matrix, spark_name: "clusterCenterMatrix"
|
|
82
|
+
attribute :num_features, :int, spark_name: "numFeatures"
|
|
83
|
+
method_call :predict, arg_count: 1, returns: :int
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Bisecting (divisive hierarchical) k-means clustering.
|
|
87
|
+
class BisectingKMeans < Estimator
|
|
88
|
+
operator "org.apache.spark.ml.clustering.BisectingKMeans"
|
|
89
|
+
model -> { BisectingKMeansModel }
|
|
90
|
+
|
|
91
|
+
param :k, :int, default: 4, doc: "the desired number of leaf clusters (> 1)"
|
|
92
|
+
param :min_divisible_cluster_size, :double, default: 1.0,
|
|
93
|
+
doc: "minimum divisible cluster size (> 0)"
|
|
94
|
+
param :max_iter, :int, default: 20, doc: "maximum number of iterations (>= 0)"
|
|
95
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed"
|
|
96
|
+
param :distance_measure, :string, default: "euclidean", doc: "distance measure: euclidean or cosine"
|
|
97
|
+
param :weight_col, :string, doc: "weight column name; unset means every instance has weight 1.0"
|
|
98
|
+
param :features_col, :string, default: "features", doc: "features column name"
|
|
99
|
+
param :prediction_col, :string, default: "prediction", doc: "prediction column name"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# A fitted {BisectingKMeans}.
|
|
103
|
+
class BisectingKMeansModel < Model
|
|
104
|
+
operator "org.apache.spark.ml.clustering.BisectingKMeansModel"
|
|
105
|
+
summary_class -> { BisectingKMeansSummary }
|
|
106
|
+
|
|
107
|
+
param :k, :int, default: 4, doc: "the desired number of leaf clusters (> 1)"
|
|
108
|
+
param :min_divisible_cluster_size, :double, default: 1.0,
|
|
109
|
+
doc: "minimum divisible cluster size (> 0)"
|
|
110
|
+
param :max_iter, :int, default: 20, doc: "maximum number of iterations (>= 0)"
|
|
111
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed"
|
|
112
|
+
param :distance_measure, :string, default: "euclidean", doc: "distance measure: euclidean or cosine"
|
|
113
|
+
param :weight_col, :string, doc: "weight column name; unset means every instance has weight 1.0"
|
|
114
|
+
param :features_col, :string, default: "features", doc: "features column name"
|
|
115
|
+
param :prediction_col, :string, default: "prediction", doc: "prediction column name"
|
|
116
|
+
|
|
117
|
+
attribute :cluster_center_matrix, :matrix, spark_name: "clusterCenterMatrix"
|
|
118
|
+
attribute :num_features, :int, spark_name: "numFeatures"
|
|
119
|
+
method_call :predict, arg_count: 1, returns: :int
|
|
120
|
+
method_call :compute_cost, arg_count: 1, returns: :double, spark_name: "computeCost"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Gaussian mixture model fitted by expectation-maximization.
|
|
124
|
+
class GaussianMixture < Estimator
|
|
125
|
+
operator "org.apache.spark.ml.clustering.GaussianMixture"
|
|
126
|
+
model -> { GaussianMixtureModel }
|
|
127
|
+
|
|
128
|
+
param :k, :int, default: 2, doc: "number of independent Gaussians in the mixture model"
|
|
129
|
+
param :max_iter, :int, default: 100, doc: "maximum number of iterations (>= 0)"
|
|
130
|
+
param :tol, :double, default: 0.01, doc: "convergence tolerance for iterative algorithms (>= 0)"
|
|
131
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed"
|
|
132
|
+
param :aggregation_depth, :int, default: 2, doc: "suggested depth for treeAggregate (>= 2)"
|
|
133
|
+
param :weight_col, :string, doc: "weight column name; unset means every instance has weight 1.0"
|
|
134
|
+
param :features_col, :string, default: "features", doc: "features column name"
|
|
135
|
+
param :prediction_col, :string, default: "prediction", doc: "prediction column name"
|
|
136
|
+
param :probability_col, :string, default: "probability", doc: "predicted class conditional probabilities column"
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# A fitted {GaussianMixture}.
|
|
140
|
+
class GaussianMixtureModel < Model
|
|
141
|
+
operator "org.apache.spark.ml.clustering.GaussianMixtureModel"
|
|
142
|
+
summary_class -> { GaussianMixtureSummary }
|
|
143
|
+
|
|
144
|
+
param :k, :int, default: 2, doc: "number of independent Gaussians in the mixture model"
|
|
145
|
+
param :max_iter, :int, default: 100, doc: "maximum number of iterations (>= 0)"
|
|
146
|
+
param :tol, :double, default: 0.01, doc: "convergence tolerance for iterative algorithms (>= 0)"
|
|
147
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed"
|
|
148
|
+
param :aggregation_depth, :int, default: 2, doc: "suggested depth for treeAggregate (>= 2)"
|
|
149
|
+
param :weight_col, :string, doc: "weight column name; unset means every instance has weight 1.0"
|
|
150
|
+
param :features_col, :string, default: "features", doc: "features column name"
|
|
151
|
+
param :prediction_col, :string, default: "prediction", doc: "prediction column name"
|
|
152
|
+
param :probability_col, :string, default: "probability", doc: "predicted class conditional probabilities column"
|
|
153
|
+
|
|
154
|
+
attribute :weights, :double_array
|
|
155
|
+
attribute :num_features, :int, spark_name: "numFeatures"
|
|
156
|
+
attribute :gaussians_df, :dataframe, spark_name: "gaussiansDF"
|
|
157
|
+
method_call :predict, arg_count: 1, returns: :int
|
|
158
|
+
method_call :predict_probability, arg_count: 1, returns: :vector, spark_name: "predictProbability"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Latent Dirichlet allocation. The fitted class depends on `optimizer`: see {LDA#model_class}.
|
|
162
|
+
class LDA < Estimator
|
|
163
|
+
operator "org.apache.spark.ml.clustering.LDA"
|
|
164
|
+
model -> { LocalLDAModel }
|
|
165
|
+
|
|
166
|
+
param :k, :int, default: 10, doc: "the number of topics (clusters) to infer (> 1)"
|
|
167
|
+
param :max_iter, :int, default: 20, doc: "maximum number of iterations (>= 0)"
|
|
168
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed"
|
|
169
|
+
param :checkpoint_interval, :int, default: 10,
|
|
170
|
+
doc: "checkpoint interval (>= 1), or -1 to disable checkpointing"
|
|
171
|
+
param :doc_concentration, :double_array, doc: "concentration parameter for the prior over documents"
|
|
172
|
+
param :topic_concentration, :double, doc: "concentration parameter for the prior over topics"
|
|
173
|
+
param :optimizer, :string, default: "online", doc: "optimizer: online or em"
|
|
174
|
+
param :learning_offset, :double, default: 1024.0, doc: "online optimizer: learning offset (> 0)"
|
|
175
|
+
param :learning_decay, :double, default: 0.51, doc: "online optimizer: learning decay in (0.5, 1.0]"
|
|
176
|
+
param :subsampling_rate, :double, default: 0.05, doc: "online optimizer: mini-batch fraction in (0, 1]"
|
|
177
|
+
param :optimize_doc_concentration, :boolean, default: true,
|
|
178
|
+
doc: "online optimizer: whether to optimize doc concentration"
|
|
179
|
+
param :keep_last_checkpoint, :boolean, default: true, doc: "whether to keep the last checkpoint"
|
|
180
|
+
param :topic_distribution_col, :string, default: "topicDistribution",
|
|
181
|
+
doc: "output column with the topic distribution of each document"
|
|
182
|
+
param :features_col, :string, default: "features", doc: "features column name"
|
|
183
|
+
|
|
184
|
+
# The fitted model: {DistributedLDAModel} for the `em` optimizer, {LocalLDAModel} otherwise.
|
|
185
|
+
# @return [Class<LDAModel>]
|
|
186
|
+
def model_class
|
|
187
|
+
optimizer == "em" ? DistributedLDAModel : LocalLDAModel
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Ruby base of the LDA models (Spark's abstract `LDAModel`). Its members apply to both subclasses.
|
|
192
|
+
class LDAModel < Model
|
|
193
|
+
param :k, :int, default: 10, doc: "the number of topics (clusters) to infer (> 1)"
|
|
194
|
+
param :max_iter, :int, default: 20, doc: "maximum number of iterations (>= 0)"
|
|
195
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed (the default is set by each concrete model class)"
|
|
196
|
+
param :checkpoint_interval, :int, default: 10,
|
|
197
|
+
doc: "checkpoint interval (>= 1), or -1 to disable checkpointing"
|
|
198
|
+
param :doc_concentration, :double_array, doc: "concentration parameter for the prior over documents"
|
|
199
|
+
param :topic_concentration, :double, doc: "concentration parameter for the prior over topics"
|
|
200
|
+
param :optimizer, :string, default: "online", doc: "optimizer: online or em"
|
|
201
|
+
param :learning_offset, :double, default: 1024.0, doc: "online optimizer: learning offset (> 0)"
|
|
202
|
+
param :learning_decay, :double, default: 0.51, doc: "online optimizer: learning decay in (0.5, 1.0]"
|
|
203
|
+
param :subsampling_rate, :double, default: 0.05, doc: "online optimizer: mini-batch fraction in (0, 1]"
|
|
204
|
+
param :optimize_doc_concentration, :boolean, default: true,
|
|
205
|
+
doc: "online optimizer: whether to optimize doc concentration"
|
|
206
|
+
param :keep_last_checkpoint, :boolean, default: true, doc: "whether to keep the last checkpoint"
|
|
207
|
+
param :topic_distribution_col, :string, default: "topicDistribution",
|
|
208
|
+
doc: "output column with the topic distribution of each document"
|
|
209
|
+
param :features_col, :string, default: "features", doc: "features column name"
|
|
210
|
+
|
|
211
|
+
attribute :estimated_doc_concentration, :vector, spark_name: "estimatedDocConcentration"
|
|
212
|
+
attribute :topics_matrix, :matrix, spark_name: "topicsMatrix"
|
|
213
|
+
attribute :is_distributed, :boolean, spark_name: "isDistributed"
|
|
214
|
+
attribute :vocab_size, :int, spark_name: "vocabSize"
|
|
215
|
+
method_call :log_likelihood, arg_count: 1, returns: :double, spark_name: "logLikelihood"
|
|
216
|
+
method_call :log_perplexity, arg_count: 1, returns: :double, spark_name: "logPerplexity"
|
|
217
|
+
|
|
218
|
+
# The `topics` of the model as a DataFrame: `describeTopics(maxTermsPerTopic)`
|
|
219
|
+
# for a limit, or Spark's no-argument overload (ten terms per topic) without one.
|
|
220
|
+
#
|
|
221
|
+
# @param max_terms_per_topic [Integer, nil]
|
|
222
|
+
# @return [DataFrame]
|
|
223
|
+
def describe_topics(max_terms_per_topic = nil)
|
|
224
|
+
args = max_terms_per_topic.nil? ? [] : [max_terms_per_topic]
|
|
225
|
+
AttributeSupport.invoke(self, [AttributeSupport.method_proto("describeTopics", args)], :dataframe)
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# An LDA model fitted with the `em` optimizer (the server keeps the distributed model).
|
|
230
|
+
class DistributedLDAModel < LDAModel
|
|
231
|
+
operator "org.apache.spark.ml.clustering.DistributedLDAModel"
|
|
232
|
+
|
|
233
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed"
|
|
234
|
+
|
|
235
|
+
attribute :training_log_likelihood, :double, spark_name: "trainingLogLikelihood"
|
|
236
|
+
attribute :log_prior, :double, spark_name: "logPrior"
|
|
237
|
+
attribute :checkpoint_files, :string_array, spark_name: "getCheckpointFiles"
|
|
238
|
+
|
|
239
|
+
# The same topics as a local model (`toLocal`), with this model's params.
|
|
240
|
+
#
|
|
241
|
+
# @return [LocalLDAModel]
|
|
242
|
+
def to_local
|
|
243
|
+
info = Rpc.fetch(session, ref_id, [AttributeSupport.method_proto("toLocal", [])])
|
|
244
|
+
local = LocalLDAModel.new(uid: uid).copy_values_from(self)
|
|
245
|
+
local.attach(session, info[:obj_ref])
|
|
246
|
+
local
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# An LDA model fitted with the `online` optimizer (the default).
|
|
251
|
+
class LocalLDAModel < LDAModel
|
|
252
|
+
operator "org.apache.spark.ml.clustering.LocalLDAModel"
|
|
253
|
+
|
|
254
|
+
param :seed, :long, default: SEED_DEFAULT, doc: "random seed"
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Power iteration clustering over an affinity graph. Spark's `PowerIterationClusteringWrapper`
|
|
258
|
+
# has no transform: use {#assign_clusters}.
|
|
259
|
+
class PowerIterationClustering < Transformer
|
|
260
|
+
operator "org.apache.spark.ml.clustering.PowerIterationClusteringWrapper"
|
|
261
|
+
|
|
262
|
+
param :k, :int, default: 2, doc: "the number of clusters to create (> 1)"
|
|
263
|
+
param :max_iter, :int, default: 20, doc: "maximum number of iterations (>= 0)"
|
|
264
|
+
param :init_mode, :string, default: "random", doc: "initialization algorithm: random or degree"
|
|
265
|
+
param :src_col, :string, default: "src", doc: "name of the input column for source vertex ids"
|
|
266
|
+
param :dst_col, :string, default: "dst", doc: "name of the input column for destination vertex ids"
|
|
267
|
+
param :weight_col, :string, doc: "weight column name; unset means every edge has weight 1.0"
|
|
268
|
+
|
|
269
|
+
# Cluster the vertices of an affinity graph given as `src`, `dst` and weight columns.
|
|
270
|
+
#
|
|
271
|
+
# @param dataset [DataFrame]
|
|
272
|
+
# @return [DataFrame] one row per vertex with columns `id` (long) and `cluster` (int)
|
|
273
|
+
def assign_clusters(dataset)
|
|
274
|
+
weight = is_defined?(:weight_col) ? weight_col : ""
|
|
275
|
+
args = [dataset, k, max_iter, init_mode, src_col, dst_col, weight]
|
|
276
|
+
helper_frame(dataset, "powerIterationClusteringAssignClusters", args)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# @raise [NotImplementedError] always; use {#assign_clusters}.
|
|
280
|
+
def transform(*)
|
|
281
|
+
raise SparkConnect::NotImplementedError, "PowerIterationClustering has no transform; use assign_clusters"
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
private
|
|
285
|
+
|
|
286
|
+
# A DataFrame produced by a method of the ML connect helper (no model reference).
|
|
287
|
+
def helper_frame(dataset, method_name, args)
|
|
288
|
+
session = dataset.session
|
|
289
|
+
methods = [AttributeSupport.method_proto(method_name, args)]
|
|
290
|
+
fetch = Proto::Fetch.new(obj_ref: Proto::ObjectRef.new(id: Rpc::HELPER_ID), methods: methods)
|
|
291
|
+
DataFrame.new(session, PlanBuilder.relation(session, ml_relation: Proto::MlRelation.new(fetch: fetch)))
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SparkConnect
|
|
4
|
+
module ML
|
|
5
|
+
# Evaluators (`org.apache.spark.ml.evaluation`). Each computes a metric over
|
|
6
|
+
# a predictions DataFrame with {Evaluator#evaluate}. Defaults are Spark
|
|
7
|
+
# 4.3.0's `setDefault` values; `larger_better?` mirrors Spark's `isLargerBetter`.
|
|
8
|
+
module Evaluation
|
|
9
|
+
# Regression metrics: rmse (default), mse, r2, mae or var.
|
|
10
|
+
class RegressionEvaluator < Evaluator
|
|
11
|
+
operator "org.apache.spark.ml.evaluation.RegressionEvaluator"
|
|
12
|
+
|
|
13
|
+
param :prediction_col, :string, default: "prediction", spark_name: "predictionCol",
|
|
14
|
+
doc: "prediction column name"
|
|
15
|
+
param :label_col, :string, default: "label", spark_name: "labelCol", doc: "label column name"
|
|
16
|
+
param :metric_name, :string, default: "rmse", spark_name: "metricName",
|
|
17
|
+
doc: "metric name in evaluation: mse, rmse, r2, mae or var"
|
|
18
|
+
param :weight_col, :string, spark_name: "weightCol",
|
|
19
|
+
doc: "weight column name; unset means every instance has weight 1.0"
|
|
20
|
+
param :through_origin, :boolean, default: false, spark_name: "throughOrigin",
|
|
21
|
+
doc: "whether the regression is through the origin (r2 only)"
|
|
22
|
+
|
|
23
|
+
# Only r2 and var are larger-is-better.
|
|
24
|
+
def larger_better? = %w[r2 var].include?(get(:metric_name))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Binary classification metrics: areaUnderROC (default) or areaUnderPR.
|
|
28
|
+
class BinaryClassificationEvaluator < Evaluator
|
|
29
|
+
operator "org.apache.spark.ml.evaluation.BinaryClassificationEvaluator"
|
|
30
|
+
|
|
31
|
+
param :raw_prediction_col, :string, default: "rawPrediction", spark_name: "rawPredictionCol",
|
|
32
|
+
doc: "raw prediction (confidence) column name"
|
|
33
|
+
param :label_col, :string, default: "label", spark_name: "labelCol", doc: "label column name"
|
|
34
|
+
param :metric_name, :string, default: "areaUnderROC", spark_name: "metricName",
|
|
35
|
+
doc: "metric name in evaluation: areaUnderROC or areaUnderPR"
|
|
36
|
+
param :weight_col, :string, spark_name: "weightCol",
|
|
37
|
+
doc: "weight column name; unset means every instance has weight 1.0"
|
|
38
|
+
param :num_bins, :int, default: 1000, spark_name: "numBins",
|
|
39
|
+
doc: "number of bins to down-sample the ROC and PR curves (>= 0; 0 means no down-sampling)"
|
|
40
|
+
|
|
41
|
+
# Both binary metrics are larger-is-better.
|
|
42
|
+
def larger_better? = true
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Multiclass classification metrics (f1 by default, also accuracy, logLoss, ...).
|
|
46
|
+
class MulticlassClassificationEvaluator < Evaluator
|
|
47
|
+
operator "org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator"
|
|
48
|
+
|
|
49
|
+
param :prediction_col, :string, default: "prediction", spark_name: "predictionCol",
|
|
50
|
+
doc: "prediction column name"
|
|
51
|
+
param :label_col, :string, default: "label", spark_name: "labelCol", doc: "label column name"
|
|
52
|
+
param :metric_name, :string, default: "f1", spark_name: "metricName",
|
|
53
|
+
doc: "metric name in evaluation, e.g. f1, accuracy, weightedPrecision, logLoss"
|
|
54
|
+
param :weight_col, :string, spark_name: "weightCol",
|
|
55
|
+
doc: "weight column name; unset means every instance has weight 1.0"
|
|
56
|
+
param :metric_label, :double, default: 0.0, spark_name: "metricLabel",
|
|
57
|
+
doc: "the class whose metric is computed by the *ByLabel metrics (>= 0)"
|
|
58
|
+
param :beta, :double, default: 1.0, doc: "the beta value used in f-measure metrics (> 0)"
|
|
59
|
+
param :probability_col, :string, default: "probability", spark_name: "probabilityCol",
|
|
60
|
+
doc: "column name for predicted class conditional probabilities"
|
|
61
|
+
param :eps, :double, default: 1e-15, doc: "log-loss clipping epsilon, in (0, 0.5)"
|
|
62
|
+
|
|
63
|
+
# Error-type metrics are smaller-is-better; all others are larger-is-better.
|
|
64
|
+
LOWER_IS_BETTER = %w[weightedFalsePositiveRate falsePositiveRateByLabel logLoss hammingLoss].freeze
|
|
65
|
+
|
|
66
|
+
def larger_better? = !LOWER_IS_BETTER.include?(get(:metric_name))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Multilabel classification metrics (f1Measure by default, also subsetAccuracy, accuracy, hammingLoss, ...).
|
|
70
|
+
class MultilabelClassificationEvaluator < Evaluator
|
|
71
|
+
operator "org.apache.spark.ml.evaluation.MultilabelClassificationEvaluator"
|
|
72
|
+
|
|
73
|
+
param :prediction_col, :string, default: "prediction", spark_name: "predictionCol",
|
|
74
|
+
doc: "prediction column name"
|
|
75
|
+
param :label_col, :string, default: "label", spark_name: "labelCol", doc: "label column name"
|
|
76
|
+
param :metric_name, :string, default: "f1Measure", spark_name: "metricName",
|
|
77
|
+
doc: "metric name in evaluation, e.g. f1Measure, subsetAccuracy, hammingLoss"
|
|
78
|
+
param :metric_label, :double, default: 0.0, spark_name: "metricLabel",
|
|
79
|
+
doc: "the class whose metric is computed by the *ByLabel metrics (>= 0)"
|
|
80
|
+
|
|
81
|
+
# Only hammingLoss is smaller-is-better.
|
|
82
|
+
def larger_better? = get(:metric_name) != "hammingLoss"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Clustering metrics (silhouette, the only one supported, with squaredEuclidean or cosine distance).
|
|
86
|
+
class ClusteringEvaluator < Evaluator
|
|
87
|
+
operator "org.apache.spark.ml.evaluation.ClusteringEvaluator"
|
|
88
|
+
|
|
89
|
+
param :prediction_col, :string, default: "prediction", spark_name: "predictionCol",
|
|
90
|
+
doc: "prediction column name"
|
|
91
|
+
param :features_col, :string, default: "features", spark_name: "featuresCol",
|
|
92
|
+
doc: "features column name"
|
|
93
|
+
param :metric_name, :string, default: "silhouette", spark_name: "metricName", doc: "metric name: silhouette"
|
|
94
|
+
param :distance_measure, :string, default: "squaredEuclidean", spark_name: "distanceMeasure",
|
|
95
|
+
doc: "distance measure: squaredEuclidean or cosine"
|
|
96
|
+
param :weight_col, :string, spark_name: "weightCol",
|
|
97
|
+
doc: "weight column name; unset means every instance has weight 1.0"
|
|
98
|
+
|
|
99
|
+
# Silhouette is larger-is-better.
|
|
100
|
+
def larger_better? = true
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Ranking metrics over arrays of predicted and relevant items (meanAveragePrecision by default).
|
|
104
|
+
class RankingEvaluator < Evaluator
|
|
105
|
+
operator "org.apache.spark.ml.evaluation.RankingEvaluator"
|
|
106
|
+
|
|
107
|
+
param :prediction_col, :string, default: "prediction", spark_name: "predictionCol",
|
|
108
|
+
doc: "prediction column name (array of items, ranked)"
|
|
109
|
+
param :label_col, :string, default: "label", spark_name: "labelCol",
|
|
110
|
+
doc: "label column name (array of relevant items)"
|
|
111
|
+
param :metric_name, :string, default: "meanAveragePrecision", spark_name: "metricName",
|
|
112
|
+
doc: "metric name in evaluation, e.g. meanAveragePrecision, precisionAtK, ndcgAtK"
|
|
113
|
+
param :k, :int, default: 10, doc: "the cutoff used by the @K metrics (> 0)"
|
|
114
|
+
|
|
115
|
+
# Every ranking metric is larger-is-better.
|
|
116
|
+
def larger_better? = true
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|