1.问题描述
- 使用np.random.choice创建list,使用这个list作为data 这个list的索引。
- 出现TypeError: only integer scalar arrays can be converted to a scalar index错误。
- 原始语句:
train_indices = np.random.choice(35444, 24500, replace=False)
writer.writerows(data[train_indices])
# TypeError: only integer scalar arrays can be converted to a scalar index错误。
2.解决方案
- 将data这个list转换为numpy数组即可: np.array(data)[test_indices]
train_indices = np.random.choice(35444, 24500, replace=False)
writer.writerows(np.array(data)[train_indices])
3.参考