models/train_classifier.py
def main():
    """
    Runs script step by step until it saves a new trained model
    :return: None
    """

    messages_filepath = ''
    categories_filepath = ''
    database_filepath = ''
    model_filepath = ''
    models_path = ''

    basedir = [f for f in os.listdir("../") if isdir(join("../", f))]

    if 'data' in basedir:

        data_path = "../data"
        data_files = [f for f in os.listdir(data_path) if isfile(join(data_path, f))]

        for file in data_files:
            if "messages.csv" in file:
                messages_filepath = f"{data_path}/{file}"

            elif "categories.csv" in file:
                categories_filepath = f"{data_path}/{file}"

            elif "data.db" in file:
                database_filepath = f"{data_path}/{file}"

    if 'models' in basedir:

        models_path = "../models"
        models_files = [f for f in os.listdir(models_path) if isfile(join(models_path, f))]

        for file in models_files:

            if "classifier.pkl" in file:
                model_filepath = f"{models_path}/{file}"

    if database_filepath:
        print("Database Path: Ok")
        print(database_filepath)
    if messages_filepath:
        print("Messages Data Path: Ok")
        print(messages_filepath)
    if categories_filepath:
        print("Categories Data Path: Ok")
        print(categories_filepath)
    if model_filepath:
        print("Model Path: Ok")
        print(model_filepath)
    else:
        model_filepath = f"{models_path}/classifier.pkl"

    # Print the system arguments
    try:
        print('Loading data...\n    DATABASE: {}'.format(database_filepath))
        X, Y, category_names = load_data(database_filepath)
        X_train, X_test, y_train, y_test = train_test_split(X, Y, random_state=42)
        
        print('Building model...')

        grid_tag = False

        if grid_tag:
            model = build_model(gridsearch=grid_tag)
        else:
            warnings.warn("\nGridSearch is off due to timeXperformance tradeoff:\n"
                  "Without Gridsearch it takes:\n 11 to 20 minutes to train the model in my machine and 4 minutes in Udacity Machine\n"
                  "With Gridsearch it takes 2 hours and 28 minutes. Gridsearch with the parameters selected improved only 0.13%.\n"
                  "If you want to test with GridSearch change the variable 'grid_tag' and assign True")
            model = build_model(gridsearch=grid_tag)

        
        print('Training model...')
        model.fit(X_train, y_train)
        
        print('Evaluating model...')
        evaluate_model(model, X_test, y_test, category_names)

        print('Saving model...\n    MODEL: {}'.format(model_filepath))
        save_model(model, model_filepath)

        print('Trained model saved!')

    except:
        print('Please provide the filepath of the disaster messages database '\
              'as the first argument and the filepath of the pickle file to '\
              'save the model to as the second argument. \n\nExample: python '\
              'train_classifier.py ../data/DisasterResponse.db classifier.pkl')

    Runs script step by step until it saves a new trained model
    :return: None
    
Runs script step by step until it saves a new trained model
Runs script step by step until it saves a new trained model
def main():
    messages_filepath = ''
    categories_filepath = ''
    database_filepath = ''
    model_filepath = ''
    models_path = ''
    basedir = [f for f in os.listdir("../") if isdir(join("../", f))]
    if 'data' in basedir:
        data_path = "../data"
        data_files = [f for f in os.listdir(data_path) if isfile(join(data_path, f))]
        for file in data_files:
            if "messages.csv" in file:
                messages_filepath = f"{data_path}/{file}"
            elif "categories.csv" in file:
                categories_filepath = f"{data_path}/{file}"
            elif "data.db" in file:
                database_filepath = f"{data_path}/{file}"
    if 'models' in basedir:
        models_path = "../models"
        models_files = [f for f in os.listdir(models_path) if isfile(join(models_path, f))]
        for file in models_files:
            if "classifier.pkl" in file:
                model_filepath = f"{models_path}/{file}"
    if database_filepath:
        print("Database Path: Ok")
        print(database_filepath)
    if messages_filepath:
        print("Messages Data Path: Ok")
        print(messages_filepath)
    if categories_filepath:
        print("Categories Data Path: Ok")
        print(categories_filepath)
    if model_filepath:
        print("Model Path: Ok")
        print(model_filepath)
    else:
        model_filepath = f"{models_path}/classifier.pkl"
    try:
        print('Loading data...\n    DATABASE: {}'.format(database_filepath))
        X, Y, category_names = load_data(database_filepath)
        X_train, X_test, y_train, y_test = train_test_split(X, Y, random_state=42)
        print('Building model...')
        grid_tag = False
        if grid_tag:
            model = build_model(gridsearch=grid_tag)
        else:
            warnings.warn("\nGridSearch is off due to timeXperformance tradeoff:\n"
                  "Without Gridsearch it takes:\n 11 to 20 minutes to train the model in my machine and 4 minutes in Udacity Machine\n"
                  "With Gridsearch it takes 2 hours and 28 minutes. Gridsearch with the parameters selected improved only 0.13%.\n"
                  "If you want to test with GridSearch change the variable 'grid_tag' and assign True")
            model = build_model(gridsearch=grid_tag)
        print('Training model...')
        model.fit(X_train, y_train)
        print('Evaluating model...')
        evaluate_model(model, X_test, y_test, category_names)
        print('Saving model...\n    MODEL: {}'.format(model_filepath))
        save_model(model, model_filepath)
        print('Trained model saved!')
    except:
        print('Please provide the filepath of the disaster messages database '\
              'as the first argument and the filepath of the pickle file to '\
              'save the model to as the second argument. \n\nExample: python '\
              'train_classifier.py ../data/DisasterResponse.db classifier.pkl')
def main ( ) : messages_filepath = '' categories_filepath = '' database_filepath = '' model_filepath = '' models_path = '' basedir = [ f for f in os . listdir ( "../" ) if isdir ( join ( "../" , f ) ) ] if 'data' in basedir : data_path = "../data" data_files = [ f for f in os . listdir ( data_path ) if isfile ( join ( data_path , f ) ) ] for file in data_files : if "messages.csv" in file : messages_filepath = f"{data_path}/{file}" elif "categories.csv" in file : categories_filepath = f"{data_path}/{file}" elif "data.db" in file : database_filepath = f"{data_path}/{file}" if 'models' in basedir : models_path = "../models" models_files = [ f for f in os . listdir ( models_path ) if isfile ( join ( models_path , f ) ) ] for file in models_files : if "classifier.pkl" in file : model_filepath = f"{models_path}/{file}" if database_filepath : print ( "Database Path: Ok" ) print ( database_filepath ) if messages_filepath : print ( "Messages Data Path: Ok" ) print ( messages_filepath ) if categories_filepath : print ( "Categories Data Path: Ok" ) print ( categories_filepath ) if model_filepath : print ( "Model Path: Ok" ) print ( model_filepath ) else : model_filepath = f"{models_path}/classifier.pkl" try : print ( 'Loading data...\n    DATABASE: {}' . format ( database_filepath ) ) X , Y , category_names = load_data ( database_filepath ) X_train , X_test , y_train , y_test = train_test_split ( X , Y , random_state = 42 ) print ( 'Building model...' ) grid_tag = False if grid_tag : model = build_model ( gridsearch = grid_tag ) else : warnings . warn ( "\nGridSearch is off due to timeXperformance tradeoff:\n" "Without Gridsearch it takes:\n 11 to 20 minutes to train the model in my machine and 4 minutes in Udacity Machine\n" "With Gridsearch it takes 2 hours and 28 minutes. Gridsearch with the parameters selected improved only 0.13%.\n" "If you want to test with GridSearch change the variable 'grid_tag' and assign True" ) model = build_model ( gridsearch = grid_tag ) print ( 'Training model...' ) model . fit ( X_train , y_train ) print ( 'Evaluating model...' ) evaluate_model ( model , X_test , y_test , category_names ) print ( 'Saving model...\n    MODEL: {}' . format ( model_filepath ) ) save_model ( model , model_filepath ) print ( 'Trained model saved!' ) except : print ( 'Please provide the filepath of the disaster messages database ' 'as the first argument and the filepath of the pickle file to ' 'save the model to as the second argument. \n\nExample: python ' 'train_classifier.py ../data/DisasterResponse.db classifier.pkl' )
Runs script step by step until it saves a new trained model
Runs script step by step until it saves a new trained model
"""
    Runs script step by step until it saves a new trained model
    :return: None
    """ # Print the system arguments
None