src/models/train_model.py
def build_embedding_network(X_train, X_val, cat_cols, cols_to_keep, n_num=100, n=60, d=False, verbose=False):
    """ Builds a neural network model with entity embedding for categorical variables.

    The function builds the neural network, for which it creates entity embedding for each categorical feature specified
    in cat_cols argument. Numerical features are projected ot a dense layer. Entity embedding with the numerical
    features projected to the dense layer project to 2 more layers. The final layer is the output layer.

    :param X_train: training set before preprocessing.
    :param X_val: validation set before preprocessing. The training and validation sets are need to calculate the
    dimensions of the entity embeddings.
    :param cat_cols: list with the names of categorical columns.
    :param cols_to_keep: list with the names of the columns that are selected for the model.
    :param n_num: number of neurons in the layer receiving input from the numerical feature matrix.
    :param n: number of neurons in the two hidden layers prior to the output layer.
    :param d: dropout setting for the two hidden layers before the output layer.
    :param verbose: if True prints the output with the size of the models.
    :return: neural network model.
    """
    inputs = []
    embeddings = []
    # models = []
    for categorical_var in cat_cols:
        if verbose:
            print("------------------------------------------------------------------")
            print("for categorical column ", categorical_var)
        input_cat = Input(shape=(1,))
        el_index = cols_to_keep.index(categorical_var)
        no_of_unique_cat = np.unique(np.concatenate((X_train, X_val), axis=0)[:, el_index]).size
        if verbose:
            print("number of unique cat", no_of_unique_cat)
        embedding_size = min(np.ceil((no_of_unique_cat) / 2), 50)
        embedding_size = int(embedding_size)
        if verbose:
            print("embedding_size set as ", embedding_size)
        embedding = Embedding(no_of_unique_cat + 1, embedding_size, input_length=1)(input_cat)
        embedding = Reshape(target_shape=(embedding_size,))(embedding)
        inputs.append(input_cat)
        embeddings.append(embedding)

    other_cols = [not c in cat_cols for c in cols_to_keep]
    len_other_cols = len([c for c in other_cols if c])
    if verbose:
        print("------------------------------------------------------------------")
        print('Numeric columns')
        print('Number of columns', len_other_cols)
    input_numeric = Input(shape=(len_other_cols,))
    embedding_numeric = Dense(n_num)(input_numeric)
    inputs.append(input_numeric)
    embeddings.append(embedding_numeric)

    x = Concatenate()(embeddings)
    x = Dense(n, activation='relu')(x)
    if d:
        x = Dropout(d)(x)
    x = Dense(n, activation='relu')(x)
    if d:
        x = Dropout(d)(x)
    output = Dense(1, activation='sigmoid')(x)

    model = Model(inputs, output)

    model.compile(loss='binary_crossentropy', optimizer='adam')
    return model
 Builds a neural network model with entity embedding for categorical variables.

    The function builds the neural network, for which it creates entity embedding for each categorical feature specified
    in cat_cols argument. Numerical features are projected ot a dense layer. Entity embedding with the numerical
    features projected to the dense layer project to 2 more layers. The final layer is the output layer.

    :param X_train: training set before preprocessing.
    :param X_val: validation set before preprocessing. The training and validation sets are need to calculate the
    dimensions of the entity embeddings.
    :param cat_cols: list with the names of categorical columns.
    :param cols_to_keep: list with the names of the columns that are selected for the model.
    :param n_num: number of neurons in the layer receiving input from the numerical feature matrix.
    :param n: number of neurons in the two hidden layers prior to the output layer.
    :param d: dropout setting for the two hidden layers before the output layer.
    :param verbose: if True prints the output with the size of the models.
    :return: neural network model.
    
Builds a neural network model with entity embedding for categorical variables.
The function builds the neural network, for which it creates entity embedding for each categorical feature specified
in cat_cols argument. Numerical features are projected ot a dense layer. Entity embedding with the numerical
features projected to the dense layer project to 2 more layers. The final layer is the output layer.
Builds a neural network model with entity embedding for categorical variables . The function builds the neural network for which it creates entity embedding for each categorical feature specified in cat_cols argument . Numerical features are projected ot a dense layer . Entity embedding with the numerical features projected to the dense layer project to 2 more layers . The final layer is the output layer .
def build_embedding_network(X_train, X_val, cat_cols, cols_to_keep, n_num=100, n=60, d=False, verbose=False):
    inputs = []
    embeddings = []
    for categorical_var in cat_cols:
        if verbose:
            print("------------------------------------------------------------------")
            print("for categorical column ", categorical_var)
        input_cat = Input(shape=(1,))
        el_index = cols_to_keep.index(categorical_var)
        no_of_unique_cat = np.unique(np.concatenate((X_train, X_val), axis=0)[:, el_index]).size
        if verbose:
            print("number of unique cat", no_of_unique_cat)
        embedding_size = min(np.ceil((no_of_unique_cat) / 2), 50)
        embedding_size = int(embedding_size)
        if verbose:
            print("embedding_size set as ", embedding_size)
        embedding = Embedding(no_of_unique_cat + 1, embedding_size, input_length=1)(input_cat)
        embedding = Reshape(target_shape=(embedding_size,))(embedding)
        inputs.append(input_cat)
        embeddings.append(embedding)
    other_cols = [not c in cat_cols for c in cols_to_keep]
    len_other_cols = len([c for c in other_cols if c])
    if verbose:
        print("------------------------------------------------------------------")
        print('Numeric columns')
        print('Number of columns', len_other_cols)
    input_numeric = Input(shape=(len_other_cols,))
    embedding_numeric = Dense(n_num)(input_numeric)
    inputs.append(input_numeric)
    embeddings.append(embedding_numeric)
    x = Concatenate()(embeddings)
    x = Dense(n, activation='relu')(x)
    if d:
        x = Dropout(d)(x)
    x = Dense(n, activation='relu')(x)
    if d:
        x = Dropout(d)(x)
    output = Dense(1, activation='sigmoid')(x)
    model = Model(inputs, output)
    model.compile(loss='binary_crossentropy', optimizer='adam')
    return model
def build_embedding_network ( X_train , X_val , cat_cols , cols_to_keep , n_num = 100 , n = 60 , d = False , verbose = False ) : inputs = [ ] embeddings = [ ] for categorical_var in cat_cols : if verbose : print ( "------------------------------------------------------------------" ) print ( "for categorical column " , categorical_var ) input_cat = Input ( shape = ( 1 , ) ) el_index = cols_to_keep . index ( categorical_var ) no_of_unique_cat = np . unique ( np . concatenate ( ( X_train , X_val ) , axis = 0 ) [ : , el_index ] ) . size if verbose : print ( "number of unique cat" , no_of_unique_cat ) embedding_size = min ( np . ceil ( ( no_of_unique_cat ) / 2 ) , 50 ) embedding_size = int ( embedding_size ) if verbose : print ( "embedding_size set as " , embedding_size ) embedding = Embedding ( no_of_unique_cat + 1 , embedding_size , input_length = 1 ) ( input_cat ) embedding = Reshape ( target_shape = ( embedding_size , ) ) ( embedding ) inputs . append ( input_cat ) embeddings . append ( embedding ) other_cols = [ not c in cat_cols for c in cols_to_keep ] len_other_cols = len ( [ c for c in other_cols if c ] ) if verbose : print ( "------------------------------------------------------------------" ) print ( 'Numeric columns' ) print ( 'Number of columns' , len_other_cols ) input_numeric = Input ( shape = ( len_other_cols , ) ) embedding_numeric = Dense ( n_num ) ( input_numeric ) inputs . append ( input_numeric ) embeddings . append ( embedding_numeric ) x = Concatenate ( ) ( embeddings ) x = Dense ( n , activation = 'relu' ) ( x ) if d : x = Dropout ( d ) ( x ) x = Dense ( n , activation = 'relu' ) ( x ) if d : x = Dropout ( d ) ( x ) output = Dense ( 1 , activation = 'sigmoid' ) ( x ) model = Model ( inputs , output ) model . compile ( loss = 'binary_crossentropy' , optimizer = 'adam' ) return model
Builds a neural network model with entity embedding for categorical variables.
Builds a neural network model with entity embedding for categorical variables .
""" Builds a neural network model with entity embedding for categorical variables.

    The function builds the neural network, for which it creates entity embedding for each categorical feature specified
    in cat_cols argument. Numerical features are projected ot a dense layer. Entity embedding with the numerical
    features projected to the dense layer project to 2 more layers. The final layer is the output layer.

    :param X_train: training set before preprocessing.
    :param X_val: validation set before preprocessing. The training and validation sets are need to calculate the
    dimensions of the entity embeddings.
    :param cat_cols: list with the names of categorical columns.
    :param cols_to_keep: list with the names of the columns that are selected for the model.
    :param n_num: number of neurons in the layer receiving input from the numerical feature matrix.
    :param n: number of neurons in the two hidden layers prior to the output layer.
    :param d: dropout setting for the two hidden layers before the output layer.
    :param verbose: if True prints the output with the size of the models.
    :return: neural network model.
    """ # models = []
neural network model.
neural network model .
X_train
training set before preprocessing.
training set before preprocessing .
X_val
validation set before preprocessing. The training and validation sets are need to calculate the
dimensions of the entity embeddings.
validation set before preprocessing . The training and validation sets are need to calculate the dimensions of the entity embeddings .
cat_cols
list with the names of categorical columns.
list with the names of categorical columns .
cols_to_keep
list with the names of the columns that are selected for the model.
list with the names of the columns that are selected for the model .
n_num
number of neurons in the layer receiving input from the numerical feature matrix.
number of neurons in the layer receiving input from the numerical feature matrix .
n
number of neurons in the two hidden layers prior to the output layer.
number of neurons in the two hidden layers prior to the output layer .
d
dropout setting for the two hidden layers before the output layer.
dropout setting for the two hidden layers before the output layer .
verbose
if True prints the output with the size of the models.
if True prints the output with the size of the models .