0% found this document useful (0 votes)
3 views2 pages

Ann

The document outlines a process for preparing maintenance data for analysis using a neural network model. It includes steps for loading data, transforming continuous variables into categorical ones, performing one-hot encoding, and training a neural network to predict machine failure. The accuracy of the model is calculated and presented alongside a confusion matrix for evaluation.

Uploaded by

loicblue86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Ann

The document outlines a process for preparing maintenance data for analysis using a neural network model. It includes steps for loading data, transforming continuous variables into categorical ones, performing one-hot encoding, and training a neural network to predict machine failure. The accuracy of the model is calculated and presented alongside a confusion matrix for evaluation.

Uploaded by

loicblue86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Charger les données

Maintenance_data <- read.csv2("C:/Users/Lenovo/Documents/LICENCE3/Cours


DataScience/Maintenance_data.csv")

# Transformation des variables continues en catégorielles


Maintenance_data$Air_temperature2 <- ifelse(Maintenance_data$Air_temperature < 299,
"<299", ">299")
Maintenance_data$Process_temperature2 <-
ifelse(Maintenance_data$Process_temperature < 309, "<309", ">309")
Maintenance_data$Rotational_speed2 <- ifelse(Maintenance_data$Rotational_speed <
1524, "<1524", ">1524")
Maintenance_data$Torque2 <- ifelse(Maintenance_data$Torque < 43, "<43", ">43")
Maintenance_data$Tool_wear2 <- ifelse(Maintenance_data$Tool_wear < 120, "<120",
">120")

# Conversion des variables catégorielles en facteurs


Maintenance_data$Type <- as.factor(Maintenance_data$Type)
Maintenance_data$Air_temperature2 <- as.factor(Maintenance_data$Air_temperature2)
Maintenance_data$Process_temperature2 <-
as.factor(Maintenance_data$Process_temperature2)
Maintenance_data$Rotational_speed2 <- as.factor(Maintenance_data$Rotational_speed2)
Maintenance_data$Torque2 <- as.factor(Maintenance_data$Torque2)
Maintenance_data$Tool_wear2 <- as.factor(Maintenance_data$Tool_wear2)

# Définir une graine aléatoire pour reproductibilité


set.seed(123)

# Sélectionner 70% des lignes pour l'ensemble d'apprentissage et de test


library(caret)
train_index <- sample(nrow(Maintenance_data), size = nrow(Maintenance_data)*0.7)

training_test_data <- Maintenance_data[train_index, ]


test_data <- Maintenance_data[-train_index, ]

# Conversion de la variable cible en facteur si nécessaire


training_test_data$Machine_failure <- as.factor(training_test_data$Machine_failure)
test_data$Machine_failure <- as.factor(test_data$Machine_failure)

# Conversion de la variable cible en numérique (0 et 1)


training_test_data$Machine_failure_num <- ifelse(training_test_data$Machine_failure
== levels(training_test_data$Machine_failure)[1], 0, 1)
test_data$Machine_failure_num <- ifelse(test_data$Machine_failure ==
levels(test_data$Machine_failure)[1], 0, 1)

# Utilisation de dplyr et tidyr pour one-hot encoding


library(dplyr)
library(tidyr)

# One-hot encoding pour les variables catégorielles


train_set_encoded <- training_test_data %>%
mutate_if(is.factor, as.character) %>%
pivot_longer(cols = c("Type", "Air_temperature2", "Process_temperature2",
"Rotational_speed2", "Torque2", "Tool_wear2")) %>%
mutate(value = as.numeric(factor(value))) %>%
pivot_wider(names_from = name, values_from = value)

test_set_encoded <- test_data %>%


mutate_if(is.factor, as.character) %>%
pivot_longer(cols = c("Type", "Air_temperature2", "Process_temperature2",
"Rotational_speed2", "Torque2", "Tool_wear2")) %>%
mutate(value = as.numeric(factor(value))) %>%
pivot_wider(names_from = name, values_from = value)

# Préparation des données pour le réseau de neurones


train_data_ann <- data.frame(Machine_failure_num =
training_test_data$Machine_failure_num, train_set_encoded)
test_data_ann <- data.frame(Machine_failure_num = test_data$Machine_failure_num,
test_set_encoded)

# Installation et chargement de neuralnet


library(neuralnet)

# Formation du modèle ANN


net_model <- neuralnet(Machine_failure_num ~ ., data = train_data_ann[,
sapply(train_data_ann, is.numeric)],
hidden = c(5, 3), threshold = 0.01)

# Prédiction sur l'ensemble de test


predictions_ann <- predict(net_model, newdata = test_data_ann[,
sapply(test_data_ann, is.numeric)])

# Conversion des probabilités en classes


predicted_classes_ann <- ifelse(predictions_ann > 0.5, 1, 0)

# Calcul de l'accuracy
accuracy_ann <- sum(predicted_classes_ann == test_data$Machine_failure_num) /
nrow(test_data)
print(paste("Accuracy ANN:", round(accuracy_ann, digits = 4)))

# Matrice de confusion pour une évaluation plus complète


table(y_true = test_data$Machine_failure_num, y_pred = predicted_classes_ann)

accuracy_ann

You might also like