Giter Site home page Giter Site logo

pkopper / machine-failures-dataset Goto Github PK

View Code? Open in Web Editor NEW

This project forked from casillasalba/machine-failures-dataset

0.0 0.0 0.0 634 KB

Introductory Project to R language using a Machine Failures Dataset.

License: GNU General Public License v3.0

R 100.00%

machine-failures-dataset's Introduction

Machine-Failures-Dataset

Project of introduction to R language using a Machine Failures Datased and programmed in RStudio Cloud.

Main Files:

  • code: notebookProyecto.R
  • dataset: DataSetFallosMaquina.csv

Read DataSet and Analysis of data

df <- read.csv(file = 'DataSetFallosMaquina.csv',sep=';')
glimpse(df)
skim(df)
knitr::kable(skim(df))

chi1

chi2

As we can see, this dataset was originally created to extract rules to determine the conditions under which a machine will fail. It includes 20 columns, where the first 19 represent the attributes of each instance (elements X) and they can be two types of data: 'in' and 'fct' (factors); and one factor value 'Failure' that can be [No,Yes] and it will be our target (element Y).

After getting information about the data, it can be seen that there are some problems with the variables:

  • Measure2 and Measure3 are integer values, but there isn't much difference in value from each other, so this leads us to think that they are factors instead of integers.
  • Seeing the p0 (minimun) and p25 values of 'Temperature', it seems it has atypical values.

We can also conclude that there aren't null values, so we will not have to remove any characteristics.

Temperature Analysis

 ggplot(df,x=1) + geom_boxplot(aes(y=Temperature))

chi3

The conclusion seeing this boxplot is that most of the values are concentrated in the p25 and p75 but there are four points that are very atypical (because they are under 60º).

So, to fix this problem, I'm going to remove the temperature values under 60º and I'll transform Measure2 and Measure3 in factors variables.

df <- df %>%
mutate(Measure2 = as.factor(Measure2), 
     Measure3 = as.factor(Measure3)) %>% 
     filter(Temperature > 50) 

We show a new Temperature's boxplot without the atypical values:

chi4

E.D.A.: Exploratory data analysis

Factor variables:

df %>%
select_if(is.factor) %>%
gather() %>%
ggplot(aes(value)) + geom_bar() + facet_wrap(~key,scales='free') +
theme(axis.text=element_text(size=6))

chi5

Integer variables:

df %>%
select_if(is.integer) %>%
gather() %>%
ggplot(aes(value)) + geom_density() + facet_wrap(~key,scales='free') +
theme(axis.text=element_text(size=6))

chi6

Correlation Analysis

df %>%
select_if(is.integer) %>%
cor() %>% 
round(digits = 2)

It is also interesting to know if there is a linear correlation between the input characteristics as we can decide whether to remove some redundant characteristics or not. To do this, we do a correlation analysis:

chi7

We can see that all the attributes provide us information. The values in the correlation matrix vary in a range of [-1,1]. However, a high negative correlation demonstrates a connection between two characteristics in the same way as a positive correlation.

Balance the dataset

Referring to the exploratory data analysis of the factor variables, we noticed that the 'Failure' variable is really imbalanced.

There are 8699 predictions where the machine doesn't fail, otherwise there are only 81 times where the machine fails. In consecuense, we take the risk that our model will 'accommodate' and predict always a 'No'.

To avoid this, we will try to achieve and ideal balanced dataset using a undersampling technique. The simplest undersampling technique involves randomly selecting from the majority class and deleting them from the dataset.

Undersampling dataset

df_nos <- df %>%
filter(Failure == 'No') %>%
sample_frac(size = 0.08)

df_sis <- df %>% filter(Failure == 'Yes')

df_red <- rbind(df_nos,df_sis)

Now we have a more balanced dataset with 80% No's and 20% Yes's.

chi8

Logistic Regression

—— Machine Failure Prediction based on Logistic Regression.

We should divide the dataset in training, validation and test datasets; but we will not do it in this case because we are using a very simple dataset.

  • We separate the variables from the dataset:

    indep <- names(df_red)[-20]
    formula <- reformulate(indep,target)

  • We use a Logistic Regression to model the dataset because we want to obtain a result between [0,1] == [No,Yes]. We use the glm functions in R:

    rl <- glm(formula,df_red,family=binomial(link='logit')) summary(rl)

chi9

We see that there are only three predictive variables, those which have one or more ''. [NOTE: '' indicates that it is a predictor variable at a 95% confidence level]

Therefore we remove all the attributes which don't contribute with predictive information.

indep_fin <- c('Temperature','Humidity','Measure9')
formula <- reformulate(indep_fin,target) #actualizamos la fórmula

rl <- glm(formula,df_red,family=binomial(link='logit'))
summary(rl)

chi10

Apply the model

We apply the model to our dataset, making a prediction taking into account the variables of the model over the total data.

df$scoring <- predict(rl,df,type='response')
head(df$scoring)

chi11

As we can see with the previous image, we have obteined initial results with a 1% probability that the machine fails. So, we determine a cut-off-point above 80%. If this condition is met, we will determine that the machine will fail.

df$prediccion <- ifelse(df$scoring > 0.8,1,0)
table(df$prediccion)

We contrast the prediction with reality.

table(df$prediccion,df$Failure)

chi12

This confusion matrix allows the visualization of the performance of the model, where the horizontal axis (columns) represent the values that the model has predicted,while the vertical axis corresponds to the real values of the labels.

machine-failures-dataset's People

Contributors

casillasalba avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.