-
Notifications
You must be signed in to change notification settings - Fork 21
Implemented Solutions to Problems 2 and 3 #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jeffbulmer
wants to merge
2
commits into
MathieuNls:master
Choose a base branch
from
jeffbulmer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .RData | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| library(glmnet) | ||
| library(neuralnet) | ||
| library(NeuralNetTools) | ||
| library(nnet) | ||
| library(randomForest) | ||
|
|
||
| set.seed(212919156) | ||
|
|
||
| #read in the sample data | ||
| sample <- read.csv("seq/sample.csv") | ||
| sample$class = factor(sample$class) | ||
|
|
||
| #Use a random forest to generate a model to predict class | ||
| rf <- randomForest(factor(class)~., data=data.frame(sample[,-1])) | ||
|
|
||
| #Now look at the Variable Importance Plot of the Random Forest to determine relevant variables to the model | ||
| varImpPlot(rf) | ||
|
|
||
| #based on the variable importance plot, the following variables are below an arbitrary 200 MeanGiniDecrease threshold | ||
| #So, we'll disregard them and remake the data without | ||
| simdat <- sample[,-c(1,4,24,17,21,23,14,19,22,25,26,27,28,29,30,31)] | ||
|
|
||
| #now using only those, we can run a logistic regression | ||
| simlog <- glm(class ~ ., family="binomial", data=simdat) | ||
|
|
||
| #Now get the confusion matrix for both | ||
| #First the randomForest | ||
| table(predict(rf, newdata=sample[,-1]), sample$class) | ||
|
|
||
| #Then the logistic regression | ||
| table(predict(simlog, type="response") > 0.5, sample$class) | ||
|
|
||
| #obviously, the randomForest performs better for predictions, so given data about the next events, we would use the randomForest | ||
| #model to predict the class. Random Forests have built in cross-validation as well, due to Out-of-Bag estimation, | ||
| #so there's no need to additionally cross-validate this model. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| --- | ||
| title: "Seq.Rmd" | ||
| author: "Jeff B" | ||
| date: "May 24, 2019" | ||
| output: pdf_document | ||
| --- | ||
|
|
||
| ```{r setup, include=FALSE} | ||
| knitr::opts_chunk$set(echo = TRUE) | ||
| ``` | ||
|
|
||
| #Sequence Question | ||
|
|
||
| This question is a sequence classification question. Considering the large number of variables, however, we can start by examining it as a straightforward classification problem. This will allow us some of the benefits of straightforward classification, most importantly variable selection. This part was done in R. | ||
| The sequence classification portion was done in Python, and can be found in the seq folder as seq.py | ||
|
|
||
| Start by importing the necessary libraries and setting the seed for reproducibility | ||
| ```{r, echo=FALSE, warning=FALSE} | ||
| library(glmnet) | ||
| library(neuralnet) | ||
| library(NeuralNetTools) | ||
| library(nnet) | ||
| library(randomForest) | ||
|
|
||
| set.seed(212919156) | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| Next, we read in the data, and generate a RandomForest model for this data. RandomForests have built in cross-validation via Out-of-Bag estimates, so we don't need to do any extra cross-validation. We can also look at the variable importance plot here to determine the most important variables. Somewhat arbitrarily, we'll set a cutoff at 200 MeanDecreaseGini. This leaves us with 15 relevant variables. | ||
| ```{r} | ||
| sample <- read.csv("seq/sample.csv") | ||
| sample$class = factor(sample$class) | ||
|
|
||
| #Use a random forest to generate a model to predict class | ||
| rf <- randomForest(factor(class)~., data=data.frame(sample[,-1])) | ||
|
|
||
| #Now look at the Variable Importance Plot of the Random Forest to determine relevant variables to the model | ||
| varImpPlot(rf) | ||
| ``` | ||
|
|
||
| For comparison, we'll run a logistic regression using only variables above the threshold | ||
|
|
||
| ```{r} | ||
| simdat <- sample[,-c(1,4,24,17,21,23,14,19,22,25,26,27,28,29,30,31)] | ||
|
|
||
| #now using only those, we can run a logistic regression | ||
| simlog <- glm(class ~ ., family="binomial", data=simdat) | ||
| ``` | ||
|
|
||
| Now we'll get the confusion matrix for both | ||
| ```{r} | ||
| #First the randomForest | ||
| table(predict(rf, newdata=sample[,-1]), sample$class) | ||
|
|
||
| #Then the logistic regression | ||
| table(predict(simlog, type="response") > 0.5, sample$class) | ||
| ``` | ||
|
|
||
| The randomForest model perdorms better for predictions, so given data about the next event, we would use the randomForest model to predict the class. | ||
| Considering we don't have that information, this process has still been useful, since we will use only those variables we've deemed relevant in our sequence prediction problem. | ||
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ,jeff,Rupert,24.05.2019 12:01,file:///home/jeff/.config/libreoffice/4; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first column is an ID so of course, it's not relevant by itself but it can be used to link the information here with the one in the
res.csvfile. Which you neither used nor seem to looked at. Any reason?