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])




