python - 高斯混合模型 : Difference between Spark MLlib and scikit-learn

标签 python apache-spark scikit-learn pyspark apache-spark-mllib

我正在尝试对数据集样本使用高斯混合模型。 我同时使用了MLlib(与pyspark)和scikit-learn,得到了截然不同的结果,scikit-learn一个看起来更逼真。

from pyspark.mllib.clustering import GaussianMixture as SparkGaussianMixture
from sklearn.mixture import GaussianMixture
from pyspark.mllib.linalg import Vectors

Scikit-learn:

local = pd.DataFrame([ x.asDict() for x in df.sample(0.0001).collect() ])
model1 = GaussianMixture(n_components=3)
model1.fit([ [x] for x in local['field'].tolist() ])

model1.means_
array([[7.56123598e+00],
   [1.32517410e+07],
   [3.96762639e+04]])

model1.covariances_
array([[[6.65177423e+00]],
   [[1.00000000e-06]],
   [[8.38380897e+10]]])

MLLib:

model2 = SparkGaussianMixture.train(
    sc.createDataFrame(local).rdd.map(lambda x: Vectors.dense(x.field)),
    k=3,
    convergenceTol=1e-4,
    maxIterations=100
)

model2.gaussians
[MultivariateGaussian(mu=DenseVector([28736.5113]), sigma=DenseMatrix(1, 1, [1094083795.0001], 0)),
 MultivariateGaussian(mu=DenseVector([7839059.9208]), sigma=DenseMatrix(1, 1, [38775218707109.83], 0)),
 MultivariateGaussian(mu=DenseVector([43.8723]), sigma=DenseMatrix(1, 1, [608204.4711], 0))]

但是,我对通过模型运行整个数据集很感兴趣,我担心这需要并行化(并因此使用 MLlib)才能在有限的时间内获得结果。我做错了什么/遗漏了什么吗?

数据:

完整的数据有一条极长的尾部,看起来像: enter image description here

而数据有一个明显正常的 dist ceneterd 某处更接近 scikit-learn 聚类:

enter image description here

我正在使用 Spark 2.3.0 (AWS EMR)。

编辑:初始化参数:

local = pd.DataFrame([ x.asDict() for x in df.sample(0.0001).collect() ])
model1 = GaussianMixture(n_components=3, init_params='random')
model1.fit([ [x] for x in local['field'].tolist() ])

model1.means_
array([[2.17611913e+04],
   [8.03184505e+06],
   [7.56871801e+00]])

model1.covariances_
rray([[[1.01835902e+09]],
   [[3.98552130e+13]],
   [[6.95161493e+00]]])

最佳答案

这本身不是 python 问题。 IMO,这似乎更像是一个机器学习/数据验证/数据分割问题。话虽这么说,您认为必须并行化您的工作是正确的,但是您以何种方式进行工作很重要。您可以研究模型中的 8 位量化和模型并行性等内容,以帮助您实现目标:在不牺牲数据质量或保真度的情况下,及时地在大型数据集上训练模型。

这是一篇关于量化的博文:https://petewarden.com/2016/05/03/how-to-quantize-neural-networks-with-tensorflow/

这是来自 Tim Dettmers 博客的关于模型并行性和 8 位量化的博文:http://timdettmers.com/2017/04/09/which-gpu-for-deep-learning/

及相关论文:https://arxiv.org/pdf/1511.04561.pdf

尽管您需要记住,根据 GPU 上的 FP 操作,您可能看不到这条路线的实质性好处:https://blog.inten.to/hardware-for-deep-learning-part-3-gpu-8906c1644664

HTH 和 YMMV。

此外,您可能想查看数据折叠,但不记得细节或我此时阅读的论文。我会把它放在这里以记住一旦我这样做了。

关于python - 高斯混合模型 : Difference between Spark MLlib and scikit-learn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50915749/

相关文章:

Python 套接字脚本。如何向特定客户端发送数据

python - Visual Studio Code 使用错误的 Python 版本 - 未显示正确的版本

java - Spark 提交到 Amazon EMR 时如何指定自定义 log4j.configuration 的位置?

apache-spark - Spark 作业服务器的 Spark SQL 作业中的错误 "Invalid job type for this context"

python - sklearn 中的 CountVectorizer 仅包含出现次数高于某个最小次数的单词

python - 评估 Python 中的比较运算符的异常行为

python - 无法理解 Django 中的查询集

python - 获取 TypeError ("StructType can not accept object %r in type %s"% (object, type(obj)))

python - SVM:训练后从头开始生成模型

python - 有效计算python中的词频