TypeError: only integer scalar arrays can be converted to a scalar index
TypeError: only integer scalar arrays can be converted to a scalar index
觉得有用的话,欢迎一起讨论相互学习~
- 使用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错误。
解决方案
- 将List转换为numpy数组即:np.array(data)[test_indices]
train_indices = np.random.choice(35444, 24500, replace=False)
writer.writerows(np.array(data)[train_indices])