kflod,cross_validation等函数包提供了很好的“成比例分割数据集”的方法,但是当我们希望获得指定大小的数据集时,应该怎么做呢?
笔者在课程实验中遇到了这个问题,编写了如下函数:
#Split the data into target number
def trainTestSplit(X,Y,train_num_of_X):
'''
This function can split the data into desire num for test and train by random.
Variables Describe:
X: Datafram without label
Y: Data labels
train_num_of_X: numbers of train set
'''
X_num=X.shape[0]
test_index= list(range(X_num))
train_index=[]
train_num=train_num_of_X
for i in range(train_num):
randomIndex=int(np.random.uniform(0,len(test_index)))#Choose train set by random
train_index.append(test_index[randomIndex])
del test_index[randomIndex]
#Control the label consistency
train=X.iloc[train_index]
label_train=Y.iloc[train_index]
test=X.iloc[test_index]
label_test=Y.iloc[test_index]
return train,test,label_train,label_test
上面的函数是在x存储特征,y存储label的操作。如果你的daraframe把label也放进去了,那么应该尝试如下函数:
#Split the data into target number
def trainTestSplit(X,train_num_of_X):
'''
This function can split the data into desire num for test and train by random.
Variables Describe:
X: Datafram without label
train_num_of_X: numbers of train set
'''
X_num=X.shape[0]
test_index= list(range(X_num))
train_index=[]
train_num=train_num_of_X
for i in range(train_num):
randomIndex=int(np.random.uniform(0,len(test_index)))#Choose train set by random
train_index.append(test_index[randomIndex])
del test_index[randomIndex]
#Control the label consistency
train=X.iloc[train_index]
test=X.iloc[test_index]
return train,test