簡體   English   中英

TypeError:數據應該是LabeledPoint的RDD,但是得到了<type 'numpy.ndarray'>

[英]TypeError: data should be an RDD of LabeledPoint, but got <type 'numpy.ndarray'>

我收到錯誤:

TypeError: data should be an RDD of LabeledPoint, but got <type 'numpy.ndarray'>

當我執行時:

import sys
import numpy as np
from pyspark import SparkConf, SparkContext
from pyspark.mllib.classification import LogisticRegressionWithSGD


conf = (SparkConf().setMaster("local")
.setAppName("Logistic Regression")
.set("spark.executor.memory", "1g"))
sc = SparkContext(conf = conf) 


def mapper(line):
    feats = line.strip().split(",") 
    label = feats[len(feats) - 1]       # Last column is the label
    feats = feats[2: len(feats) - 1]    # remove id and type column
    feats.insert(0,label)
    features = [ float(feature) for feature in feats ] # need floats
    return np.array(features)

data = sc.textFile("test.csv")
parsedData = data.map(mapper)

# Train model
model = LogisticRegressionWithSGD.train(parsedData)

我在model = LogisticRegressionWithSGD.train(parsedData)行上收到錯誤。

parsedData應該是RDD。 我不確定為什么要得到這個。

Github鏈接到完整源代碼

parsedData應該是RDD。 我不確定為什么要得到這個。

問題不是parsedData不是RDD ,而是它存儲的內容。 由於消息說,你需要RDD[LabeledPoint]當你通過RDD[numpy.ndarray]

from pyspark.mllib.regression import LabeledPoint

def mapper(line):
    ...
    return LabeledPoint(label, features)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM