<commit_before>"""Utilities for Kaggle competitions

"""

# Author          : <NAME> jjinking(at)gmail(dot)com
# Creation date   : 2015.12.31
# Last Modified   : 2016.04.12
#
# License         : MIT

import pandas as pd

from datsci import ensemble


DEFAULT_SAMPLE_SUBMISSION_FILE = 'data/sample_submission.csv'
DEFAULT_SAMPLE_SUBMISSION_IDX = 'ID'


def save_submission(y_hat, save_file,
                    sample_submission_file=DEFAULT_SAMPLE_SUBMISSION_FILE):
    """Format predicted values for test set, and save for submission to kaggle

    Parameters
    ----------
    y_hat : numpy.ndarray or pandas.Series
        Machine learning model-predicted values.

    save_file : str
        File name to save the predicted values, including path

    sample_submission_file : str
        Path to example submission file provided by Kaggle
    """
    df = pd.read_csv(sample_submission_file)
    df[df.columns[1]] = y_hat
    df.to_csv(save_file, index=False)


def weighted_avg_from_files(
        fnames, outfile, weights=[],
        sample_submission_file=DEFAULT_SAMPLE_SUBMISSION_FILE,
        sample_submission_idx=DEFAULT_SAMPLE_SUBMISSION_IDX):
    """Compute weighted avg from submission files, and save results to a new file

    Parameters
    ----------
    fnames : Iterable of str's
        Submission file names of y_hats to be averaged

    outfile : str
        Output file name, including path<commit_msg>Fixed bug with kaggle save submission function<commit_after>"""Utilities for Kaggle competitions

"""

# Author          : <NAME> jjinking(at)gmail(dot)com
# Creation date   : 2015.12.31
# Last Modified   : 2016.04.29
#
# License         : MIT

import pandas as pd

from datsci import ensemble


DEFAULT_SAMPLE_SUBMISSION_FILE = 'data/sample_submission.csv'
DEFAULT_SAMPLE_SUBMISSION_IDX = 'ID'


def save_submission(y_hat, save_file,
                    sample_submission_file=DEFAULT_SAMPLE_SUBMISSION_FILE):
    """Format predicted values for test set, and save for submission to kaggle

    Parameters
    ----------
    y_hat : numpy.ndarray or pandas.Series
        Machine learning model-predicted values.

    save_file : str
        File name to save the predicted values, including path

    sample_submission_file : str
        Path to example submission file provided by Kaggle
    """
    if isinstance(y_hat, pd.Series):
        y_hat = y_hat.values

    df = pd.read_csv(sample_submission_file)
    df[df.columns[1]] = y_hat
    df.to_csv(save_file, index=False)


def weighted_avg_from_files(
        fnames, outfile, weights=[],
        sample_submission_file=DEFAULT_SAMPLE_SUBMISSION_FILE,
        sample_submission_idx=DEFAULT_SAMPLE_SUBMISSION_IDX):
    """Compute weighted avg from submission files, and save results to a new file

    Parameters
    ----------
    fnames : Iterable of str's
        Submission file names of y_hats to be averaged

    outfile : str
        Output file name, including path
{'fraction_non_alphanumeric': 0.07809330628803246, 'fraction_numerical': 0.011494252873563218, 'mean_word_length': 3.0313351498637604, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5063731', 'n_tokens_mistral': 936, 'n_tokens_neox': 886, 'n_words': 248}