【问题标题】:Spark Logistic Regression Error Dimension MismatchSpark Logistic 回归错误维度不匹配
【发布时间】:2016-09-19 23:38:51
【问题描述】:

我刚开始使用 spark 并正在尝试运行逻辑回归。 我不断收到此错误:

Caused by: java.lang.IllegalArgumentException: requirement failed: 
Dimensions mismatch when adding new sample. Expecting 21 but got 17.

我拥有的功能数量是 21 ,但我不确定 17 在这里是什么意思。不知道该怎么办? 我的代码在这里:

from pyspark.mllib.regression import LabeledPoint
from numpy import array

def isfloat(string):
   try:
    float(string)
        return True
    except ValueError:
        return False

def parse_interaction(line):
    line_split = line.split(",")
    # leave_out = [1,2,3]
    clean_line_split = line_split[3:24]
    retention = 1.0
    if line_split[0] == '0.0':
       retention = 0.0
    return LabeledPoint(retention, array([map(float,i) for i in clean_line_split if isfloat(i)]))

training_data = raw_data.map(parse_interaction)

from pyspark.mllib.classification import LogisticRegressionWithLBFGS
from time import time

t0 = time()
logit_model = LogisticRegressionWithLBFGS.train(training_data)
tt = time() - t0

print "Classifier trained in {} seconds".format(round(tt,3))

【问题讨论】:

  • 由于您在创建 array 时过滤掉了值,因此它的长度可以在 0 和预期大小之间。丢弃格式错误的条目会更有意义。

标签: python apache-spark pyspark logistic-regression


【解决方案1】:

错误来自维度不匹配的矩阵乘法。数组没有得到所有 21 个值。我建议你将变量设置为 0,以防它们不是浮动的,因为(似乎)你想要的

【讨论】:

    【解决方案2】:

    原始数据似乎有些问题。我猜有些值没有通过 isFloat 验证。您可以尝试在控制台上打印值吗,它将帮助您识别错误行。

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 2017-03-02
      • 1970-01-01
      • 2017-07-19
      • 2020-04-27
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多