數據預處理—打亂訓練數據順序

改變二維數組的一維順序

import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
lable = np.arange(3)
permutation = np.random.permutation(lable.shape[0])
data = a[permutation,:]
或讀取文件時定義函數 :
def open_data(fname, ratio_train):
    """Input:
    direc: location of the UCR archive
    ratio_train: ratio to split training and testset
    dataset: name of the dataset in the UCR archive"""
    # (390,177)
    data_train = np.loadtxt(fname+'/'+fname+'_TRAIN', delimiter=',')
    # (391,177)
    data_test_val = np.loadtxt(fname+'/'+fname+'_TEST', delimiter=',')
    # data shape=(781,177)
    data = np.concatenate((data_train, data_test_val), axis=0)
    N, D = data.shape
    ind_cut = int(ratio_train * N)
    ind = np.random.permutation(N) # ind從0到N-1的亂序數據
    # 返回ind_cut前的數據做作爲訓練集,ind_cut後的數據作爲測試集
    return data[ind[:ind_cut], 1:], data[ind[ind_cut:], 1:], data[ind[:ind_cut], 0], data[ind[ind_cut:], 0]
X_train, X_val, y_train, y_val = open_data(direc,ratio_train)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章