<filename>IMLearn/utils/utils.py
from typing import Tuple
import numpy as np
import pandas as pd
import time


def split_train_test(X: pd.DataFrame, y: pd.Series, train_proportion: float = .75) \
        -> Tuple[pd.DataFrame, pd.Series, pd.DataFrame, pd.Series]:
    """
    Randomly split given sample to a training- and testing sample

    Parameters
    ----------
    X : DataFrame of shape (n_samples, n_features)
        Data frame of samples and feature values.

    y : Series of shape (n_samples, )
        Responses corresponding samples in data frame.

    train_proportion: Fraction of samples to be split as training set

    Returns
    -------
    train_X : DataFrame of shape (ceil(train_proportion * n_samples), n_features)
        Design matrix of train set

    train_y : Series of shape (ceil(train_proportion * n_samples), )
        Responses of training samples

    test_X : DataFrame of shape (floor((1-train_proportion) * n_samples), n_features)
        Design matrix of test set

    test_y : Series of shape (floor((1-train_proportion) * n_samples), )
        Responses of test samples

    """
    # join X_y to shuffle them simultaneously and then seperate them again
    y.name = 'labels'
    X_y = X.join(y)
    X_y = X_y.sample(frac=1).reset_index(drop=True)
    X, y = X_y.iloc[:, :-1], X_y.iloc[:, -1:]

    amount = int(np.ceil(train_proportion * y.size))
    return X.iloc[:amount], y.iloc[:amount], X.iloc[amount:], y.iloc[amount:]


def confusion_matrix(a: np.ndarray, b: np.ndarray) -> np.ndarray:
    """
    Compute a confusion matrix between two sets of integer vectors

    Parameters
    ----------
    a: ndarray of shape (n_samples,)
        First vector of integers

    b: ndarray of shape (n_samples,)
        Second vector of integers

    Returns
    -------
    confusion_matrix: ndarray of shape (a_unique_values, b_unique_values)
        A confusion matrix where the value of the i,j index shows the number of times value `i` was found in vector `a`
        while value `j` vas found in vector `b`
    """
    raise NotImplementedError()


def measure_time(f):
    """
    wrapper for a function f that prints the time it ran.
    :return:
    """
    def timed(*args, **kw):
        ts = time.time()
        result = f(*args, **kw)
        te = time.time()
        print('%r %2.2f sec' % (f.__name__, te - ts))
        return result

    return timed

<filename>IMLearn/utils/utils.py
from typing import Tuple
import numpy as np
import pandas as pd
import time


def split_train_test(X: pd.DataFrame, y: pd.Series, train_proportion: float = .75) \
        -> Tuple[pd.DataFrame, pd.Series, pd.DataFrame, pd.Series]:
    """
    Randomly split given sample to a training- and testing sample

    Parameters
    ----------
    X : DataFrame of shape (n_samples, n_features)
        Data frame of samples and feature values.

    y : Series of shape (n_samples, )
        Responses corresponding samples in data frame.

    train_proportion: Fraction of samples to be split as training set

    Returns
    -------
    train_X : DataFrame of shape (ceil(train_proportion * n_samples), n_features)
        Design matrix of train set

    train_y : Series of shape (ceil(train_proportion * n_samples), )
        Responses of training samples

    test_X : DataFrame of shape (floor((1-train_proportion) * n_samples), n_features)
        Design matrix of test set

    test_y : Series of shape (floor((1-train_proportion) * n_samples), )
        Responses of test samples

    """
    # join X_y to shuffle them simultaneously and then seperate them again
    y.name = 'labels'
    X_y = X.join(y)
    X_y = X_y.sample(frac=1).reset_index(drop=True)
    X, y = X_y.iloc[:, :-1], X_y.iloc[:, -1:]

    amount = int(np.ceil(train_proportion * y.size))
    return X.iloc[:amount], y.iloc[:amount], X.iloc[amount:], y.iloc[amount:]


def confusion_matrix(a: np.ndarray, b: np.ndarray) -> np.ndarray:
    """
    Compute a confusion matrix between two sets of integer vectors

    Parameters
    ----------
    a: ndarray of shape (n_samples,)
        First vector of integers

    b: ndarray of shape (n_samples,)
        Second vector of integers

    Returns
    -------
    confusion_matrix: ndarray of shape (a_unique_values, b_unique_values)
        A confusion matrix where the value of the i,j index shows the number of times value `i` was found in vector `a`
        while value `j` vas found in vector `b`
    """
    raise NotImplementedError()


def measure_time(f):
    """
    wrapper for a function f that prints the time it ran.
    :return:
    """
    def timed(*args, **kw):
        ts = time.time()
        result = f(*args, **kw)
        te = time.time()
        print('%r %2.2f sec' % (f.__name__, te - ts))
        return result

    return timed

Randomly split given sample to a training- and testing sample      Parameters     ----------     X : DataFrame of shape (n_samples, n_features)         Data frame of samples and feature values.      y : Series of shape (n_samples, )         Responses corresponding samples in data frame.      train_proportion: Fraction of samples to be split as training set      Returns     -------     train_X : DataFrame of shape (ceil(train_proportion * n_samples), n_features)         Design matrix of train set      train_y : Series of shape (ceil(train_proportion * n_samples), )         Responses of training samples      test_X : DataFrame of shape (floor((1-train_proportion) * n_samples), n_features)         Design matrix of test set      test_y : Series of shape (floor((1-train_proportion) * n_samples), )         Responses of test samples # join X_y to shuffle them simultaneously and then seperate them again Compute a confusion matrix between two sets of integer vectors      Parameters     ----------     a: ndarray of shape (n_samples,)         First vector of integers      b: ndarray of shape (n_samples,)         Second vector of integers      Returns     -------     confusion_matrix: ndarray of shape (a_unique_values, b_unique_values)         A confusion matrix where the value of the i,j index shows the number of times value `i` was found in vector `a`         while value `j` vas found in vector `b` wrapper for a function f that prints the time it ran.     :return: