很难理解为什么float()会给出这个

2024-04-25 05:22:48 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是数据集的一部分:

18,8,307,130,3504,12,70,1,chevrolet
15,8,350,165,3693,11.5,70,1,buick
18,8,318,150,3436,11,70,1,plymouth
16,8,304,150,3433,12,70,1,amc
17,8,302,140,3449,10.5,70,1,ford
15,8,429,198,4341,10,70,1,ford
14,8,454,220,4354,9,70,1,chevrolet
14,8,440,215,4312,8.5,70,1,plymouth

代码如下:

data = sc.textFile("hw6/auto_mpg_original.csv")
records = data.map(lambda x: x.split(","))

hp = float(records.map(lambda x: x[3]))
disp = np.array(float(records.map(lambda x: x[2])))

final_data_1 = LabeledPoint(hp, disp)

错误如下:

Traceback (most recent call last):
  File "/home/cloudera/Desktop/hw6.py", line 41, in <module>
    hp = float(records.map(lambda x: x[3]))
TypeError: float() argument must be a string or a number

这看起来很基本,但我真的很难找到解决办法。你知道吗


Tags: 数据lambda代码mapdatafloathprecords
2条回答

检查records.map()的类型可能是RDD。您可以在map()中应用float(),例如:

hp = records.map(lambda x: float(x[3]))

但在使用前需要.collect()结果,例如:

hp = records.map(lambda x: float(x[3])).collect()
disp = np.array(records.map(lambda x: float(x[2])).collect())

CSV的输入有问题,列为空或包含非数字值

相关问题 更多 >