def update_classifier_graph( filename, contents, target_variable, test_size, feature_selection, num_feat, classifier, tol, C, max_depth, n_estimators, k, threshold ): t_start = time.time() # building dataframe df = build_dataframe(contents, filename) if (target_variable is not None) and (target_variable in df.keys()): # splitting dataset X, y = df.drop([target_variable], axis=1), df[target_variable] X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=test_size, random_state=1 ) numeric = X._get_numeric_data().columns non_numeric = list(set(X.columns) - set(numeric)) tol = 10 ** tol C = C / 10 clf = get_classifier(classifier, tol, C, max_depth, n_estimators, k) feature_selector = get_feature_selector(feature_selection, num_feat) preprocessor = get_preprocessor(numeric, non_numeric) pipe = Pipeline(steps=[('preprocessor', preprocessor), ('feat_selector',feature_selector), ('clf',clf)]) pipe.fit(X_train, y_train) print(feature_selector) print(clf) roc_figure = figs.serve_roc_curve(model=pipe, X_test=X_test, y_test=y_test) metric, confusion_figure = figs.serve_pie_confusion_matrix( model=pipe, X_test=X_test, y_test=y_test, threshold=threshold ) params = [ 'Accuracy', 'Precision','Recall','F1-Score','Specificity' ] t_end = time.time() print("Time: {}\n\n".format(t_end - t_start)) uri = dump_pipeline(pipe) return [[ html.Div( id="graphs-container", children=[ dcc.Loading( className="graph-wrapper", children=dcc.Graph(id="graph-line-roc-curve", figure=roc_figure), ), dcc.Loading( className="graph-wrapper", children=dcc.Graph( id="graph-pie-confusion-matrix", figure=confusion_figure ), ), dcc.Loading( className="graph-wrapper", children=dash_table.DataTable( id='table', columns=( [{'id': p, 'name': p} for p in params] ), style_header={ 'fontWeight': 'bold' }, style_cell={'textAlign' : 'center'}, data=metric, ) ), ], ), ], build_download_button(uri)] else: return [[ html.Div( id="graphs-container", children=[ dcc.Loading( className="graph-wrapper", children=html.H5("Please Upload file and Choose Target Variable..."), ) ], ), ], None]
def update_svm_graph( model, kernel, degree, C_coef, C_power, gamma_coef, gamma_power, dataset, noise, shrinking, threshold, sample_size, logreg_reg_type, logreg_C_coef, logreg_C_power, logreg_l1_ratio, mlp_layers, mlp_activation, mlp_batch_size, mlp_l2_coef, mlp_l2_pow, ): h = 0.3 # step size in the mesh # Data Pre-processing X, y = generate_data(n_samples=sample_size, dataset=dataset, noise=noise) X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42) x_min = X[:, 0].min() - 1.5 x_max = X[:, 0].max() + 1.5 y_min = X[:, 1].min() - 1.5 y_max = X[:, 1].max() + 1.5 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) if model == "SVM": C = C_coef * 10**C_power gamma = gamma_coef * 10**gamma_power if shrinking == "True": flag = True else: flag = False clf = SVC( C=C, kernel=kernel, degree=degree, gamma=gamma, shrinking=flag, probability=True, ) elif model == "LogReg": C = logreg_C_coef * 10**logreg_C_power if logreg_reg_type == "none" or logreg_reg_type == "elasticnet": solver = "saga" else: solver = "liblinear" clf = LogisticRegression(penalty=logreg_reg_type, C=C, l1_ratio=logreg_l1_ratio, solver=solver) elif model == "LDA": clf = LinearDiscriminantAnalysis() elif model == "QDA": clf = QuadraticDiscriminantAnalysis() elif model == "MLP": hidden_layers = tuple(map(int, mlp_layers.split(", "))) l2_penalty = mlp_l2_coef * 10**mlp_l2_pow clf = MLPClassifier( hidden_layer_sizes=hidden_layers, activation=mlp_activation, batch_size=mlp_batch_size, alpha=l2_penalty, ) else: raise ValueError(f"Unsupported model: {model}") # clf = DecisionTreeClassifier() # clf = MLPClassifier() clf.fit(X_train, y_train) # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, x_max]x[y_min, y_max]. # if hasattr(clf, "decision_function"): # Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) # else: Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1] prediction_figure = figs.serve_prediction_plot( model=clf, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test, Z=Z, xx=xx, yy=yy, mesh_step=h, threshold=threshold, ) roc_figure = figs.serve_roc_curve(model=clf, X_test=X_test, y_test=y_test) confusion_figure = figs.serve_pie_confusion_matrix(model=clf, X_test=X_test, y_test=y_test, Z=Z, threshold=threshold) return [ html.Div( id="svm-graph-container", children=dcc.Loading( className="graph-wrapper", children=dcc.Graph(id="graph-sklearn-svm", figure=prediction_figure), style={"display": "none"}, ), ), html.Div( id="graphs-container", children=[ dcc.Loading( className="graph-wrapper", children=dcc.Graph(id="graph-line-roc-curve", figure=roc_figure), ), dcc.Loading( className="graph-wrapper", children=dcc.Graph(id="graph-pie-confusion-matrix", figure=confusion_figure), ), ], ), ]
def update_svm_graph( kernel, degree, C_coef, C_power, gamma_coef, gamma_power, dataset, noise, shrinking, threshold, sample_size, ): t_start = time.time() h = 0.3 # step size in the mesh # Data Pre-processing # BEGINNING WORK HERE ----------------------------------------------------------- #X, y = generate_data(n_samples=sample_size, dataset=dataset, noise=noise) #ME #X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42) x_min = X.loc[:, 0].min() - 0.5 #ME x_max = X.loc[:, 0].max() + 0.5 #ME y_min = X.loc[:, 1].min() - 0.5 #ME y_max = X.loc[:, 1].max() + 0.5 #Me #x_min = X[:, 0].min() - 0.5 #original #x_max = X[:, 0].max() + 0.5 #original #y_min = X[:, 1].min() - 0.5 #original #y_max = X[:, 1].max() + 0.5 #original xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) C = C_coef * 10**C_power gamma = gamma_coef * 10**gamma_power if shrinking == "True": flag = True else: flag = False # Train SVM clf = SVC(C=C, kernel=kernel, degree=degree, gamma=gamma, shrinking=flag) clf.fit(X_train, y_train) # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, x_max]x[y_min, y_max]. if hasattr(clf, "decision_function"): Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) else: Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1] prediction_figure = figs.serve_prediction_plot( model=clf, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test, Z=Z, xx=xx, yy=yy, mesh_step=h, threshold=threshold, ) roc_figure = figs.serve_roc_curve(model=clf, X_test=X_test, y_test=y_test) confusion_figure = figs.serve_pie_confusion_matrix(model=clf, X_test=X_test, y_test=y_test, Z=Z, threshold=threshold) return [ html.Div( id="svm-graph-container", children=dcc.Loading( className="graph-wrapper", children=dcc.Graph(id="graph-sklearn-svm", figure=prediction_figure), style={"display": "none"}, ), ), html.Div( id="graphs-container", children=[ dcc.Loading( className="graph-wrapper", children=dcc.Graph(id="graph-line-roc-curve", figure=roc_figure), ), dcc.Loading( className="graph-wrapper", children=dcc.Graph(id="graph-pie-confusion-matrix", figure=confusion_figure), ), ], ), ]
def update_svm_graph(kernel, degree, C_coef, C_power, gamma_coef, gamma_power, dataset, noise, shrinking, threshold, sample_size): t_start = time.time() h = .3 # step size in the mesh # Data Pre-processing X, y = generate_data(n_samples=sample_size, dataset=dataset, noise=noise) X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = \ train_test_split(X, y, test_size=.4, random_state=42) x_min = X[:, 0].min() - .5 x_max = X[:, 0].max() + .5 y_min = X[:, 1].min() - .5 y_max = X[:, 1].max() + .5 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) C = C_coef * 10**C_power gamma = gamma_coef * 10**gamma_power # Train SVM clf = SVC(C=C, kernel=kernel, degree=degree, gamma=gamma, shrinking=shrinking) clf.fit(X_train, y_train) # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, x_max]x[y_min, y_max]. if hasattr(clf, "decision_function"): Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) else: Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1] prediction_figure = serve_prediction_plot(model=clf, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test, Z=Z, xx=xx, yy=yy, mesh_step=h, threshold=threshold) roc_figure = serve_roc_curve(model=clf, X_test=X_test, y_test=y_test) confusion_figure = serve_pie_confusion_matrix(model=clf, X_test=X_test, y_test=y_test, Z=Z, threshold=threshold) print(f"Total Time Taken: {time.time() - t_start:.3f} sec") return [ html.Div( className='three columns', style={ 'min-width': '24.5%', 'height': 'calc(100vh - 90px)', 'margin-top': '5px', # Remove possibility to select the text for better UX 'user-select': 'none', '-moz-user-select': 'none', '-webkit-user-select': 'none', '-ms-user-select': 'none' }, children=[ dcc.Graph(id='graph-line-roc-curve', style={'height': '40%'}, figure=roc_figure), dcc.Graph(id='graph-pie-confusion-matrix', figure=confusion_figure, style={'height': '60%'}) ]), html.Div(className='six columns', style={'margin-top': '5px'}, children=[ dcc.Graph(id='graph-sklearn-svm', figure=prediction_figure, style={'height': 'calc(100vh - 90px)'}) ]) ]