-
Notifications
You must be signed in to change notification settings - Fork 10
Use cases #86
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
base: gh-pages
Are you sure you want to change the base?
Use cases #86
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # Use Cases | ||
|
|
||
| ## Classification | ||
|
|
||
| One popular example regarding a classification task is the "Titanic" showcase. We have different passenger information - like name, age or fare - available with the aim to predict which kind of people would have survived the titanic sinking. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Titanic |
||
|
|
||
| Therefore we load the titanic dataset and other libraries that are needed for this use case. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. libraries -> packages |
||
|
|
||
| ```{r, results='hide', message=FALSE, warning=FALSE} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are all those options necessary? |
||
| library(titanic) | ||
| library(mlr) | ||
| library(BBmisc) | ||
| ``` | ||
|
|
||
|
|
||
| ```{r} | ||
| data = titanic_train | ||
| head(data) | ||
| ``` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is wrong with titanic_train?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using this data set for training and testing and thought that "data" is not so confusing than titanic_train. |
||
|
|
||
| Our aim - as mentioned before - is to predict which kind of people would have survided. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. survived |
||
|
|
||
| Therefore we will work off the following steps: | ||
|
|
||
| * preprocessing, [here](http://mlr-org.github.io/mlr-tutorial/devel/html/preproc/index.html) and [here](http://mlr-org.github.io/mlr-tutorial/devel/html/impute/index.html) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I overlooked this during my first review. If you want to link to other tutorial pages please use e.g. |
||
| * create a task,[here](http://mlr-org.github.io/mlr-tutorial/devel/html/task/index.html) | ||
| * provide a learner, [here](http://mlr-org.github.io/mlr-tutorial/devel/html/learner/index.html) | ||
| * train the model, [here](http://mlr-org.github.io/mlr-tutorial/devel/html/train/index.html) | ||
| * predict the survival chance, [here](http://mlr-org.github.io/mlr-tutorial/devel/html/predict/index.html) | ||
| * validate the model,[here](http://mlr-org.github.io/mlr-tutorial/devel/html/performance/index.html) and [here](http://mlr-org.github.io/mlr-tutorial/devel/html/resample/index.html) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general I like this list. |
||
| #### Preprocessing | ||
|
|
||
| The data set is corrected regarding their data types. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would show beforehand that they are bad types and say, "Hey we need to fix this" |
||
|
|
||
| ```{r} | ||
| data[, c("Survived", "Pclass", "Sex", "SibSp", "Embarked")] = lapply(data[, c("Survived", "Pclass", "Sex", "SibSp", "Embarked")], as.factor) | ||
| ``` | ||
|
|
||
| Next, unuseful columns will be dropped. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unuseful -> useless |
||
|
|
||
| ```{r} | ||
| data = dropNamed(data, c("Cabin","PassengerId", "Ticket", "Name")) | ||
| ``` | ||
|
|
||
| And missing values will be imputed, in this case Age and Fare. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add a half sentence to say which imputation methods you use. |
||
|
|
||
| ```{r} | ||
| data$Embarked[data$Embarked == ""] = NA | ||
| data$Embarked = droplevels(data$Embarked) | ||
| data = impute(data, cols = list(Age = imputeMedian(), Fare = imputeMedian(), Embarked = imputeMode())) | ||
| data = data$data | ||
| ``` | ||
|
|
||
| ### Create a task | ||
|
|
||
| In the "task" the data set and the target column is specified. People who survived are labelled with "1". | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the first sentence in l. 57 you kind of assume that the reader has some knowledge about what the mlr Tasks are about. Please say what you are actually doing here, sth. like: "Let's first define our learning problem. To this end, we need to provide the data and the name of the target column we are going to predict..." |
||
|
|
||
| ```{r} | ||
| task = makeClassifTask(data = data, target = "Survived", positive = "1") | ||
| ``` | ||
|
|
||
| ### Define a learner | ||
|
|
||
| A classification learner is selected. You can find an overview of all learners [here](http://mlr-org.github.io/mlr-tutorial/devel/html/integrated_learners/index.html) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| ```{r} | ||
| lrn = makeLearner("classif.randomForest", predict.type = "prob") | ||
| ``` | ||
|
|
||
| ### Fit the model | ||
|
|
||
| To fit the model - and afterwards predict - the data set is split into a training and a test data set. | ||
|
|
||
| ```{r} | ||
| n = getTaskSize(task) | ||
| trainSet = seq(1, n, by = 2) | ||
| testSet = seq(2, n, by = 2) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use random train and test sets here. |
||
| ``` | ||
|
|
||
| ```{r} | ||
| mod = train(learner = lrn, task = task, subset = trainSet) | ||
| ``` | ||
|
|
||
| ### Predict | ||
|
|
||
| Predicting the target values for new observations is implemented the same way as most of the other predict methods in R. In general, all you need to do is call predict on the object returned by train and pass the data you want predictions for. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would leave out all technical information, i.e. remove the first sentence in l. 87 completely and make the second sentence more specific (in the same spirit as my suggestions above). |
||
|
|
||
| ```{r} | ||
| pred = predict(mod, task, subset = testSet) | ||
| ``` | ||
|
|
||
| The quality of the predictions of a model in mlr can be assessed with respect to a number of different performance measures. In order to calculate the performance measures, call performance on the object returned by predict and specify the desired performance measures. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| ```{r} | ||
| calculateConfusionMatrix(pred) | ||
| performance(pred, measures = list(acc, fpr, tpr)) | ||
| df = generateThreshVsPerfData(pred, list(fpr, tpr, acc)) | ||
| plotThreshVsPerf(df) | ||
| plotROCCurves(df) | ||
| ``` | ||
|
|
||
| ### Extension of the original use case | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This header is too meta. Please make this more specific. You are making predictions on an additional test set, right? |
||
|
|
||
| As you might have seen the titanic library also provides a second dataset. | ||
|
|
||
| ```{r} | ||
| test = titanic_test | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would keep the name titanic_test |
||
| head(test) | ||
| ``` | ||
|
|
||
| This one does not contain any survival information, but we now can use our fitted model and predict the survival probability for this data set. | ||
|
|
||
| The same preprocessing steps - as for the "data" data set - have to be applied | ||
|
|
||
| ```{r} | ||
| test[, c("Pclass", "Sex", "SibSp", "Embarked")] = lapply(test[, c("Pclass", "Sex", "SibSp", "Embarked")], as.factor) | ||
|
|
||
| test = dropNamed(test, c("Cabin","PassengerId", "Ticket", "Name")) | ||
|
|
||
| test = impute(test, cols = list(Age = imputeMedian(), Fare = imputeMedian())) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not totally happy about how you are doing the imputation.
I think the cleanest way would be imputation on the train samples and reimputation (using the estimated means and medians from the train samples) on the test samples, respectively. This is easy to do with an imputation wrapper, but the least beginner-friendly variant. Therefore, I would stick to approach 2., but use it consistently.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Julia,
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe what Julia is saying is both
I believe you can use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, sorry for the late reply.
To better explain what I mean:
The first thing I was trying to say is that your example is not consistent w.r.t. the order of What Steve is saying is that the order in Situation 1 is usually discouraged because you are using information from the test sample (by calculating mean and median) for training, which might lead to overoptimistic performance values.
I thought about it a little more and now think it would be easiest to use the imputation wrapper. Let me know if you need help.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EDIT: A more beginne-friendly variant would be (if it's possible)
|
||
| test = test$data | ||
|
|
||
| summarizeColumns(test) | ||
| ``` | ||
|
|
||
| You can use the task and learner that you have already created. | ||
|
|
||
| ```{r} | ||
| task | ||
| lrn | ||
| ``` | ||
|
|
||
| The training step will be different now. We don't use a subset to fit the model, but use all data. | ||
|
|
||
| ```{r} | ||
| mod = train(learner = lrn, task = task) | ||
| ``` | ||
|
|
||
| For the prediction part, we will use the new test data set. | ||
|
|
||
| ```{r} | ||
| pred = predict(mod, newdata = test) | ||
| pred | ||
| ``` | ||
|
|
||
|
|
||
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.
I would remove "#Use Cases" and think of a more specific title like "# Classification: Predicting Survival on the Titanic"