|
|
@ -5,23 +5,29 @@ from flask_cors import CORS, cross_origin |
|
|
|
import json |
|
|
|
import json |
|
|
|
import collections |
|
|
|
import collections |
|
|
|
import numpy as np |
|
|
|
import numpy as np |
|
|
|
from numpy import array |
|
|
|
import re |
|
|
|
|
|
|
|
from numpy import array |
|
|
|
import pandas as pd |
|
|
|
import pandas as pd |
|
|
|
import warnings |
|
|
|
import warnings |
|
|
|
import copy |
|
|
|
import copy |
|
|
|
from joblib import Memory |
|
|
|
from joblib import Memory |
|
|
|
|
|
|
|
from itertools import chain |
|
|
|
|
|
|
|
|
|
|
|
from sklearn.linear_model import LogisticRegression |
|
|
|
from sklearn.linear_model import LogisticRegression |
|
|
|
from sklearn.neighbors import KNeighborsClassifier |
|
|
|
from sklearn.neighbors import KNeighborsClassifier |
|
|
|
from sklearn.naive_bayes import GaussianNB |
|
|
|
from sklearn.naive_bayes import GaussianNB |
|
|
|
from sklearn.ensemble import RandomForestClassifier |
|
|
|
from sklearn.ensemble import RandomForestClassifier |
|
|
|
from mlxtend.classifier import StackingCVClassifier |
|
|
|
from sklearn.pipeline import make_pipeline |
|
|
|
from sklearn import model_selection |
|
|
|
from sklearn import model_selection |
|
|
|
from sklearn.model_selection import GridSearchCV |
|
|
|
from sklearn.model_selection import GridSearchCV |
|
|
|
from sklearn.manifold import MDS |
|
|
|
from sklearn.manifold import MDS |
|
|
|
|
|
|
|
from sklearn.manifold import TSNE |
|
|
|
from sklearn.metrics import classification_report |
|
|
|
from sklearn.metrics import classification_report |
|
|
|
from sklearn.preprocessing import scale |
|
|
|
from sklearn.preprocessing import scale |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from mlxtend.classifier import StackingCVClassifier |
|
|
|
|
|
|
|
from mlxtend.feature_selection import ColumnSelector |
|
|
|
|
|
|
|
|
|
|
|
# This block of code is for the connection between the server, the database, and the client (plus routing). |
|
|
|
# This block of code is for the connection between the server, the database, and the client (plus routing). |
|
|
|
|
|
|
|
|
|
|
|
# Access MongoDB |
|
|
|
# Access MongoDB |
|
|
@ -36,18 +42,65 @@ cors = CORS(app, resources={r"/data/*": {"origins": "*"}}) |
|
|
|
@cross_origin(origin='localhost',headers=['Content-Type','Authorization']) |
|
|
|
@cross_origin(origin='localhost',headers=['Content-Type','Authorization']) |
|
|
|
@app.route('/data/ServerRequest', methods=["GET", "POST"]) |
|
|
|
@app.route('/data/ServerRequest', methods=["GET", "POST"]) |
|
|
|
def RetrieveFileName(): |
|
|
|
def RetrieveFileName(): |
|
|
|
global fileName |
|
|
|
|
|
|
|
fileName = request.get_data().decode('utf8').replace("'", '"') |
|
|
|
fileName = request.get_data().decode('utf8').replace("'", '"') |
|
|
|
global featureSelection |
|
|
|
global featureSelection |
|
|
|
featureSelection = request.get_data().decode('utf8').replace("'", '"') |
|
|
|
featureSelection = request.get_data().decode('utf8').replace("'", '"') |
|
|
|
featureSelection = json.loads(featureSelection) |
|
|
|
featureSelection = json.loads(featureSelection) |
|
|
|
return jsonify(fileName) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Sent data to client |
|
|
|
|
|
|
|
@app.route('/data/ClientRequest', methods=["GET", "POST"]) |
|
|
|
|
|
|
|
def CollectionData(): |
|
|
|
|
|
|
|
global DataRawLength |
|
|
|
global DataRawLength |
|
|
|
global DataResultsRaw |
|
|
|
global DataResultsRaw |
|
|
|
|
|
|
|
global RANDOM_SEED |
|
|
|
|
|
|
|
RANDOM_SEED = 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global XData |
|
|
|
|
|
|
|
XData = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global yData |
|
|
|
|
|
|
|
yData = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global ClassifierIDsList |
|
|
|
|
|
|
|
ClassifierIDsList = '' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Initializing models |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global classifiersId |
|
|
|
|
|
|
|
classifiersId = [] |
|
|
|
|
|
|
|
global classifiersIDwithFI |
|
|
|
|
|
|
|
classifiersIDwithFI = [] |
|
|
|
|
|
|
|
global classifiersIDPlusParams |
|
|
|
|
|
|
|
classifiersIDPlusParams = [] |
|
|
|
|
|
|
|
global classifierID |
|
|
|
|
|
|
|
classifierID = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global resultsList |
|
|
|
|
|
|
|
resultsList = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global allParametersPerformancePerModel |
|
|
|
|
|
|
|
allParametersPerformancePerModel = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global all_classifiers |
|
|
|
|
|
|
|
all_classifiers = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global crossValidation |
|
|
|
|
|
|
|
crossValidation = 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global scoring |
|
|
|
|
|
|
|
#scoring = {'accuracy': 'accuracy', 'f1_macro': 'f1_weighted', 'precision': 'precision_weighted', 'recall': 'recall_weighted', 'jaccard': 'jaccard_weighted', 'neg_log_loss': 'neg_log_loss', 'r2': 'r2', 'neg_mean_absolute_error': 'neg_mean_absolute_error', 'neg_mean_absolute_error': 'neg_mean_absolute_error'} |
|
|
|
|
|
|
|
scoring = {'accuracy': 'accuracy', 'f1_macro': 'f1_weighted', 'precision': 'precision_weighted', 'recall': 'recall_weighted', 'jaccard': 'jaccard_weighted'} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global loopFeatures |
|
|
|
|
|
|
|
loopFeatures = 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global flag |
|
|
|
|
|
|
|
flag = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global yPredictProb |
|
|
|
|
|
|
|
yPredictProb = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global results |
|
|
|
|
|
|
|
results = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global target_names |
|
|
|
|
|
|
|
target_names = [] |
|
|
|
DataRawLength = -1 |
|
|
|
DataRawLength = -1 |
|
|
|
data = json.loads(fileName) |
|
|
|
data = json.loads(fileName) |
|
|
|
if data['fileName'] == 'BreastC': |
|
|
|
if data['fileName'] == 'BreastC': |
|
|
@ -62,13 +115,58 @@ def CollectionData(): |
|
|
|
item['InstanceID'] = index |
|
|
|
item['InstanceID'] = index |
|
|
|
DataResultsRaw.append(item) |
|
|
|
DataResultsRaw.append(item) |
|
|
|
DataRawLength = len(DataResultsRaw) |
|
|
|
DataRawLength = len(DataResultsRaw) |
|
|
|
json.dumps(DataResultsRaw) |
|
|
|
DataSetSelection() |
|
|
|
|
|
|
|
return 'Everything is okay' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Sent data to client |
|
|
|
|
|
|
|
@app.route('/data/ClientRequest', methods=["GET", "POST"]) |
|
|
|
|
|
|
|
def CollectionData(): |
|
|
|
|
|
|
|
json.dumps(DataResultsRaw) |
|
|
|
response = { |
|
|
|
response = { |
|
|
|
'Collection': DataResultsRaw |
|
|
|
'Collection': DataResultsRaw |
|
|
|
} |
|
|
|
} |
|
|
|
return jsonify(response) |
|
|
|
return jsonify(response) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def DataSetSelection(): |
|
|
|
|
|
|
|
DataResults = copy.deepcopy(DataResultsRaw) |
|
|
|
|
|
|
|
for dictionary in DataResultsRaw: |
|
|
|
|
|
|
|
for key in dictionary.keys(): |
|
|
|
|
|
|
|
if (key.find('*') != -1): |
|
|
|
|
|
|
|
target = key |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataResultsRaw.sort(key=lambda x: x[target], reverse=True) |
|
|
|
|
|
|
|
DataResults.sort(key=lambda x: x[target], reverse=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for dictionary in DataResults: |
|
|
|
|
|
|
|
del dictionary['_id'] |
|
|
|
|
|
|
|
del dictionary['InstanceID'] |
|
|
|
|
|
|
|
del dictionary[target] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AllTargets = [o[target] for o in DataResultsRaw] |
|
|
|
|
|
|
|
AllTargetsFloatValues = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
previous = None |
|
|
|
|
|
|
|
Class = 0 |
|
|
|
|
|
|
|
for i, value in enumerate(AllTargets): |
|
|
|
|
|
|
|
if (i == 0): |
|
|
|
|
|
|
|
previous = value |
|
|
|
|
|
|
|
target_names.append(value) |
|
|
|
|
|
|
|
if (value == previous): |
|
|
|
|
|
|
|
AllTargetsFloatValues.append(Class) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
Class = Class + 1 |
|
|
|
|
|
|
|
target_names.append(value) |
|
|
|
|
|
|
|
AllTargetsFloatValues.append(Class) |
|
|
|
|
|
|
|
previous = value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ArrayDataResults = pd.DataFrame.from_dict(DataResults) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global XData, yData, RANDOM_SEED |
|
|
|
|
|
|
|
XData, yData = ArrayDataResults, AllTargetsFloatValues |
|
|
|
|
|
|
|
warnings.simplefilter('ignore') |
|
|
|
|
|
|
|
return 'Everything is okay' |
|
|
|
|
|
|
|
|
|
|
|
# Main function |
|
|
|
# Main function |
|
|
|
if __name__ == '__main__': |
|
|
|
if __name__ == '__main__': |
|
|
@ -84,24 +182,29 @@ def catch_all(path): |
|
|
|
|
|
|
|
|
|
|
|
# This block of code is for server computations |
|
|
|
# This block of code is for server computations |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def column_index(df, query_cols): |
|
|
|
|
|
|
|
cols = df.columns.values |
|
|
|
|
|
|
|
sidx = np.argsort(cols) |
|
|
|
|
|
|
|
return sidx[np.searchsorted(cols,query_cols,sorter=sidx)].tolist() |
|
|
|
|
|
|
|
|
|
|
|
global mem |
|
|
|
global mem |
|
|
|
mem = Memory("./cache_dir") |
|
|
|
mem = Memory("./cache_dir") |
|
|
|
|
|
|
|
|
|
|
|
def GridSearch(clf, params, scoring, FI, target_names): |
|
|
|
def GridSearch(clf, params, FI): |
|
|
|
|
|
|
|
global XData |
|
|
|
|
|
|
|
global yData |
|
|
|
|
|
|
|
global scoring |
|
|
|
|
|
|
|
global target_names |
|
|
|
grid = GridSearchCV(estimator=clf, |
|
|
|
grid = GridSearchCV(estimator=clf, |
|
|
|
param_grid=params, |
|
|
|
param_grid=params, |
|
|
|
scoring=scoring, |
|
|
|
scoring=scoring, |
|
|
|
cv=5, |
|
|
|
cv=crossValidation, |
|
|
|
refit='accuracy', |
|
|
|
refit='accuracy', |
|
|
|
n_jobs = -1) |
|
|
|
n_jobs = -1) |
|
|
|
|
|
|
|
|
|
|
|
grid.fit(XData, yData) |
|
|
|
grid.fit(XData, yData) |
|
|
|
|
|
|
|
|
|
|
|
cv_results = [] |
|
|
|
cv_results = [] |
|
|
|
cv_results.append(grid.cv_results_) |
|
|
|
cv_results.append(grid.cv_results_) |
|
|
|
df_cv_results = pd.DataFrame.from_dict(cv_results) |
|
|
|
df_cv_results = pd.DataFrame.from_dict(cv_results) |
|
|
|
|
|
|
|
|
|
|
|
number_of_classifiers = len(df_cv_results.iloc[0][0]) |
|
|
|
number_of_classifiers = len(df_cv_results.iloc[0][0]) |
|
|
|
number_of_columns = len(df_cv_results.iloc[0]) |
|
|
|
number_of_columns = len(df_cv_results.iloc[0]) |
|
|
|
df_cv_results_per_item = [] |
|
|
|
df_cv_results_per_item = [] |
|
|
@ -117,8 +220,12 @@ def GridSearch(clf, params, scoring, FI, target_names): |
|
|
|
parameters = df_cv_results_classifiers['params'] |
|
|
|
parameters = df_cv_results_classifiers['params'] |
|
|
|
PerClassMetrics = [] |
|
|
|
PerClassMetrics = [] |
|
|
|
FeatureImp = [] |
|
|
|
FeatureImp = [] |
|
|
|
|
|
|
|
PerFeatureAccuracy = [] |
|
|
|
global subset |
|
|
|
global subset |
|
|
|
print(XData.columns) |
|
|
|
global loopFeatures |
|
|
|
|
|
|
|
global flag |
|
|
|
|
|
|
|
global yPredictProb |
|
|
|
|
|
|
|
counter = 0 |
|
|
|
subset = XData |
|
|
|
subset = XData |
|
|
|
for i, eachClassifierParams in enumerate(grid.cv_results_['params']): |
|
|
|
for i, eachClassifierParams in enumerate(grid.cv_results_['params']): |
|
|
|
eachClassifierParamsDictList = {} |
|
|
|
eachClassifierParamsDictList = {} |
|
|
@ -126,51 +233,52 @@ def GridSearch(clf, params, scoring, FI, target_names): |
|
|
|
Listvalue = [] |
|
|
|
Listvalue = [] |
|
|
|
Listvalue.append(value) |
|
|
|
Listvalue.append(value) |
|
|
|
eachClassifierParamsDictList[key] = Listvalue |
|
|
|
eachClassifierParamsDictList[key] = Listvalue |
|
|
|
if (FI == 1): |
|
|
|
counter = counter + 1 |
|
|
|
if (featureSelection['featureSelection'] == ''): |
|
|
|
|
|
|
|
subset = XData |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
featureSelected = [] |
|
|
|
|
|
|
|
if ((i+1) == int(''.join(x for x in featureSelection['featureSelection'][0] if x.isdigit()))): |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][2] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('petal_l') |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][5] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('petal_w') |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][8] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('sepal_l') |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][11] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('sepal_w') |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][14] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('petal_l') |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][17] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('petal_w') |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][20] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('sepal_l') |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][23] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('sepal_w') |
|
|
|
|
|
|
|
print(featureSelected) |
|
|
|
|
|
|
|
subset = XData[featureSelected] |
|
|
|
|
|
|
|
grid = GridSearchCV(estimator=clf, |
|
|
|
grid = GridSearchCV(estimator=clf, |
|
|
|
param_grid=eachClassifierParamsDictList, |
|
|
|
param_grid=eachClassifierParamsDictList, |
|
|
|
scoring=scoring, |
|
|
|
scoring=scoring, |
|
|
|
cv=5, |
|
|
|
cv=crossValidation, |
|
|
|
refit='accuracy', |
|
|
|
refit='accuracy', |
|
|
|
n_jobs = -1) |
|
|
|
n_jobs = -1) |
|
|
|
grid.fit(subset, yData) |
|
|
|
if (featureSelection['featureSelection'] == ''): |
|
|
|
|
|
|
|
subset = XData |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
featureSelected = [] |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][loopFeatures] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('petal_l') |
|
|
|
|
|
|
|
loopFeatures = loopFeatures + 3 |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][loopFeatures] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('petal_w') |
|
|
|
|
|
|
|
loopFeatures = loopFeatures + 3 |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][loopFeatures] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('sepal_l') |
|
|
|
|
|
|
|
loopFeatures = loopFeatures + 3 |
|
|
|
|
|
|
|
if (int(''.join(x for x in featureSelection['featureSelection'][loopFeatures] if x.isdigit())) == 1): |
|
|
|
|
|
|
|
featureSelected.append('sepal_w') |
|
|
|
|
|
|
|
loopFeatures = loopFeatures + 3 |
|
|
|
|
|
|
|
subset = XData[featureSelected] |
|
|
|
|
|
|
|
element = (column_index(XData, featureSelected)) |
|
|
|
|
|
|
|
columns[flag] = element |
|
|
|
|
|
|
|
flag = flag + 1 |
|
|
|
|
|
|
|
grid.fit(subset, yData) |
|
|
|
|
|
|
|
if (FI == 0): |
|
|
|
|
|
|
|
n_feats = XData.shape[1] |
|
|
|
|
|
|
|
for i in range(n_feats): |
|
|
|
|
|
|
|
scores = model_selection.cross_val_score(grid.best_estimator_, XData.values[:, i].reshape(-1, 1), yData, cv=crossValidation) |
|
|
|
|
|
|
|
PerFeatureAccuracy.append(scores.mean()) |
|
|
|
|
|
|
|
|
|
|
|
yPredict = grid.predict(subset) |
|
|
|
yPredict = grid.predict(subset) |
|
|
|
|
|
|
|
yPredictProb.append(grid.predict_proba(subset)) |
|
|
|
PerClassMetrics.append(classification_report(yData, yPredict, target_names=target_names, digits=2, output_dict=True)) |
|
|
|
PerClassMetrics.append(classification_report(yData, yPredict, target_names=target_names, digits=2, output_dict=True)) |
|
|
|
if (FI == 1): |
|
|
|
if (FI == 1): |
|
|
|
X = subset.values |
|
|
|
X = subset.values |
|
|
|
Y = array(yData) |
|
|
|
Y = array(yData) |
|
|
|
FeatureImp.append(class_feature_importance(X, Y, grid.best_estimator_.feature_importances_)) |
|
|
|
FeatureImp.append(class_feature_importance(X, Y, grid.best_estimator_.feature_importances_)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FeatureImpPandas = pd.DataFrame(FeatureImp) |
|
|
|
FeatureImpPandas = pd.DataFrame(FeatureImp) |
|
|
|
PerClassMetricsPandas = pd.DataFrame(PerClassMetrics) |
|
|
|
PerClassMetricsPandas = pd.DataFrame(PerClassMetrics) |
|
|
|
|
|
|
|
PerFeatureAccuracyPandas = pd.DataFrame(PerFeatureAccuracy) |
|
|
|
return df_cv_results_classifiers, parameters, FeatureImpPandas, PerClassMetricsPandas |
|
|
|
return df_cv_results_classifiers, parameters, FeatureImpPandas, PerClassMetricsPandas, PerFeatureAccuracyPandas |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def class_feature_importance(X, Y, feature_importances): |
|
|
|
def class_feature_importance(X, Y, feature_importances): |
|
|
|
N, M = X.shape |
|
|
|
N, M = X.shape |
|
|
@ -186,89 +294,31 @@ def class_feature_importance(X, Y, feature_importances): |
|
|
|
|
|
|
|
|
|
|
|
#GridSearch = mem.cache(GridSearch) |
|
|
|
#GridSearch = mem.cache(GridSearch) |
|
|
|
|
|
|
|
|
|
|
|
def InitializeEnsemble(): |
|
|
|
def InitializeEnsemble(): |
|
|
|
DataResults = copy.deepcopy(DataResultsRaw) |
|
|
|
global resultsList |
|
|
|
for dictionary in DataResultsRaw: |
|
|
|
df_cv_results_classifiersList = [] |
|
|
|
for key in dictionary.keys(): |
|
|
|
parametersList = [] |
|
|
|
if (key.find('*') != -1): |
|
|
|
FeatureImportanceList = [] |
|
|
|
target = key |
|
|
|
PerClassMetricsList = [] |
|
|
|
continue |
|
|
|
FeatureAccuracyList = [] |
|
|
|
continue |
|
|
|
for j, result in enumerate(resultsList): |
|
|
|
|
|
|
|
df_cv_results_classifiersList.append(resultsList[j][0]) |
|
|
|
DataResultsRaw.sort(key=lambda x: x[target], reverse=True) |
|
|
|
parametersList.append(resultsList[j][1]) |
|
|
|
DataResults.sort(key=lambda x: x[target], reverse=True) |
|
|
|
FeatureImportanceList.append(resultsList[j][2]) |
|
|
|
|
|
|
|
PerClassMetricsList.append(resultsList[j][3]) |
|
|
|
for dictionary in DataResults: |
|
|
|
FeatureAccuracyList.append(resultsList[j][4]) |
|
|
|
del dictionary['_id'] |
|
|
|
|
|
|
|
del dictionary['InstanceID'] |
|
|
|
df_cv_results_classifiers = pd.concat(df_cv_results_classifiersList, ignore_index=True, sort=False) |
|
|
|
del dictionary[target] |
|
|
|
parameters = pd.concat(parametersList, ignore_index=True, sort=False) |
|
|
|
|
|
|
|
FeatureImportance = pd.concat(FeatureImportanceList, ignore_index=True, sort=False) |
|
|
|
AllTargets = [o[target] for o in DataResultsRaw] |
|
|
|
PerClassMetrics = pd.concat(PerClassMetricsList, ignore_index=True, sort=False) |
|
|
|
AllTargetsFloatValues = [] |
|
|
|
FeatureAccuracy = pd.concat(FeatureAccuracyList, ignore_index=True, sort=False) |
|
|
|
|
|
|
|
|
|
|
|
previous = None |
|
|
|
global scoring |
|
|
|
Class = 0 |
|
|
|
|
|
|
|
target_names = [] |
|
|
|
|
|
|
|
for i, value in enumerate(AllTargets): |
|
|
|
|
|
|
|
if (i == 0): |
|
|
|
|
|
|
|
previous = value |
|
|
|
|
|
|
|
target_names.append(value) |
|
|
|
|
|
|
|
if (value == previous): |
|
|
|
|
|
|
|
AllTargetsFloatValues.append(Class) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
Class = Class + 1 |
|
|
|
|
|
|
|
target_names.append(value) |
|
|
|
|
|
|
|
AllTargetsFloatValues.append(Class) |
|
|
|
|
|
|
|
previous = value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ArrayDataResults = pd.DataFrame.from_dict(DataResults) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global XData, yData, RANDOM_SEED |
|
|
|
|
|
|
|
XData, yData = ArrayDataResults, AllTargetsFloatValues |
|
|
|
|
|
|
|
warnings.simplefilter('ignore') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RANDOM_SEED = 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClassifierIDsList = '' |
|
|
|
|
|
|
|
key = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Initializing models |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#scoring = {'accuracy': 'accuracy', 'f1_macro': 'f1_weighted', 'precision': 'precision_weighted', 'recall': 'recall_weighted', 'jaccard': 'jaccard_weighted', 'neg_log_loss': 'neg_log_loss', 'r2': 'r2', 'neg_mean_absolute_error': 'neg_mean_absolute_error', 'neg_mean_absolute_error': 'neg_mean_absolute_error'} |
|
|
|
|
|
|
|
scoring = {'accuracy': 'accuracy', 'f1_macro': 'f1_weighted', 'precision': 'precision_weighted', 'recall': 'recall_weighted', 'jaccard': 'jaccard_weighted'} |
|
|
|
|
|
|
|
NumberofscoringMetrics = len(scoring) |
|
|
|
NumberofscoringMetrics = len(scoring) |
|
|
|
results = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clf = KNeighborsClassifier() |
|
|
|
|
|
|
|
params = {'n_neighbors': [1, 2, 10]} |
|
|
|
|
|
|
|
IF = 0 |
|
|
|
|
|
|
|
#params = {'n_neighbors': [1, 3, 5], |
|
|
|
|
|
|
|
# 'weights': ['uniform', 'distance'], |
|
|
|
|
|
|
|
# 'metric': ['euclidean', 'manhattan']} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results.append(GridSearch(clf, params, scoring, IF, target_names)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clf = RandomForestClassifier() |
|
|
|
|
|
|
|
params = {'n_estimators': [10, 50]} |
|
|
|
|
|
|
|
IF = 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results.append(GridSearch(clf, params, scoring, IF, target_names)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
df_cv_results_classifiers = pd.concat([results[0][0], results[1][0]], ignore_index=True, sort=False) |
|
|
|
|
|
|
|
parameters = pd.concat([results[0][1], results[1][1]], ignore_index=True, sort=False) |
|
|
|
|
|
|
|
FeatureImportance = pd.concat([results[0][2], results[1][2]], ignore_index=True, sort=False) |
|
|
|
|
|
|
|
PerClassMetrics = pd.concat([results[0][3], results[1][3]], ignore_index=True, sort=False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
classifiersIDPlusParams = [] |
|
|
|
|
|
|
|
classifierID = 0 |
|
|
|
|
|
|
|
for oneClassifier in parameters: |
|
|
|
|
|
|
|
classifiersIDPlusParams.append(classifierID) |
|
|
|
|
|
|
|
classifiersIDPlusParams.append(oneClassifier) |
|
|
|
|
|
|
|
classifierID = classifierID + 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
del df_cv_results_classifiers['params'] |
|
|
|
del df_cv_results_classifiers['params'] |
|
|
|
df_cv_results_classifiers_metrics = df_cv_results_classifiers.copy() |
|
|
|
df_cv_results_classifiers_metrics = df_cv_results_classifiers.copy() |
|
|
|
|
|
|
|
|
|
|
|
df_cv_results_classifiers_metrics = df_cv_results_classifiers_metrics.ix[:, 0:NumberofscoringMetrics+1] |
|
|
|
df_cv_results_classifiers_metrics = df_cv_results_classifiers_metrics.ix[:, 0:NumberofscoringMetrics+1] |
|
|
|
del df_cv_results_classifiers_metrics['mean_fit_time'] |
|
|
|
del df_cv_results_classifiers_metrics['mean_fit_time'] |
|
|
|
del df_cv_results_classifiers_metrics['mean_score_time'] |
|
|
|
del df_cv_results_classifiers_metrics['mean_score_time'] |
|
|
@ -279,90 +329,126 @@ def InitializeEnsemble(): |
|
|
|
for elements in row: |
|
|
|
for elements in row: |
|
|
|
rowSum = elements + rowSum |
|
|
|
rowSum = elements + rowSum |
|
|
|
sumPerClassifier.append(rowSum) |
|
|
|
sumPerClassifier.append(rowSum) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mergedPredList = zip(*yPredictProb) |
|
|
|
|
|
|
|
mergedPredListListForm = [] |
|
|
|
|
|
|
|
for el in mergedPredList: |
|
|
|
|
|
|
|
mergedPredListListForm.append(list(chain(*el))) |
|
|
|
XClassifiers = df_cv_results_classifiers_metrics |
|
|
|
XClassifiers = df_cv_results_classifiers_metrics |
|
|
|
embedding = MDS(n_components=2, random_state=RANDOM_SEED) |
|
|
|
|
|
|
|
X_transformed = embedding.fit_transform(XClassifiers).T |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
X_transformed = X_transformed.tolist() |
|
|
|
PredictionSpace = FunTsne(mergedPredListListForm) |
|
|
|
|
|
|
|
DataSpace = FunTsne(XData) |
|
|
|
|
|
|
|
ModelSpace = FunMDS(XClassifiers) |
|
|
|
|
|
|
|
global ClassifierIDsList |
|
|
|
|
|
|
|
key = 0 |
|
|
|
EnsembleModel(ClassifierIDsList, key) |
|
|
|
EnsembleModel(ClassifierIDsList, key) |
|
|
|
|
|
|
|
DataSpaceList = DataSpace.tolist() |
|
|
|
|
|
|
|
PredictionSpaceList = PredictionSpace.tolist() |
|
|
|
|
|
|
|
|
|
|
|
global ResultsforOverview |
|
|
|
global Results |
|
|
|
ResultsforOverview = [] |
|
|
|
|
|
|
|
|
|
|
|
Results = [] |
|
|
|
FeatureImportance = FeatureImportance.to_json(orient='records') |
|
|
|
FeatureImportance = FeatureImportance.to_json(orient='records') |
|
|
|
PerClassMetrics = PerClassMetrics.to_json(orient='records') |
|
|
|
PerClassMetrics = PerClassMetrics.to_json(orient='records') |
|
|
|
ResultsforOverview.append(json.dumps(sumPerClassifier)) |
|
|
|
FeatureAccuracy = FeatureAccuracy.to_json(orient='records') |
|
|
|
ResultsforOverview.append(json.dumps(X_transformed)) |
|
|
|
DataSpaceList = DataSpace.tolist() |
|
|
|
ResultsforOverview.append(json.dumps(classifiersIDPlusParams)) |
|
|
|
XDataJSON = XData.columns.tolist() |
|
|
|
ResultsforOverview.append(FeatureImportance) |
|
|
|
Results.append(json.dumps(sumPerClassifier)) # Position: 0 |
|
|
|
ResultsforOverview.append(PerClassMetrics) |
|
|
|
Results.append(json.dumps(ModelSpace)) # Position: 1 |
|
|
|
ResultsforOverview.append(json.dumps(target_names)) |
|
|
|
Results.append(json.dumps(classifiersIDPlusParams)) # Position: 2 |
|
|
|
|
|
|
|
Results.append(FeatureImportance) # Position: 3 |
|
|
|
return ResultsforOverview |
|
|
|
Results.append(PerClassMetrics) # Position: 4 |
|
|
|
|
|
|
|
Results.append(json.dumps(target_names)) # Position: 5 |
|
|
|
|
|
|
|
Results.append(FeatureAccuracy) # Position: 6 |
|
|
|
|
|
|
|
Results.append(json.dumps(XDataJSON)) # Position: 7 |
|
|
|
|
|
|
|
Results.append(json.dumps(classifiersId)) # Position: 8 |
|
|
|
|
|
|
|
Results.append(json.dumps(classifiersIDwithFI)) # Position: 9 |
|
|
|
|
|
|
|
Results.append(json.dumps(DataSpaceList)) # Position: 10 |
|
|
|
|
|
|
|
Results.append(json.dumps(PredictionSpaceList)) # Position: 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Results |
|
|
|
|
|
|
|
|
|
|
|
# Retrieve data from client |
|
|
|
# Retrieve data from client |
|
|
|
@cross_origin(origin='localhost',headers=['Content-Type','Authorization']) |
|
|
|
@cross_origin(origin='localhost',headers=['Content-Type','Authorization']) |
|
|
|
@app.route('/data/ServerRequestSelPoin', methods=["GET", "POST"]) |
|
|
|
@app.route('/data/ServerRequestSelPoin', methods=["GET", "POST"]) |
|
|
|
def RetrieveSelClassifiersID(): |
|
|
|
def RetrieveSelClassifiersID(): |
|
|
|
|
|
|
|
global ClassifierIDsList |
|
|
|
ClassifierIDsList = request.get_data().decode('utf8').replace("'", '"') |
|
|
|
ClassifierIDsList = request.get_data().decode('utf8').replace("'", '"') |
|
|
|
key = 1 |
|
|
|
key = 1 |
|
|
|
EnsembleModel(ClassifierIDsList, key) |
|
|
|
EnsembleModel(ClassifierIDsList, key) |
|
|
|
return 'Everything Okay' |
|
|
|
return 'Everything Okay' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def FunMDS (data): |
|
|
|
|
|
|
|
mds = MDS(n_components=2, random_state=RANDOM_SEED) |
|
|
|
|
|
|
|
XTransformed = mds.fit_transform(data).T |
|
|
|
|
|
|
|
XTransformed = XTransformed.tolist() |
|
|
|
|
|
|
|
return XTransformed |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def FunTsne (data): |
|
|
|
|
|
|
|
tsne = TSNE(n_components=2).fit_transform(data) |
|
|
|
|
|
|
|
tsne.shape |
|
|
|
|
|
|
|
return tsne |
|
|
|
|
|
|
|
|
|
|
|
def EnsembleModel (ClassifierIDsList, keyRetrieved): |
|
|
|
def EnsembleModel (ClassifierIDsList, keyRetrieved): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global scores |
|
|
|
|
|
|
|
scores = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global all_classifiers |
|
|
|
if (keyRetrieved == 0): |
|
|
|
if (keyRetrieved == 0): |
|
|
|
all_classifiers = [] |
|
|
|
all_classifiers = [] |
|
|
|
all_classifiers.append(KNeighborsClassifier(n_neighbors=1)) |
|
|
|
columns = [] |
|
|
|
all_classifiers.append(KNeighborsClassifier(n_neighbors=2)) |
|
|
|
columns = [XData.columns.get_loc(c) for c in XData.columns if c in XData] |
|
|
|
all_classifiers.append(KNeighborsClassifier(n_neighbors=10)) |
|
|
|
for index, eachelem in enumerate(algorithmList): |
|
|
|
all_classifiers.append(RandomForestClassifier(random_state=RANDOM_SEED, n_estimators = 1)) |
|
|
|
if (eachelem == 'KNN'): |
|
|
|
all_classifiers.append(RandomForestClassifier(random_state=RANDOM_SEED, n_estimators = 50)) |
|
|
|
for each in resultsList[index][1]: |
|
|
|
lr = LogisticRegression() |
|
|
|
all_classifiers.append(make_pipeline(ColumnSelector(cols=columns), KNeighborsClassifier().set_params(**each))) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
for each in resultsList[index][1]: |
|
|
|
|
|
|
|
all_classifiers.append(make_pipeline(ColumnSelector(cols=columns), RandomForestClassifier().set_params(**each))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lr = LogisticRegression() |
|
|
|
sclf = StackingCVClassifier(classifiers=all_classifiers, |
|
|
|
sclf = StackingCVClassifier(classifiers=all_classifiers, |
|
|
|
use_probas=True, |
|
|
|
use_probas=True, |
|
|
|
meta_classifier=lr, |
|
|
|
meta_classifier=lr, |
|
|
|
random_state=RANDOM_SEED, |
|
|
|
random_state=RANDOM_SEED, |
|
|
|
n_jobs = -1) |
|
|
|
n_jobs = -1) |
|
|
|
|
|
|
|
|
|
|
|
for clf, label in zip([sclf], |
|
|
|
for clf, label in zip([sclf], |
|
|
|
['StackingClassifierAllClassifiers']): |
|
|
|
['StackingClassifier']): |
|
|
|
|
|
|
|
|
|
|
|
scores = model_selection.cross_val_score(clf, subset, yData, |
|
|
|
|
|
|
|
cv=5, scoring='accuracy') |
|
|
|
|
|
|
|
print("Accuracy: %0.2f (+/- %0.2f) [%s]" |
|
|
|
|
|
|
|
% (scores.mean(), scores.std(), label)) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
all_classifiers = [] |
|
|
|
|
|
|
|
ClassifierIDsList = ClassifierIDsList.split('"') |
|
|
|
|
|
|
|
for loop in ClassifierIDsList: |
|
|
|
|
|
|
|
if ('ClassifierID' in loop): |
|
|
|
|
|
|
|
if (loop == 'ClassifierID: 0'): |
|
|
|
|
|
|
|
all_classifiers.append(KNeighborsClassifier(n_neighbors=1)) |
|
|
|
|
|
|
|
elif (loop == 'ClassifierID: 1'): |
|
|
|
|
|
|
|
all_classifiers.append(KNeighborsClassifier(n_neighbors=2)) |
|
|
|
|
|
|
|
elif (loop == 'ClassifierID: 2'): |
|
|
|
|
|
|
|
all_classifiers.append(KNeighborsClassifier(n_neighbors=10)) |
|
|
|
|
|
|
|
elif (loop == 'ClassifierID: 3'): |
|
|
|
|
|
|
|
all_classifiers.append(RandomForestClassifier(random_state=RANDOM_SEED, n_estimators = 1)) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
all_classifiers.append(RandomForestClassifier(random_state=RANDOM_SEED, n_estimators = 50)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lr = LogisticRegression() |
|
|
|
scores = model_selection.cross_val_score(clf, XData, yData, |
|
|
|
|
|
|
|
cv=crossValidation, scoring='accuracy') |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
all_classifiersSelection = [] |
|
|
|
|
|
|
|
ClassifierIDsList = json.loads(ClassifierIDsList) |
|
|
|
|
|
|
|
for loop in ClassifierIDsList['ClassifiersList']: |
|
|
|
|
|
|
|
temp = [int(s) for s in re.findall(r'\b\d+\b', loop)] |
|
|
|
|
|
|
|
all_classifiersSelection.append(all_classifiers[temp[0]]) |
|
|
|
|
|
|
|
|
|
|
|
sclf = StackingCVClassifier(classifiers=all_classifiers, |
|
|
|
lr = LogisticRegression() |
|
|
|
|
|
|
|
sclf = StackingCVClassifier(classifiers=all_classifiersSelection, |
|
|
|
use_probas=True, |
|
|
|
use_probas=True, |
|
|
|
meta_classifier=lr, |
|
|
|
meta_classifier=lr, |
|
|
|
random_state=RANDOM_SEED, |
|
|
|
random_state=RANDOM_SEED, |
|
|
|
n_jobs = -1) |
|
|
|
n_jobs = -1) |
|
|
|
|
|
|
|
|
|
|
|
for clf, label in zip([sclf], |
|
|
|
for clf, label in zip([sclf], |
|
|
|
['StackingClassifierSelectedClassifiers']): |
|
|
|
['StackingClassifier']): |
|
|
|
|
|
|
|
|
|
|
|
scores = model_selection.cross_val_score(clf, subset, yData, |
|
|
|
scores = model_selection.cross_val_score(clf, XData, yData, |
|
|
|
cv=5, scoring='accuracy') |
|
|
|
cv=crossValidation, scoring='accuracy') |
|
|
|
print("Accuracy: %0.2f (+/- %0.2f) [%s]" |
|
|
|
|
|
|
|
% (scores.mean(), scores.std(), label)) |
|
|
|
# Sending the final results to be visualized as a line plot |
|
|
|
|
|
|
|
@app.route('/data/SendFinalResultsBacktoVisualize', methods=["GET", "POST"]) |
|
|
|
|
|
|
|
def SendToPlotFinalResults(): |
|
|
|
|
|
|
|
FinalResults = [] |
|
|
|
|
|
|
|
FinalResults.append(scores.mean()) |
|
|
|
|
|
|
|
FinalResults.append(scores.std()) |
|
|
|
|
|
|
|
response = { |
|
|
|
|
|
|
|
'FinalResults': FinalResults |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return jsonify(response) |
|
|
|
|
|
|
|
|
|
|
|
# Sending the overview classifiers' results to be visualized as a scatterplot |
|
|
|
# Sending the overview classifiers' results to be visualized as a scatterplot |
|
|
|
@app.route('/data/PlotClassifiers', methods=["GET", "POST"]) |
|
|
|
@app.route('/data/PlotClassifiers', methods=["GET", "POST"]) |
|
|
@ -371,6 +457,118 @@ def SendToPlot(): |
|
|
|
pass |
|
|
|
pass |
|
|
|
InitializeEnsemble() |
|
|
|
InitializeEnsemble() |
|
|
|
response = { |
|
|
|
response = { |
|
|
|
'OverviewResults': ResultsforOverview |
|
|
|
'OverviewResults': Results |
|
|
|
} |
|
|
|
} |
|
|
|
return jsonify(response) |
|
|
|
return jsonify(response) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
algorithmList = [] |
|
|
|
|
|
|
|
# Retrieve data from client |
|
|
|
|
|
|
|
@cross_origin(origin='localhost',headers=['Content-Type','Authorization']) |
|
|
|
|
|
|
|
@app.route('/data/ServerRequestSelParameters', methods=["GET", "POST"]) |
|
|
|
|
|
|
|
def RetrieveModel(): |
|
|
|
|
|
|
|
global RetrievedModel |
|
|
|
|
|
|
|
RetrievedModel = request.get_data().decode('utf8').replace("'", '"') |
|
|
|
|
|
|
|
RetrievedModel = json.loads(RetrievedModel) |
|
|
|
|
|
|
|
global parametersPerformancePerModel |
|
|
|
|
|
|
|
parametersPerformancePerModel = [] |
|
|
|
|
|
|
|
global algorithms |
|
|
|
|
|
|
|
algorithms = RetrievedModel['Algorithms'] |
|
|
|
|
|
|
|
for eachAlgor in algorithms: |
|
|
|
|
|
|
|
if (eachAlgor) == 'KNN': |
|
|
|
|
|
|
|
clf = KNeighborsClassifier() |
|
|
|
|
|
|
|
params = {'n_neighbors': list(range(1, 25)), 'weights': ['uniform', 'distance'], 'algorithm': ['brute', 'kd_tree', 'ball_tree'], 'metric': ['chebyshev', 'manhattan', 'euclidean', 'minkowski']} |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
clf = RandomForestClassifier() |
|
|
|
|
|
|
|
params = {'n_estimators': list(range(80, 120)), 'criterion': ['gini', 'entropy']} |
|
|
|
|
|
|
|
GridSearchForParameters(clf, params) |
|
|
|
|
|
|
|
SendEachClassifiersPerformanceToVisualize() |
|
|
|
|
|
|
|
return 'Everything Okay' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def GridSearchForParameters(clf, params): |
|
|
|
|
|
|
|
grid = GridSearchCV(estimator=clf, |
|
|
|
|
|
|
|
param_grid=params, |
|
|
|
|
|
|
|
scoring='accuracy', |
|
|
|
|
|
|
|
cv=crossValidation, |
|
|
|
|
|
|
|
n_jobs = -1) |
|
|
|
|
|
|
|
grid.fit(XData, yData) |
|
|
|
|
|
|
|
cv_results = [] |
|
|
|
|
|
|
|
cv_results.append(grid.cv_results_) |
|
|
|
|
|
|
|
df_cv_results = pd.DataFrame.from_dict(cv_results) |
|
|
|
|
|
|
|
number_of_classifiers = len(df_cv_results.iloc[0][0]) |
|
|
|
|
|
|
|
number_of_columns = len(df_cv_results.iloc[0]) |
|
|
|
|
|
|
|
df_cv_results_per_item = [] |
|
|
|
|
|
|
|
df_cv_results_per_row = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(number_of_classifiers): |
|
|
|
|
|
|
|
df_cv_results_per_item = [] |
|
|
|
|
|
|
|
for column in df_cv_results.iloc[0]: |
|
|
|
|
|
|
|
df_cv_results_per_item.append(column[i]) |
|
|
|
|
|
|
|
df_cv_results_per_row.append(df_cv_results_per_item) |
|
|
|
|
|
|
|
df_cv_results_classifiers = pd.DataFrame(data = df_cv_results_per_row, columns= df_cv_results.columns) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global allParametersPerformancePerModel |
|
|
|
|
|
|
|
parametersPerformancePerModel = df_cv_results_classifiers[['mean_test_score','params']] |
|
|
|
|
|
|
|
parametersPerformancePerModel = parametersPerformancePerModel.to_json() |
|
|
|
|
|
|
|
allParametersPerformancePerModel.append(parametersPerformancePerModel) |
|
|
|
|
|
|
|
return 'Everything is okay' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#GridSearchForParameters = mem.cache(GridSearchForParameters) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Sending each model's results |
|
|
|
|
|
|
|
@app.route('/data/PerformanceForEachModel', methods=["GET", "POST"]) |
|
|
|
|
|
|
|
def SendEachClassifiersPerformanceToVisualize (): |
|
|
|
|
|
|
|
response = { |
|
|
|
|
|
|
|
'PerformancePerModel': allParametersPerformancePerModel |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return jsonify(response) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def Remove(duplicate): |
|
|
|
|
|
|
|
final_list = [] |
|
|
|
|
|
|
|
for num in duplicate: |
|
|
|
|
|
|
|
if num not in final_list: |
|
|
|
|
|
|
|
final_list.append(num) |
|
|
|
|
|
|
|
return final_list |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Retrieve data from client |
|
|
|
|
|
|
|
@cross_origin(origin='localhost',headers=['Content-Type','Authorization']) |
|
|
|
|
|
|
|
@app.route('/data/SendBrushedParam', methods=["GET", "POST"]) |
|
|
|
|
|
|
|
def RetrieveModelsParam(): |
|
|
|
|
|
|
|
RetrieveModelsPar = request.get_data().decode('utf8').replace("'", '"') |
|
|
|
|
|
|
|
RetrieveModelsPar = json.loads(RetrieveModelsPar) |
|
|
|
|
|
|
|
algorithm = RetrieveModelsPar['algorithm'] |
|
|
|
|
|
|
|
RetrieveModelsParPandas = pd.DataFrame(RetrieveModelsPar['brushed']) |
|
|
|
|
|
|
|
RetrieveModelsParPandas = RetrieveModelsParPandas.drop(columns=['performance']) |
|
|
|
|
|
|
|
RetrieveModelsParPandas = RetrieveModelsParPandas.to_dict(orient='list') |
|
|
|
|
|
|
|
RetrieveModels = {} |
|
|
|
|
|
|
|
for key, value in RetrieveModelsParPandas.items(): |
|
|
|
|
|
|
|
withoutDuplicates = Remove(value) |
|
|
|
|
|
|
|
RetrieveModels[key] = withoutDuplicates |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global classifierID |
|
|
|
|
|
|
|
global algorithmList |
|
|
|
|
|
|
|
results = [] |
|
|
|
|
|
|
|
print(algorithm) |
|
|
|
|
|
|
|
algorithmList.append(algorithm) |
|
|
|
|
|
|
|
if (algorithm == 'KNN'): |
|
|
|
|
|
|
|
clf = KNeighborsClassifier() |
|
|
|
|
|
|
|
params = RetrieveModels |
|
|
|
|
|
|
|
IF = 0 |
|
|
|
|
|
|
|
results.append(GridSearch(clf, params, IF)) |
|
|
|
|
|
|
|
resultsList.append(results[0]) |
|
|
|
|
|
|
|
for j, oneClassifier in enumerate(results[0][1]): |
|
|
|
|
|
|
|
classifiersId.append(classifierID) |
|
|
|
|
|
|
|
classifiersIDPlusParams.append(classifierID) |
|
|
|
|
|
|
|
classifierID = classifierID + 1 |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
clf = RandomForestClassifier() |
|
|
|
|
|
|
|
params = RetrieveModels |
|
|
|
|
|
|
|
IF = 1 |
|
|
|
|
|
|
|
results.append(GridSearch(clf, params, IF)) |
|
|
|
|
|
|
|
resultsList.append(results[0]) |
|
|
|
|
|
|
|
for oneClassifier, j in enumerate(results[0][1]): |
|
|
|
|
|
|
|
classifiersIDPlusParams.append(classifierID) |
|
|
|
|
|
|
|
classifiersIDwithFI.append(classifierID) |
|
|
|
|
|
|
|
classifierID = classifierID + 1 |
|
|
|
|
|
|
|
return 'Everything Okay' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|