预处理算法_5_数据集划分

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# <editable>

def execute():
    # <editable>
    '''
    载入模块
    '''
    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sqlalchemy import create_engine
    '''
    连接数据库
    '''
    engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis')
    '''
    选择目标数据
    '''
    params = {
        "left_columns": "field1, field2",
        "test_size": 0.25,
        "random_state": 20
    }
    inputs = {"table": '数据集划分', }
    '''
    选择目标数据
    '''
    left_sql = 'select ' + params['left_columns'] + ' from ' + inputs['table']
    left = pd.read_sql_query(left_sql, engine)
    '''
    数据拆分
    '''
    '''
    test_size:可以为浮点、整数或None,默认为None
    ①若为浮点时,表示测试集占总样本的百分比
    ②若为整数时,表示测试样本样本数
    ③若为None时,test size自动设置成0.25
    #######################################
    random_state:可以为整数、RandomState实例或None,默认为None
    ①若为None时,每次生成的数据都是随机,可能不一样
    ②若为整数时,每次生成的数据都相同
    '''
    train, test = train_test_split(left, test_size=float(params['test_size']),
                                   random_state=int(params['random_state']))
    '''
    将结果写出
    '''
    print(left)
    print(train)
    print(test)
    '''
        数据示例
            field1 field2
    0        0      A
    1        1      B
    2        2      A
    3        3      A
    4        4      B
    5        5      A
    6        6      B
    7        7      B
    8        8      B
    9        9      A
    10      10      A
    11      11      B
    12      12      A
    13      13      A
    14      14      A
    15      15      B
    16      16      B
    17      17      A
    18      18      B
    19      19      A
        field1 field2
    14      14      A
    17      17      A
    12      12      A
    19      19      A
    5        5      A
    8        8      B
    18      18      B
    0        0      A
    2        2      A
    7        7      B
    6        6      B
    11      11      B
    9        9      A
    15      15      B
    3        3      A
        field1 field2
    16      16      B
    13      13      A
    10      10      A
    4        4      B
    1        1      B
    '''


# </editable>

if __name__ == '__main__':
    execute()

 

posted @ 2021-03-03 19:45  我当道士那儿些年  阅读(89)  评论(0编辑  收藏  举报