To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 16f9e927 authored by Feliks Kiszkurno's avatar Feliks Kiszkurno
Browse files

Added normalization, normalized classes, AdaBoost, KNN.

parent c62bd5c2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 26.01.2021
@author: Feliks Kiszkurno
"""
from sklearn.neighbors import KNeighborsClassifier
import slopestabilityML.plot_results
import slopestabilityML.split_dataset
import slopestabilityML.run_classi
def knn_run(test_results, random_seed):
# Split the data set
test_training, test_prediction = slopestabilityML.split_dataset(test_results.keys(), random_seed)
# Create classifier
clf = KNeighborsClassifier(n_neighbors=2)
# Train classifier
accuracy_labels, accuracy_score, accuracy_labels_training, accuracy_score_training = \
slopestabilityML.run_classification(test_training, test_prediction, test_results, clf, 'KNN')
# Plot
slopestabilityML.plot_results(accuracy_labels, accuracy_score, 'KNN_prediction')
slopestabilityML.plot_results(accuracy_labels_training, accuracy_score_training, 'KNN_training')
return accuracy_score, accuracy_labels, accuracy_score_training, accuracy_labels_training
\ No newline at end of file
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -10,7 +10,7 @@ from sklearn.linear_model import SGDClassifier
import slopestabilityML.plot_results
import slopestabilityML.split_dataset
import slopestabilityML.run_classification
import slopestabilityML.run_classi
def sgd_run(test_results, random_seed):
......
File mode changed from 100644 to 100755
......@@ -10,6 +10,7 @@ from sklearn import svm
import numpy as np
import slopestabilityML.plot_results
import slopestabilityML.split_dataset
import slopestabilityML.run_classi
import random
# TODO: for comparability with other ML methods, add option to define which test should be used for training externaly
......
......@@ -8,13 +8,14 @@ Created on 16.01.2021
from .combine_results import combine_results
from .plot_results import plot_results
from .run_all_tests import run_all_tests
from .run_every import run_all_tests
from .split_dataset import split_dataset
from .run_classification import run_classification
from .run_classi import run_classification
from .preprocess_data import preprocess_data
from .SVM.svm_run import svm_run
from .GBC.gbc_run import gbc_run
from .SGD.sgd_run import sgd_run
from .KNN.knn_run import knn_run
from .ADABOOST.adaboost_run import adaboost_run
......@@ -27,9 +27,9 @@ def combine_results(ml_results):
plt.ylabel('Correct points [%]')
plt.legend(loc='lower right')
fig.savefig(Path('results/figures/ML_summary_prediction.eps'))
# fig.savefig(Path('results/figures/ML_summary_prediction.eps'))
fig.savefig(Path('results/figures/ML_summary_prediction.png'))
fig.savefig(Path('results/figures/ML_summary_prediction.pdf'))
# fig.savefig(Path('results/figures/ML_summary_prediction.pdf'))
# Training
fig = plt.figure()
......@@ -46,6 +46,6 @@ def combine_results(ml_results):
plt.legend(loc='lower right')
fig.tight_layout()
fig.savefig(Path('results/figures/ML_summary_training.eps'), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML_summary_training.eps'), bbox_inches="tight")
fig.savefig(Path('results/figures/ML_summary_training.png'), bbox_inches="tight")
fig.savefig(Path('results/figures/ML_summary_training.pdf'), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML_summary_training.pdf'), bbox_inches="tight")
......@@ -23,8 +23,8 @@ def plot_results(accuracy_labels, accuracy_score, clf_name):
plt.title(clf_name_title+' accuracy score')
print('plot script is executed')
fig.tight_layout()
fig.savefig(Path('results/figures/ML/'+clf_name+'.eps'), bbox_inches="tight")
fig.savefig(Path('results/figures/ML/'+clf_name+'.pdf'), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML/'+clf_name+'.eps'), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML/'+clf_name+'.pdf'), bbox_inches="tight")
fig.savefig(Path('results/figures/ML/'+clf_name+'.png'), bbox_inches="tight")
return
......@@ -6,14 +6,23 @@ Created on 19.01.2021
@author: Feliks Kiszkurno
"""
import settings
import pandas as pd
def preprocess_data(data_set):
x_train = data_set.drop(['X', 'Y', 'Z', 'INM', 'CLASS'], axis='columns')
#print(x_train)
#y_train = data_set.drop(['X', 'Y', 'Z', 'INM'], axis='columns')
if settings.settings['norm'] is True:
x_train = data_set.drop(['X', 'Y', 'Z', 'INM', 'RES', 'CLASS', 'CLASSN'], axis='columns')
else:
x_train = data_set.drop(['X', 'Y', 'Z', 'INM', 'RESN', 'CLASS', 'CLASSN'], axis='columns')
if settings.settings['sen'] is False:
x_train = x_train.drop(['SEN'], axis='columns')
if settings.settings['norm_class'] is True:
y_train = pd.DataFrame(data_set['CLASSN'])
else:
y_train = pd.DataFrame(data_set['CLASS'])
return x_train, y_train
......@@ -6,6 +6,7 @@ Created on 19.01.2021
@author: Feliks Kiszkurno
"""
import settings
import slopestabilityML
import numpy as np
import pandas as pd
......@@ -21,13 +22,25 @@ from pathlib import Path
def run_classification(test_training, test_prediction, test_results, clf, clf_name):
# global settings
accuracy_score = []
accuracy_labels = []
accuracy_score_training = []
accuracy_labels_training = []
if settings.settings['norm'] is True and settings.settings['sen'] is True:
num_feat = ['RESN', 'SEN']
elif settings.settings['norm'] is False and settings.settings['sen'] is True:
num_feat = ['RES', 'SEN']
elif settings.settings['norm'] is False and settings.settings['sen'] is False:
num_feat = ['RES']
elif settings.settings['norm'] is True and settings.settings['sen'] is False:
num_feat = ['RESN']
#cat_feat = ['CLASS']
cat_lab = [0, 1]
......@@ -42,8 +55,9 @@ def run_classification(test_training, test_prediction, test_results, clf, clf_na
for test_name in test_training:
# Prepare data
print(test_name)
x_train, y_train = slopestabilityML.preprocess_data(test_results[test_name])
# print('stop')
# Train classifier
# print(type(x_train))
# print(type(y_train))
......@@ -119,9 +133,9 @@ def run_classification(test_training, test_prediction, test_results, clf, clf_na
cb[2].update_ticks()
fig.tight_layout()
fig.savefig(Path('results/figures/ML/prediction/eps/{}_ML_{}_class_res.eps'.format(test_name_pred, clf_name)), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML/prediction/eps/{}_ML_{}_class_res.eps'.format(test_name_pred, clf_name)), bbox_inches="tight")
fig.savefig(Path('results/figures/ML/prediction/png/{}_ML_{}_class_res.png'.format(test_name_pred, clf_name)), bbox_inches="tight")
fig.savefig(Path('results/figures/ML/prediction/pdf/{}_ML_{}_class_res.pdf'.format(test_name_pred, clf_name)), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML/prediction/pdf/{}_ML_{}_class_res.pdf'.format(test_name_pred, clf_name)), bbox_inches="tight")
# Evaluate result
#accuracy_score.append(len(np.where(y_pred == y_answer.to_numpy())) / len(y_answer.to_numpy()) * 100)
......
......@@ -6,13 +6,15 @@ Created on 19.01.2021
@author: Feliks Kiszkurno
"""
# import slopestabilityML.combine_results
import slopestabilityML.SVM.svm_run
import slopestabilityML.GBC.gbc_run
import slopestabilityML.SGD.sgd_run
import slopestabilityML.KNN.knn_run
import slopestabilityML.ADABOOST.adaboost_run
import slopestabilityML
# import slopestabilityML.SVM.svm_run
# import slopestabilityML.GBC.gbc_run
def run_all_tests(test_results):
random_seed = 999
......@@ -36,7 +38,19 @@ def run_all_tests(test_results):
ml_results['sgd'] = {'score': sgd_accuracy_score, 'labels': sgd_accuracy_labels,
'score_training': sgd_accuracy_score_training, 'labels_training': sgd_accuracy_labels_training}
print('Running KNN...')
knn_accuracy_score, knn_accuracy_labels, knn_accuracy_score_training, knn_accuracy_labels_training = \
slopestabilityML.KNN.knn_run(test_results, random_seed)
ml_results['KNN'] = {'score': knn_accuracy_score, 'labels': knn_accuracy_labels,
'score_training': knn_accuracy_score_training, 'labels_training': knn_accuracy_labels_training}
print('Running ADABOOST...')
ada_accuracy_score, ada_accuracy_labels, ada_accuracy_score_training, ada_accuracy_labels_training = \
slopestabilityML.ADABOOST.adaboost_run(test_results, random_seed)
ml_results['ADA'] = {'score': ada_accuracy_score, 'labels': ada_accuracy_labels,
'score_training': ada_accuracy_score_training, 'labels_training': ada_accuracy_labels_training}
slopestabilityML.combine_results(ml_results)
print('end')
print('ML classification finished')
return ml_results
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -16,6 +16,9 @@ from .model_params import model_params
from .plot_and_save import plot_and_save
from .set_labels import set_labels
from .set_diff import set_diff
from .normalize import normalize
from .assign_classes import assign_classes
from .def_classes import def_classes
from .folder_structure.create_folder_structure import create_folder_structure
from .folder_structure.create_folder_for_test import create_folder_for_test
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 21.03.2021
@author: Feliks Kiszkurno
"""
import numpy as np
import settings
import slopestabilitytools.def_classes
# It assumes, that input array is normalized
def assign_classes(data_array):
classes_def = slopestabilitytools.def_classes(settings.settings['norm_class_num'])
# classes_def = np.array([[0, 0, 0.1],
# [1, 0.1, 0.2],
# [2, 0.2, 0.3],
# [3, 0.3, 0.4],
# [4, 0.4, 0.5],
# [5, 0.5, 0.6],
# [6, 0.6, 0.7],
# [7, 0.7, 0.8],
# [8, 0.8, 0.9],
# [9, 0.9, 1.0]])
#
# classes_def = np.array([[0, 0, 0.2],
# [1, 0.2, 0.4],
# [2, 0.4, 0.6],
# [3, 0.6, 0.8],
# [4, 0.8, 1.0]])
# TODO: maybe reshaping is not necessary, as long as I supply the input as a vector. Think about and change it.
classes = np.zeros_like(data_array)
classes_shape = classes.shape
classes = classes.reshape([classes.size])
for class_pair in classes_def:
# print(class_pair)
ind = np.argwhere(np.logical_and(data_array > class_pair[1], data_array <= class_pair[2]))
# print(ind)
classes[ind] = class_pair[0]
classes_array = classes.reshape(classes_shape)
return classes_array
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment