zoukankan      html  css  js  c++  java
  • 生成K-fold交叉验证数据集

    import pandas as pd
    from sklearn import model_selection
    if __name__ == "__main__":
      
      # Training data is in a CSV file called train.csv
      df = pd.read_csv("train.csv")
      # we create a new column called kfold and fill it with -1
      df["kfold"] = -1
      # the next step is to randomize the rows of the data
      df = df.sample(frac=1).reset_index(drop=True)
      # initiate the kfold class from model_selection module
      kf = model_selection.KFold(n_splits=5)
      # fill the new kfold column
      for fold, (trn_, val_) in enumerate(kf.split(X=df)):
        df.loc[val_, 'kfold'] = fold
        # save the new csv with kfold column
      df.to_csv("train_folds.csv", index=False)
    ============================================
    import pandas as pd
    from sklearn.datasets import make_regression
    from sklearn import model_selection
    if __name__ == '__main__':
    X,y = make_regression(n_samples=15000,n_features=8,n_targets=1)
    df = pd.DataFrame(X,columns=[f"f_{i}" for i in range(X.shape[1])])
    df.loc[:, "target"] = y
    df["kfold"] = -1
    df = df.sample(frac=1).reset_index(drop=True)
    kf = model_selection.KFold(n_splits=5)
    for f, (t_,v_) in enumerate(kf.split(X=df)):
    df.loc[v_, "kfold"] = f

    print(df)
  • 相关阅读:
    Ocelot + IdentityServer4 坑自己
    撸一个简易商城
    visual studio2013负载测试简单问题记录
    Jquery简单动画的实现记录
    Jquery的一些简单使用记录
    图片变灰css3
    Jquery的一些取值
    ASP.NET WebApi 简单记录
    iframe的一些简单记录
    Jquery Select 下拉框处理
  • 原文地址:https://www.cnblogs.com/songyuejie/p/14781125.html
Copyright © 2011-2022 走看看