TypeError: only integer scalar arrays can be converted to a scalar index

venu_s發表於2020-11-10

TypeError: only integer scalar arrays can be converted to a scalar index

  • 在使用 np.random.choice()時,將返回的List作為Data[]的索引物件
  • 程式出現TypeError: only integer scalar arrays can be converted to a scalar index的錯誤

原始語句

sample_index = np.random.choice(num_train, batch_size, replace = True)
X_batch = X[sample_index, :]
y_batch = y[sample_index]

TypeError: only integer scalar arrays can be converted to a scalar index

解決方法

  • List轉換為numpy陣列,即:np.array(data)[List]
sample_index = np.random.choice(num_train, batch_size, replace = True)
X_batch = np.array(X)[sample_index, :]
y_batch = np.array(y)[sample_index]

相關文章