在 pyspark 中保存和加载两个 ML 模型
Save and load two ML models in pyspark
首先,我创建了两个 ML 算法并将它们保存到两个单独的文件中。请注意,这两个模型都基于相同的数据框。 feature_1
和 feature_2
是从同一数据集中提取的不同特征集。
import sys
from pyspark.ml.classification import RandomForestClassifier
trainer_1 = RandomForestClassifier(featuresCol="features_1")
trainer_2 = RandomForestClassifier(featuresCol="features_2")
model_1 = trainer_1.fit(df_training_data)
model_2 = trainer_2.fit(df_training_data)
model_1.save(sys.argv[1])
model_2.save(sys.argv[2])
然后,当我以后想使用这些模型时,我必须从它们各自的路径加载它们,提供路径 f.ex。通过 sys.argv.
import sys
from pyspark.ml.classification import RandomForestClassificationModel
model_1 = RandomForestClassificationModel.load(sys.argv[1])
model_2 = RandomForestClassificationModel.load(sys.argv[2])
我想要的是一种优雅的方式,能够将这两个模型作为一个模型保存在同一路径中。我主要希望这样做,以便用户不必在每次保存和加载时都跟踪两个单独的路径名。这两个模型紧密相连,通常总是一起创建和使用,因此它们是一个模型。
这是管道的用途吗?
我想出了一种方法,只需将它们放在一个文件夹中即可。然后用户只需要提供并知道这个文件夹的路径。
import sys
import os
from pyspark.ml.classification import RandomForestClassifier
trainer_1 = RandomForestClassifier(featuresCol="features_1")
trainer_2 = RandomForestClassifier(featuresCol="features_2")
model_1 = trainer_1.fit(df_training_data)
model_2 = trainer_2.fit(df_training_data)
path = 'model_rfc'
os.mkdir(path)
model_1.save(os.path.join(sys.argv[1], 'model_1'))
model_2.save(os.path.join(sys.argv[1], 'model_2'))
名称 model_1
和 model_2
是硬编码的,用户不需要知道。
import sys
import os
from pyspark.ml.classification import RandomForestClassificationModel
model_1 = RandomForestClassificationModel.load(os.path.join(sys.argv[1], 'model_1'))
model_2 = RandomForestClassificationModel.load(os.path.join(sys.argv[1], 'model_2'))
这应该可以解决问题。这是最好的方法吗?还是有更好的方法使用 Spark 库中的功能将模型捆绑在一起?
首先,我创建了两个 ML 算法并将它们保存到两个单独的文件中。请注意,这两个模型都基于相同的数据框。 feature_1
和 feature_2
是从同一数据集中提取的不同特征集。
import sys
from pyspark.ml.classification import RandomForestClassifier
trainer_1 = RandomForestClassifier(featuresCol="features_1")
trainer_2 = RandomForestClassifier(featuresCol="features_2")
model_1 = trainer_1.fit(df_training_data)
model_2 = trainer_2.fit(df_training_data)
model_1.save(sys.argv[1])
model_2.save(sys.argv[2])
然后,当我以后想使用这些模型时,我必须从它们各自的路径加载它们,提供路径 f.ex。通过 sys.argv.
import sys
from pyspark.ml.classification import RandomForestClassificationModel
model_1 = RandomForestClassificationModel.load(sys.argv[1])
model_2 = RandomForestClassificationModel.load(sys.argv[2])
我想要的是一种优雅的方式,能够将这两个模型作为一个模型保存在同一路径中。我主要希望这样做,以便用户不必在每次保存和加载时都跟踪两个单独的路径名。这两个模型紧密相连,通常总是一起创建和使用,因此它们是一个模型。
这是管道的用途吗?
我想出了一种方法,只需将它们放在一个文件夹中即可。然后用户只需要提供并知道这个文件夹的路径。
import sys
import os
from pyspark.ml.classification import RandomForestClassifier
trainer_1 = RandomForestClassifier(featuresCol="features_1")
trainer_2 = RandomForestClassifier(featuresCol="features_2")
model_1 = trainer_1.fit(df_training_data)
model_2 = trainer_2.fit(df_training_data)
path = 'model_rfc'
os.mkdir(path)
model_1.save(os.path.join(sys.argv[1], 'model_1'))
model_2.save(os.path.join(sys.argv[1], 'model_2'))
名称 model_1
和 model_2
是硬编码的,用户不需要知道。
import sys
import os
from pyspark.ml.classification import RandomForestClassificationModel
model_1 = RandomForestClassificationModel.load(os.path.join(sys.argv[1], 'model_1'))
model_2 = RandomForestClassificationModel.load(os.path.join(sys.argv[1], 'model_2'))
这应该可以解决问题。这是最好的方法吗?还是有更好的方法使用 Spark 库中的功能将模型捆绑在一起?