4

我对机器学习算法和Spark非常陌生。我跟着 Twitter的数据流语言分类在这里找到:Spark MLlib/K-Means直觉

http://databricks.gitbooks.io/databricks-spark-reference-applications/content/twitter_classifier/README.html

具体验证码:

http://databricks.gitbooks.io/databricks-spark-reference-applications/content/twitter_classifier/scala/src/main/scala/com/databricks/apps/twitter_classifier/ExamineAndTrain.scala

除了我试图在批处理模式下的一些鸣叫它运行拉出了Cassandra的 ,在这种情况下总共有200条推文。

如示例所示,我使用此对象为“向量化”一组鸣叫:

object Utils{ 
    val numFeatures = 1000 
    val tf = new HashingTF(numFeatures) 

    /** 
    * Create feature vectors by turning each tweet into bigrams of 
    * characters (an n-gram model) and then hashing those to a 
    * length-1000 feature vector that we can pass to MLlib. 
    * This is a common way to decrease the number of features in a 
    * model while still getting excellent accuracy (otherwise every 
    * pair of Unicode characters would potentially be a feature). 
    */ 
    def featurize(s: String): Vector = { 
    tf.transform(s.sliding(2).toSeq) 
    } 
} 

这是我的代码是从ExaminAndTrain.scala修改:

val noSets = rawTweets.map(set => set.mkString("\n")) 

val vectors = noSets.map(Utils.featurize).cache() 
vectors.count() 

val numClusters = 5 
val numIterations = 30 

val model = KMeans.train(vectors, numClusters, numIterations) 

    for (i <- 0 until numClusters) { 
    println(s"\nCLUSTER $i") 
    noSets.foreach { 
     t => if (model.predict(Utils.featurize(t)) == 1) { 
      println(t) 
     } 
     } 
    } 

这代码运行,每个群集打印“群集0”,“群集1”等 ,没有任何打印在下面。如果我翻转

models.predict(Utils.featurize(t)) == 1 

models.predict(Utils.featurize(t)) == 0 

同样的事情发生,除了每鸣叫打印每簇之下。

这是我直觉地认为正在发生的事情(请纠正我 想,如果它的错误):此代码打开各推入载体, 随机选取若干个簇,然后运行K均值到组的tweet(在 真的高水平,我认为,集群将是共同的 “主题”)。因此,当它检查每条推文以查看models.predict == 1时,在每个群集下面应该出现不同的推文集(并且 是因为它针对自己检查训练集,每个推文 应该位于群集中)。为什么不这样做?我的 了解什么kmeans做错了,我的训练集太 小或我错过了一步。

任何帮助是极大的赞赏

回答

3

嗯,首先KMEANS的是一个聚类算法,因此不受监督。所以没有“检查自己的训练集”(好吧,你可以手动完成)。

您的理解实际上相当不错,只是您错过了model.predict(Utils.featurize(t))为您提供由KMeans分配的属于该集群的点。我想你想检查

models.predict(Utils.featurize(t)) == i

在你的代码,因为我经历了所有类群标签迭代。

此外还有一个小小的评论:特征向量创建在一个2克模型的个字符的推文。这个中间步骤很重要;)

2-gram(for words)意思是:“熊对熊的叫喊”=> {(A,熊),(熊,叫喊),(叫喊,at),( at,a),(bear)}即“一只熊”被计数两次。字符将是(A,[空间]),([空间],b),(b,e)等等。

+1

它是'bigrams of characters',所以它应该是'{(“A”,“b”,“be”...}' – 2015-03-09 14:01:56

+0

你是对的,滑动被一个Seq调用[Char]。 – uberwach 2015-03-09 14:59:54

+0

感谢你的这个反应uberwatch。我做了这个改变,它打印了5个集群,除了集群0拥有所有的tweets,而其余的集群没有,我假设意味着所有的tweets被分配到集群0.这是因为数据集太小了?(我认为在databricks提供的例子中,他们训练kmeans模型的时候有1200万条推文,而我只用了200条)或者应该调整numClusters/numIterations? – plambre 2015-03-09 16:08:24