首页 > 解决方案 > AttributeError:“numpy.int64”对象没有属性“_get_object_id”

问题描述

我在 pyspark 中有一个数据集,为此我创建了一个 row_num 列,因此我的数据如下所示:

#data:
+-----------------+-----------------+-----+------------------+-------+
|F1_imputed       |F2_imputed       |label|          features|row_num|
+-----------------+-----------------+-----+------------------+-------+
|        -0.002353|           0.9762|    0|[-0.002353,0.9762]|      1|
|           0.1265|           0.1176|    0|   [0.1265,0.1176]|      2|
|         -0.08637|          0.06524|    0|[-0.08637,0.06524]|      3|
|          -0.1428|           0.4705|    0|  [-0.1428,0.4705]|      4|
|          -0.1015|           0.6811|    0|  [-0.1015,0.6811]|      5|
|         -0.01146|           0.8273|    0| [-0.01146,0.8273]|      6|
|           0.0853|           0.2525|    0|   [0.0853,0.2525]|      7|
|           0.2186|           0.2725|    0|   [0.2186,0.2725]|      8|
|           -0.145|           0.3592|    0|   [-0.145,0.3592]|      9|
|          -0.1176|           0.4225|    0|  [-0.1176,0.4225]|     10|
+-----------------+-----------------+-----+------------------+-------+

我正在尝试使用以下方法过滤掉随机选择的行:

count = data.count()
sample = [np.random.choice(np.arange(count), replace=True, size=50)]
filtered = data.filter(data.row_num.isin(sample))

但是第二行给出了一个错误:

AttributeError: 'numpy.int64' object has no attribute '_get_object_id'

这是什么原因造成的?我使用相同的过滤代码通过label(1 和 0 的二进制列)溢出行,这确实有效,但现在重新应用代码不适用于采样

标签: pythonnumpyapache-sparkpyspark

解决方案


Numpy 数据类型不能很好地与 Spark 交互。.tolist()您可以在调用之前使用将它们转换为 Python 数据类型.isin

sample = np.random.choice(np.arange(count), replace=True, size=50).tolist()

推荐阅读