zoukankan      html  css  js  c++  java
  • K-fold Train Version2

    # config.py
    TRAINING_FILE = "../input/mnist_train_folds.csv"
    MODEL_OUTPUT = "../models/"
    # train.py
    import argparse
    import os
    import config
    import joblib
    import pandas as pd
    from sklearn import metrics
    from sklearn import tree
    def run(fold):
    # read the training data with folds
    df = pd.read_csv(config.TRAINING_FILE)
    # training data is where kfold is not equal to provided fold
    # also, note that we reset the index
    df_train = df[df.kfold != fold].reset_index(drop=True)
    # validation data is where kfold is equal to provided fold
    df_valid = df[df.kfold == fold].reset_index(drop=True)
    # drop the label column from dataframe and convert it to
    # a numpy array by using .values.
    # target is label column in the dataframe
    x_train = df_train.drop("label", axis=1).values
    y_train = df_train.label.values
    # similarly, for validation, we have
    x_valid = df_valid.drop("label", axis=1).values
    y_valid = df_valid.label.values
    # initialize simple decision tree classifier from sklearn
    clf = tree.DecisionTreeClassifier()
    # fir the model on training data
    clf.fit(x_train, y_train)
    # create predictions for validation samples
    preds = clf.predict(x_valid)
    # calculate & print accuracy
    accuracy = metrics.accuracy_score(y_valid, preds)
    print(f"Fold={fold}, Accuracy={accuracy}")
    # save the model
    joblib.dump(
    clf,
    os.path.join(config.MODEL_OUTPUT, f"dt_{fold}.bin")
    )


    if __name__ == "__main__":
    # initialize ArgumentParser class of argparse
    parser = argparse.ArgumentParser()
    # add the different arguments needed and their type
    # currently, only need fold
    parser.add_argument(
    "--fold",
    type=int
    )
    # read the arguments from the command line
    args = parser.parse_args()
    # run the fold specified by command line arguments
    run(fold=args.fold)
    ========================================================================================
    #!/bin/sh
    #run.sh
    python train.py --fold 0
    python train.py --fold 1
    python train.py --fold 2
    python train.py --fold 3
    python train.py --fold 4
    ========================================================================================= 
    sh run.sh
     
  • 相关阅读:
    Maven安装及配置
    Java部分概念理解
    API.day01
    随机生成10元素数组并找出最大元素(Java)
    冒泡排序(Java)
    俄罗斯方块部分功能(Java)
    判断闰年(Java)
    判断质数(Java)
    基于DSP的IS95正向业务信道模块设计
    Lua程序设计(4th) 第一部分 语言基础
  • 原文地址:https://www.cnblogs.com/songyuejie/p/14785902.html
Copyright © 2011-2022 走看看