only integer scalar arrays can be converted to a scalar index

时间:2022-07-23
本文章向大家介绍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])