def nlcl_model_train(n_clicks):
    c = db.get('nlcl.model_class')
    var = db.get('nlcl.model_variables')
    train = db.get('nlcl.model_train')
    if c is None and var is None and train is None:
        div = ""
    elif train is None or train < 0 or train > 100:
        div = common.error_msg('Training % should be between 0 - 100 !!')
    elif len(var) != 2:
        div = common.error_msg('Select Two Features!!')
    elif (not c is None) and (not var is None) and (not train is None):

        try:
            cols = [] + var
            cols.append(c)
            df = db.get('nlcl.data')
            df = df[cols]


            train_df, test_df = common.split_df(df, c, train)
            train_df.columns = ['X1', 'X2', 'Class']

            distinct_count_df_total = get_distinct_count_df(df, c, 'Total Count')
            distinct_count_df_train = get_distinct_count_df(train_df, c, 'Training Count')
            distinct_count_df_test = get_distinct_count_df(test_df, c, 'Testing Count')

            distinct_count_df = distinct_count_df_total.join(distinct_count_df_train.set_index('Class'), on='Class')
            distinct_count_df = distinct_count_df.join(distinct_count_df_test.set_index('Class'), on='Class')

            model = non_separable_train(train_df)
            print(model)
            summary = {}
            summary['Total Training Data'] = len(train_df)
            summary['Total Testing Data'] = len(test_df)
            summary['Total Number of Features in Dataset'] = len(var)
            summary['Model Accuracy %'] = 'TODO'
            summary['Features'] = str(var)
            summary_df = pd.DataFrame(summary.items(), columns=['Parameters', 'Value'])

            db.put('nlcl.data_train', train_df)
            db.put('nlcl.data_test', test_df)
            db.put('nlcl.model_summary', summary)
            db.put('nlcl.model_instance', model)
            #confusion_df = get_confusion_matrix(test_df, c, var, instanceOfLR)
        except Exception as e:
            traceback.print_exc()
            return common.error_msg("Exception during training model: " + str(e))

        clazz_col = c
        train_df.columns = cols
        df = train_df
        x_col = var[0]
        y_col = var[1]
        x1, y1 = get_rect_coordinates(model[0])
        x2, y2 = get_rect_coordinates(model[1])
        x3, y3 = get_rect_coordinates(model[2])
        graph_data = [
            go.Scatter(
                x=df[df[clazz_col] == clazz][x_col],
                y=df[df[clazz_col] == clazz][y_col],
                text=df[df[clazz_col] == clazz][clazz_col],
                mode='markers',
                opacity=0.8,
                marker={
                    'size': 15,
                    'line': {'width': 0.5, 'color': 'white'}
                },
                name=clazz
            ) for clazz in df[clazz_col].unique()
        ]
        graph_data.append(go.Scatter(x=x1, y=y1, text = 'Specific Rectangle', name = 'Specific Rectangle'))
        graph_data.append(go.Scatter(x=x3, y=y3, text = 'Optimal Rectangle', name = 'Optimal Rectangle'))
        graph_data.append(go.Scatter(x=x2, y=y2, text = 'Generic Rectangle', name = 'Generic Rectangle'))

        graph = dcc.Graph(
            id='nlcl-x-vs-y-rectangle',
            figure={
                'data': graph_data,
                'layout': dict(
                    title='Boundaries & Train Data Set Scatter Plot',
                    xaxis={'title': x_col},
                    yaxis={'title': y_col},
                    margin={'l': 40, 'b': 40},
                    legend={'x': 0, 'y': 1},
                    hovermode='closest'
                )
            }
        )

        div = html.Div([
            html.H2('Class Grouping in Data:'),
            dbc.Table.from_dataframe(distinct_count_df, striped=True, bordered=True, hover=True, style = common.table_style),
            html.H2('Model Parameters & Summary:'),
            dbc.Table.from_dataframe(summary_df, striped=True, bordered=True, hover=True, style = common.table_style),
            html.Br(),
            graph,
            #html.H2('Confusion Matrix (Precision & Recall):'),
            #dbc.Table.from_dataframe(confusion_df, striped=True, bordered=True, hover=True, style = common.table_style),
            html.H2('Prediction/Classification:'),
            html.P('Features to be Predicted (comma separated): ' + ','.join(var), style = {'font-size': '16px'}),
            dbc.Input(id="nlcl-prediction-data", placeholder=','.join(var), type="text"),
            html.Br(),
            dbc.Button("Predict", color="primary", id = 'nlcl-predict'),
            html.Div([], id = "nlcl-prediction"),
            html.Div([],id = "nlcl-predicted-scatter-plot")
        ])
    else:
        div = common.error_msg('Select Proper Model Parameters!!')
    return div
def sgd_model_train(n_clicks):
    c = db.get('sgd.model_class')
    var = db.get('sgd.model_variables')
    train = db.get('sgd.model_train')
    #test = db.get('sgd.model_test')
    lr = db.get('sgd.model_lr')
    epoch = db.get('sgd.model_epoch')
    #no_of_hidden_layer = db.get('sgd.no_of_hidden_layer')
    no_of_neuron = db.get('sgd.no_of_neuron')
    no_of_neuron_h2 = db.get('sgd.no_of_neuron_h2')
    layer = 1
    if not no_of_neuron_h2 is None:
        layer = 2
    db.put("sgd.model_layer", layer)
    if c is None and var is None and train is None and lr is None and epoch is None:
        div = ""
    elif train is None or train < 0 or train > 100:
        div = common.error_msg('Training % should be between 0 - 100 !!')
    elif (not c is None) and (not var is None) and (not train is None) and (
            not lr is None) and (not epoch is None):
        try:
            cols = [] + var
            cols.append(c)
            df = db.get('sgd.data')
            df = df[cols]
            ## Make DataFrame compatible for SGD API ##
            df, quantized_classes, reverse_quantized_classes = quantized_class(
                df, c)

            train_df, test_df = common.split_df(df, c, train)

            distinct_count_df_total = get_distinct_count_df(
                df, c, 'Total Count')
            distinct_count_df_train = get_distinct_count_df(
                train_df, c, 'Training Count')
            distinct_count_df_test = get_distinct_count_df(
                test_df, c, 'Testing Count')

            distinct_count_df = distinct_count_df_total.join(
                distinct_count_df_train.set_index('Class'), on='Class')
            distinct_count_df = distinct_count_df.join(
                distinct_count_df_test.set_index('Class'), on='Class')
            distinct_count_df['Class'] = distinct_count_df['Class'].map(
                reverse_quantized_classes)

            if layer == 1:
                ycap, loss_dict, cc_percentage, wc_percentage, model, yu = ann_training(
                    train_df[var], train_df[c], no_of_neuron, lr, epoch)
                ycap, cc_percentage, wc_percentage = ann_testing(
                    test_df[var], test_df[c], model, yu)
            elif layer == 2:
                ycap, loss_dict, cc_percentage, wc_percentage, model, yu = ann_training_h2(
                    train_df[var], train_df[c], no_of_neuron, no_of_neuron_h2,
                    lr, epoch)
                ycap, cc_percentage, wc_percentage = ann_testing_h2(
                    train_df[var], train_df[c], model, yu)

            summary = {}
            summary['Total Training Data'] = len(train_df)
            summary['Total Testing Data'] = len(test_df)
            summary['Total Number of Features in Dataset'] = len(var)
            summary['Total no of Layers'] = layer + 2
            summary['No of Hidden Layer'] = layer
            summary['No of Neuron in Hidden Layer 1'] = no_of_neuron
            summary['No of Neuron in Hidden Layer 2'] = no_of_neuron_h2
            summary['Activation Function'] = 'Sigmoid'
            summary['Learning rate'] = lr
            summary['Epochs'] = epoch
            summary['Model Accuracy'] = round(cc_percentage, 2)
            summary['Features'] = str(var)
            summary_df = pd.DataFrame(summary.items(),
                                      columns=['Parameters', 'Value'])

            db.put('sgd.data_train', train_df)
            db.put('sgd.data_test', test_df)
            db.put('sgd.quantized_classes', quantized_classes)
            db.put('sgd.reverse_quantized_classes', reverse_quantized_classes)
            db.put('sgd.model', model)
            db.put('sgd.model_yu', yu)
            db.put('sgd.summary', summary)
            confusion_df = get_confusion_matrix(test_df, c, var, model, yu,
                                                reverse_quantized_classes)
        except Exception as e:
            traceback.print_exc()
            return common.error_msg("Exception during training model: " +
                                    str(e))

        trace = go.Scatter(x=loss_dict['Epoch_no'],
                           y=loss_dict['Loss'],
                           line=dict(width=2, color='rgb(106, 181, 135)'))
        convergence_title = go.Layout(title='Convergence Plot',
                                      hovermode='closest',
                                      xaxis={'title': 'Epoch'},
                                      yaxis={'title': 'Loss Function'})
        convergence_fig = go.Figure(data=[trace], layout=convergence_title)

        div = html.Div([
            html.H2('Class Grouping in Data:'),
            dbc.Table.from_dataframe(distinct_count_df,
                                     striped=True,
                                     bordered=True,
                                     hover=True,
                                     style=common.table_style),
            html.H2('Model Parameters & Summary:'),
            dbc.Table.from_dataframe(summary_df,
                                     striped=True,
                                     bordered=True,
                                     hover=True,
                                     style=common.table_style),
            html.Br(),
            dcc.Graph(id='sgd-convergence-plot', figure=convergence_fig),
            html.H2('Confusion Matrix (Precision & Recall):'),
            dbc.Table.from_dataframe(confusion_df,
                                     striped=True,
                                     bordered=True,
                                     hover=True,
                                     style=common.table_style),
            html.Br(),
            html.Br()
        ])
    else:
        div = common.error_msg('Select Proper Model Parameters!!')
    return div
Ejemplo n.º 3
0
def knn_model_train(n_clicks):
    c = db.get('knn.model_class')
    var = db.get('knn.model_variables')
    train = db.get('knn.model_train')
    k = db.get('knn.distance')
    file = db.get("knn.file")
    if c is None and var is None and train is None and k is None:
        div = ""
    elif train is None or train < 0 or train > 100:
        div = common.error_msg('Training % should be between 0 - 100 !!')
    elif (not c is None) and (not var is None) and (not train is None) and (not k is None):

        try:
            cols = [] + var
            cols.append(c)
            df = db.get('knn.data')
            df = df[cols]

            train_df, test_df = common.split_df(df, c, train)
            
            distinct_count_df_total = get_distinct_count_df(df, c, 'Total Count')
            distinct_count_df_train = get_distinct_count_df(train_df, c, 'Training Count')
            distinct_count_df_test = get_distinct_count_df(test_df, c, 'Testing Count')

            distinct_count_df = distinct_count_df_total.join(distinct_count_df_train.set_index('Class'), on='Class')
            distinct_count_df = distinct_count_df.join(distinct_count_df_test.set_index('Class'), on='Class')

            train_dataset = train_df[cols].astype(str).values.tolist()
            test_dataset = test_df[cols].astype(str).values.tolist()

            result = knn_predict(train_dataset, test_dataset, k)
            cc_percentage = calculate_predict_accuracy(result)

            summary = {}
            summary['Total Training Data'] = len(train_df)
            summary['Total Testing Data'] = len(test_df)
            summary['Total Number of Features in Dataset'] = len(var)
            summary['Model Accuracy %'] = round(cc_percentage, 2)
            summary['Features'] = str(var)
            summary_df = pd.DataFrame(summary.items(), columns=['Parameters', 'Value'])

            db.put('knn.data_train', train_df)
            db.put('knn.data_test', test_df)
            db.put('knn.model_summary', summary)
            classes = df[c].unique()
            confusion_df = get_confusion_matrix(result, classes)
        except Exception as e:
            traceback.print_exc()
            return common.error_msg("Exception during training model: " + str(e))

        div = html.Div([
            html.H2('Class Grouping in Data:'),
            dbc.Table.from_dataframe(distinct_count_df, striped=True, bordered=True, hover=True, style = common.table_style),
            html.H2('Model Parameters & Summary:'),
            dbc.Table.from_dataframe(summary_df, striped=True, bordered=True, hover=True, style = common.table_style),
            html.H2('Confusion Matrix (Precision & Recall):'),
            dbc.Table.from_dataframe(confusion_df, striped=True, bordered=True, hover=True, style = common.table_style),
            html.H2('Prediction/Classification:'),
            html.P('Features to be Predicted (comma separated): ' + ','.join(var), style = {'font-size': '16px'}),
            dbc.Input(id="knn-prediction-data", placeholder=','.join(var), type="text"),
            html.Br(),
            dbc.Button("Predict", color="primary", id = 'knn-predict'),
            html.Div([], id = "knn-prediction"),
            html.Div([],id = "knn-predicted-scatter-plot")
        ])
    else:
        div = common.error_msg('Select Proper Model Parameters!!')
    return div
Ejemplo n.º 4
0
def cl_model_train(n_clicks):
    c = db.get('cl.model_class')
    var = db.get('cl.model_variables')
    train = db.get('cl.model_train')
    #test = db.get('cl.model_test')
    lr = db.get('cl.model_lr')
    epoch = db.get('cl.model_epoch')
    if c is None and var is None and train is None and lr is None and epoch is None:
        div = ""
    elif train is None or train < 0 or train > 100:
        div = common.error_msg('Training % should be between 0 - 100 !!')
    elif (not c is None) and (not var is None) and (not train is None) and (
            not lr is None) and (not epoch is None):
        #parameters = "Training Data = " + str(train) + " % Testing Data = " + str(100 - train) + " % Learning rate = " + str(lr) + " Epoch = " + str(epoch)

        try:
            cols = [] + var
            cols.append(c)
            df = db.get('cl.data')
            df = df[cols]

            train_df, test_df = common.split_df(df, c, train)

            distinct_count_df_total = get_distinct_count_df(
                df, c, 'Total Count')
            distinct_count_df_train = get_distinct_count_df(
                train_df, c, 'Training Count')
            distinct_count_df_test = get_distinct_count_df(
                test_df, c, 'Testing Count')

            distinct_count_df = distinct_count_df_total.join(
                distinct_count_df_train.set_index('Class'), on='Class')
            distinct_count_df = distinct_count_df.join(
                distinct_count_df_test.set_index('Class'), on='Class')

            instanceOfLR, summary = linearClassifier(train_df, test_df,
                                                     len(var), lr, epoch)
            summary['Features'] = str(var)
            summary_df = pd.DataFrame(summary.items(),
                                      columns=['Parameters', 'Value'])
            db.put('cl.data_train', train_df)
            db.put('cl.data_test', test_df)
            db.put('cl.model_summary', summary)
            db.put('cl.model_instance', instanceOfLR)
            confusion_df = get_confusion_matrix(test_df, c, var, instanceOfLR)
        except Exception as e:
            traceback.print_exc()
            return common.error_msg("Exception during training model: " +
                                    str(e))

        div = html.Div([
            html.H2('Class Grouping in Data:'),
            dbc.Table.from_dataframe(distinct_count_df,
                                     striped=True,
                                     bordered=True,
                                     hover=True,
                                     style=common.table_style),
            html.H2('Model Parameters & Summary:'),
            dbc.Table.from_dataframe(summary_df,
                                     striped=True,
                                     bordered=True,
                                     hover=True,
                                     style=common.table_style),
            html.H2('Confusion Matrix (Precision & Recall):'),
            dbc.Table.from_dataframe(confusion_df,
                                     striped=True,
                                     bordered=True,
                                     hover=True,
                                     style=common.table_style),
            html.H2('Prediction/Classification:'),
            html.P('Features to be Predicted (comma separated): ' +
                   ','.join(var),
                   style={'font-size': '16px'}),
            dbc.Input(id="cl-prediction-data",
                      placeholder=','.join(var),
                      type="text"),
            html.Br(),
            dbc.Button("Predict", color="primary", id='cl-predict'),
            html.Div([], id="cl-prediction"),
            html.Div([], id="cl-predicted-scatter-plot")
        ])
    else:
        div = common.error_msg('Select Proper Model Parameters!!')
    return div