代码之家  ›  专栏  ›  技术社区  ›  PatPanda

Java Spark ML-NaiveBayesModel对未来时间戳的转换/预测

  •  0
  • PatPanda  · 技术社区  · 5 年前

    关于使用SparkML进行预测/预测的小问题 朴素贝叶斯

    我有一个非常简单的数据集,它只是时间戳,代表一天,以及当天卖了多少煎饼:

    dataSetPancakes.show();
    
    +----------+-----+
    |      time|label|
    +----------+-----+
    |1622505600|    1|
    |1622592000|    0|
    |1622678400|    3|
    |1622764800|    1|
    |1622851200|    1|
    |1622937600|    1|
    |1623024000|    1|
    |1623110400|    2|
    |1623196800|    2|
    |1623283200|    0|
    +----------+-----+
    only showing top 10 rows"
    

    很简单,我只想预测明天、后天等的煎饼销量。。。

    因此,我尝试了Naive Bayes模型,遵循这里的教程 https://spark.apache.org/docs/latest/ml-classification-regression.html#naive-bayes ,我写道:

           VectorAssembler vectorAssembler = new VectorAssembler().setInputCols(new String[]{"time"}).setOutputCol("features");
            Dataset<Row> vectorData = vectorAssembler.transform(dataSetPancakes);
     NaiveBayes naiveBayes = new NaiveBayes();
            NaiveBayesModel model = naiveBayes.fit(vectorData);
            Dataset<Row> predictions = model.transform(vectorData);
            predictions.show();
        model.predict(new DenseVector(new double[]{getTomorrowTimestamp()})));
    
    

    我甚至看到了这样的结果:

    -RECORD 0--------------------------------------------------------------------------------------------------------------
     time          | 1622505600                                                                                            
     label         | 1                                                                                                     
     features      | [1.6225056E9]                                                                                         
     rawPrediction | [-0.9400072584914714,-1.0831081021321447,-1.702147310538368,-2.5494451709255714,-4.564348191467836]   
     probability   | [0.39062499999999994,0.33854166666666663,0.18229166666666666,0.07812500000000001,0.01041666666666667] 
     prediction    | 0.0                                                                                                   
    -RECORD 1--------------------------------------------------------------------------------------------------------------
     time          | 1622592000                                                                                            
     label         | 0                                                                                                     
     features      | [1.622592E9]                                                                                          
     rawPrediction | [-0.9400072584914714,-1.0831081021321447,-1.702147310538368,-2.5494451709255714,-4.564348191467836]   
     probability   | [0.39062499999999994,0.33854166666666663,0.18229166666666666,0.07812500000000001,0.01041666666666667] 
     prediction    | 0.0                                                                                                   
    
    

    但就预测本身而言 始终显示0.0 不幸的是,明天。

    请问这个问题的根本原因是什么?

    非常感谢。

    0 回复  |  直到 4 年前
        1
  •  6
  •   3Fish    4 年前

    您不应该使用用于预测的相同数据集进行训练。否则,您将不会进行任何预测。

    Dataset<Row>[] splits = vectorData.randomSplit(new double[]{0.6, 0.4}, 1234L);
    Dataset<Row> train = splits[0];
    Dataset<Row> test = splits[1];
    

    此外,算法绝对有可能了解到,对于任何一天,可能的结果都是0。你应该知道,日期和销售额之间没有真正的关系。这些日期不是重复出现的,因此无法做出真正的预测。贝叶斯算法也没有意识到这些条目实际上是一系列事件。它只是计算当值“feature”为“1622505600”时,“label”的可能值有多高。

    我建议像一周中的某一天这样的功能,因为这些功能会重复出现,看看工作日的销售额特别高会更有意义。

    或者,你可以给它提供第二个功能,就像昨天的销售一样。这将允许算法在前一天实际进行预测。

    推荐文章