zoukankan      html  css  js  c++  java
  • 利用pandas随机切分csv文件

    把数据集随机切分为训练集和测试集

    method 1:

    df = pd.read_csv('data/tgnb_merge.csv', encoding='utf-8')
    df.drop_duplicates(keep='first', inplace=True)  # 去重,只保留第一次出现的样本
    df_test = df.sample(frac=0.1)
    df_train = pd.concat([df, df_test], axis=0)   # 拼接
    df_train.drop_duplicates(keep=False, inplace=True)  # 去除所有重复的样本
    print df.shape, df_test.shape, df_train.shape  # (3045, 12) (305, 12) (2740, 12)

    method 2(推荐):

    df = pd.read_csv('data/tgnb_merge.csv', encoding='utf-8')
    # df.drop_duplicates(keep='first', inplace=True)  # 去重,只保留第一次出现的样本
    df = df.sample(frac=1.0)  # 全部打乱
    cut_idx = int(round(0.1 * df.shape[0]))
    df_test, df_train = df.iloc[:cut_idx], df.iloc[cut_idx:]
    print df.shape, df_test.shape, df_train.shape  # (3184, 12) (318, 12) (2866, 12)
  • 相关阅读:
    css布局
    css笔记
    css笔记
    css笔记
    HttpServletResponse简单理解
    SpringCloud Zuul网关的简单理解
    SpringCloud Zuul网关超时
    notepad++实用技巧
    Json常用代码
    含有Date属性的对象转化为Json
  • 原文地址:https://www.cnblogs.com/bymo/p/7248993.html
Copyright © 2011-2022 走看看