Page MenuHomec4science

Classifier.py
No OneTemporary

File Metadata

Created
Sun, Feb 2, 02:46

Classifier.py

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 26 07:14:15 2023
@author: srpv
contact: vigneashwara.solairajapandiyan@empa.ch
contact: vigneashwara.pandiyan@tii.ae
The codes in this following script will be used for the publication of the following work
"Dynamics of in-situ alloying of Ti6Al4V-Fe by means of acoustic emission monitoring
supported by operando synchrotron X-ray diffraction"
@any reuse of this code should be authorized by the first owner, code author
"""
# %%
# Libraries to import
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix
import os
from sklearn import metrics
from sklearn.metrics import ConfusionMatrixDisplay
import joblib
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
# from Plots import *
# %%
def LR(folder, X_train, X_test, y_train, y_test):
model = LogisticRegression(max_iter=1000, random_state=123)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
pred_prob = model.predict_proba(X_test)
y_pred_prob = np.vstack((y_test, predictions)).transpose()
y_pred_prob = np.hstack((y_pred_prob, pred_prob))
print("LogisticRegression Accuracy:", metrics.accuracy_score(y_test, predictions))
print(classification_report(y_test, predictions))
print(confusion_matrix(y_test, predictions))
graph_name1 = 'LR'+'_without normalization w/o Opt'
graph_name2 = 'Logistic Regression'
graph_1 = 'LR'+'_Confusion_Matrix'+'_'+'No_Opt'+'.png'
graph_2 = 'LR'+'_Confusion_Matrix'+'_'+'Opt'+'.png'
titles_options = [(graph_name1, None, graph_1),
(graph_name2, 'true', graph_2)]
for title, normalize, graphname in titles_options:
plt.figure(figsize=(20, 10), dpi=400)
disp = ConfusionMatrixDisplay.from_estimator(model, X_test, y_test,
display_labels=[
'Ti64', 'Ti64_3Fe', 'Ti64_6Fe'],
cmap=plt.cm.Reds, xticks_rotation='vertical',
normalize=normalize)
plt.title(title, size=12)
plt.savefig(os.path.join(folder, graphname), bbox_inches='tight', dpi=400)
savemodel = folder+'/'+'LR'+'_model'+'.sav'
joblib.dump(model, savemodel)
return y_pred_prob, pred_prob

Event Timeline