示例#1
0
def preprocessor_apply_to_train(preproc, X):
    """
    Apply Preprocessor to training data and memorize parameters

    preproc is initially a struct with the following fields [default]

    standardize_X - if True, makes columns of X zero mean and unit var. [True]
    rescale_X - if True, scale columns of X to lie in [-1, +1] [False]
    kernel_fn - if not None, apply kernel fn to X  default [None]

    The returned preproc object has several more fields added to it,
    which are used by  preprocessor_apply_to_test
    """

    # Set defaults
    try:
        preproc.standardize_X
    except AttributeError:
        preproc.standardize_X = True
    try:
        preproc.rescale_X
    except AttributeError:
        preproc.rescale_X = False
    try:
        preproc.kernel_fn
    except AttributeError:
        preproc.kernel_fn = None
    try:
        preproc.poly
    except AttributeError:
        preproc.poly = None
    try:
        preproc.add_ones
    except AttributeError:
        preproc.add_ones = None

    if preproc.standardize_X:
        X, preproc.Xmu = util.center_cols(X)
        X, preproc.Xstnd = util.mk_unit_variance(X)

    if preproc.rescale_X:
        try:
            preproc.Xscale
        except AttributeError:
            preproc.Xscale = [-1, 1]
        X = util.rescale_data(X, preproc.Xscale[0], preproc.Xscale[1])

    if preproc.kernel_fn is not None:
        preproc.basis = X
        X = preproc.kernel_fn(X, preproc.basis)

    if preproc.poly is not None:
        assert preproc.poly > 0, 'polynomial degree must be greater than 0'
        X = util.degexpand(X, preproc.poly, False)

    if preproc.add_ones:
        X = util.add_ones(X)

    return preproc, X
示例#2
0
def preprocessor_apply_to_test(preproc, X):
    """Transform the test data in the same way as the training data"""

    try:
        X = util.center_cols(X, preproc.Xmu)
    except AttributeError:
        pass

    try:
        X = util.mk_unit_variance(X, preproc.Xstnd)
    except AttributeError:
        pass

    try:
        X = util.rescale_data(X, preproc.Xscale[0], preproc.Xscale[1])
    except AttributeError:
        pass

    try:
        if preproc.kernel_fn is not None:
            X = preproc.kernel_fn(X, preproc.basis)
    except AttributeError:
        pass

    try:
        if preproc.poly is not None:
            X = util.degexpand(X, preproc.poly, False)
    except AttributeError:
        pass

    try:
        if preproc.add_ones:
            X = util.add_ones(X)
    except AttributeError:
        pass

    return X