最简单的Python数据集分割:将Dataframe自由分割成指定大小的test和train

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值