在做機器學習相關的資料處理時,時常需要將資料進行亂數的打亂,以達到有效的學習到資料的本身.

而在監督式學習之中,除了輸入的資料X 以外還有答案Y,而X與Y是互相對應的,因此在打亂的時候又需要保持資料的對應性,實作方法如下:

 

載入相關套件

import numpy as np
import random

建立打亂資料的函數(支援多個輸入)

def shuffle(*args):
    seed = random.randint(0,2**32)
    rand_state = np.random.RandomState(seed)
    for lst in args:
        rand_state.shuffle(lst)
        rand_state.seed(seed)

測試實驗結果如下:

arrow
arrow
    文章標籤
    python ML random
    全站熱搜

    Lung-Yu,Tsai 發表在 痞客邦 留言(0) 人氣()