我正在尝试对逻辑回归模型进行简单的训练 . 我的训练数据,模型和错误信息如下所示 . 为什么我得到这个'list'对象没有属性'first'错误?
train_data = numdata.collect()
train_data[:3]
[LabeledPoint(1.0, [2.0,36.0,0.0,100.0,100.0,38.0,0.0,100.0,95.0,100.0,100.0]),
LabeledPoint(1.0, [0.0,77.0,16.0,100.0,99.0,86.0,1.0,99.0,100.0,99.0,95.0]),
LabeledPoint(1.0, [0.0,22.0,0.0,100.0,95.0,21.0,1.0,95.0,100.0,100.0,100.0])]
lrm = LogisticRegressionWithSGD.train(train_data)
------------------------------------------------- -------------------------- AttributeError Traceback(最近一次调用last)in()----> 1 lrm = LogisticRegressionWithSGD.train(train_data )C:\ spark-2.0.1-bin-hadoop2.7 \ python \ pyspark \ mllib \ classification.pyc in train(cls,data,iterations,step,miniBatchFraction,initialWeights,regParam,regType,intercept,validateData,convergenceTol) 319 bool(拦截),bool(validateData),float(convergenceTol))320 - > 321返回_regression_train_wrapper(train,LogisticRegressionModel,data,initialWeights)322 323 C:\ spark-2.0.1-bin-hadoop2.7 \ python _regress_train_wrapper中的\ pyspark \ mllib \ regression.pyc(train_func,modelClass,data,initial_weights)206 def _regression_train_wrapper(train_func,modelClass,data,initial_weights):207 from pyspark.mllib.classification import LogisticRegressionModel - > 208 first = data.first ()209如果不是isinstance(首先,LabeledPoint):210引发TypeError(“数据应该是LabeledPoint的RDD,但得到%s” %type(first))AttributeError:'list'对象没有属性'first'
2 回答
你有一个Python
list
因为那是collect()
返回的 . 你需要RDD
,所以或者,我认为你已经分裂了
numdata
,因为70/30%的分裂所以使用
trainingData
RDD您应该使用RDD而不是列表来呈现
train
方法 . 这就是错误信息所说的内容 .通过
.collect()
,您've gathered the data in the RDD into memory, and you'重写train_data
变量 . 所以现在,它不再是RDD而是内存中的Python列表 .相反,你可以直接在
num_data
上训练 . 或者,如果你想训练3行,如你的例子: