開始使用免費開始

隨機選取列

在這個練習中,你會比較兩種在 pandas DataFrame 中「可重複抽樣」隨機選取列(entries)的方法:

  • 內建的 pandas 函式 .sample()
  • NumPy 的隨機整數產生器 np.random.randint()

在統計與機器學習領域中,訓練演算法時,常見做法是用可用資料的 75% 來訓練,剩下的 25% 用來評估效能。

在本練習中,我們會用上述每一種方法,隨機抽取所有撲克牌手牌資料的 75%,並比較哪一種方法在速度上更有效率。

本練習屬於課程

使用 pandas 撰寫高效程式碼

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Extract number of rows in dataset
N=poker_hands.shape[0]

# Select and time the selection of the 75% of the dataset's rows
rand_start_time = time.time()
poker_hands.iloc[np.random.randint(____=0, high=____, ____=int(0.75 * N))]
print("Time using Numpy: {} sec".format(time.time() - rand_start_time))
編輯並執行程式碼