python - 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 的二进制列)溢出行,这确实有效,但现在重新应用代码不适用于采样
解决方案
Numpy 数据类型不能很好地与 Spark 交互。.tolist()
您可以在调用之前使用将它们转换为 Python 数据类型.isin
:
sample = np.random.choice(np.arange(count), replace=True, size=50).tolist()
推荐阅读
- postgresql - 在 PostgreSQL 查询中将钱转换为双倍
- python - 无法使用 python 显示错误 502、504 连接到 Interactive Broker API
- c# - .NET Core 3.0 中 AspNetUserToken 和自定义 JWT 令牌之间的区别
- ssh - 公钥认证
- java - 将加密逻辑从 JAVA 转换为 DART
- pandas - 如何遍历熊猫中的选定行
- rest - 由 Heroku 托管后未设置相同的站点 Cookie 属性
- php - 如何在 php 中将 DD/MM/YYYY h:i 转换为 YYYY-MM-DD h:i
- excel - Excel 不同值列表
- python-3.x - 如何从没有唯一类标识符的情况下刮取一些文本?