Skip to content

Commit 39b41a3

Browse files
cls2 index
1 parent 11256f4 commit 39b41a3

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

source/classification2.Rmd

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ a single number. But prediction accuracy by itself does not tell the whole
117117
story. In particular, accuracy alone only tells us how often the classifier
118118
makes mistakes in general, but does not tell us anything about the *kinds* of
119119
mistakes the classifier makes. A more comprehensive view of performance can be
120-
obtained by additionally examining the **confusion matrix**. The confusion
120+
obtained by additionally examining the **confusion matrix**. The confusion\index{confusion matrix}
121121
matrix shows how many test set labels of each type are predicted correctly and
122122
incorrectly, which gives us more detail about the kinds of mistakes the
123123
classifier tends to make. Table \@ref(tab:confusion-matrix) shows an example
@@ -148,7 +148,8 @@ disastrous error, since it may lead to a patient who requires treatment not rece
148148
Since we are particularly interested in identifying malignant cases, this
149149
classifier would likely be unacceptable even with an accuracy of 89%.
150150

151-
Focusing more on one label than the other is
151+
Focusing more on one label than the other
152+
is\index{positive label}\index{negative label}\index{true positive}\index{false positive}\index{true negative}\index{false negative}
152153
common in classification problems. In such cases, we typically refer to the label we are more
153154
interested in identifying as the *positive* label, and the other as the
154155
*negative* label. In the tumor example, we would refer to malignant
@@ -166,7 +167,7 @@ therefore, 100% accuracy). However, classifiers in practice will almost always
166167
make some errors. So you should think about which kinds of error are most
167168
important in your application, and use the confusion matrix to quantify and
168169
report them. Two commonly used metrics that we can compute using the confusion
169-
matrix are the **precision** and **recall** of the classifier. These are often
170+
matrix are the **precision** and **recall** of the classifier.\index{precision}\index{recall} These are often
170171
reported together with accuracy. *Precision* quantifies how many of the
171172
positive predictions the classifier made were actually positive. Intuitively,
172173
we would like a classifier to have a *high* precision: for a classifier with
@@ -582,7 +583,7 @@ We now know that the classifier was `r round(100*cancer_acc_1$.estimate, 0)`% ac
582583
on the test data set, and had a precision of `r round(100*cancer_prec_1$.estimate, 0)`% and a recall of `r round(100*cancer_rec_1$.estimate, 0)`%.
583584
That sounds pretty good! Wait, *is* it good? Or do we need something higher?
584585

585-
In general, a *good* value for accuracy (as well as precision and recall, if applicable)\index{accuracy!assessment}
586+
In general, a *good* value for accuracy (as well as precision and recall, if applicable)\index{accuracy!assessment}\index{precision!assessment}\index{recall!assessment}
586587
depends on the application; you must critically analyze your accuracy in the context of the problem
587588
you are solving. For example, if we were building a classifier for a kind of tumor that is benign 99%
588589
of the time, a classifier with 99% accuracy is not terribly impressive (just always guess benign!).
@@ -845,7 +846,7 @@ The `collect_metrics`\index{tidymodels!collect\_metrics}\index{cross-validation!
845846
of the classifier's validation accuracy across the folds. You will find results
846847
related to the accuracy in the row with `accuracy` listed under the `.metric` column.
847848
You should consider the mean (`mean`) to be the estimated accuracy, while the standard
848-
error (`std_err`) is a measure of how uncertain we are in the mean value. A detailed treatment of this
849+
error (`std_err`) is\index{standard error}\index{sem|see{standard error}} a measure of how uncertain we are in the mean value. A detailed treatment of this
849850
is beyond the scope of this chapter; but roughly, if your estimated mean is `r round(filter(collect_metrics(knn_fit), .metric == "accuracy")$mean,2)` and standard
850851
error is `r round(filter(collect_metrics(knn_fit), .metric == "accuracy")$std_err,2)`, you can expect the *true* average accuracy of the
851852
classifier to be somewhere roughly between `r (round(filter(collect_metrics(knn_fit), .metric == "accuracy")$mean,2) - round(filter(collect_metrics(knn_fit), .metric == "accuracy")$std_err,2))*100`% and `r (round(filter(collect_metrics(knn_fit), .metric == "accuracy")$mean,2) + round(filter(collect_metrics(knn_fit), .metric == "accuracy")$std_err,2))*100`% (although it may
@@ -859,7 +860,7 @@ knn_fit |>
859860
collect_metrics()
860861
```
861862

862-
We can choose any number of folds, and typically the more we use the better our
863+
We can choose any number of folds,\index{cross-validation!folds} and typically the more we use the better our
863864
accuracy estimate will be (lower standard error). However, we are limited
864865
by computational power: the
865866
more folds we choose, the more computation it takes, and hence the more time
@@ -1180,6 +1181,7 @@ knn_fit
11801181
Then to make predictions and assess the estimated accuracy of the best model on the test data, we use the
11811182
`predict` and `metrics` functions as we did earlier in the chapter. We can then pass those predictions to
11821183
the `precision`, `recall`, and `conf_mat` functions to assess the estimated precision and recall, and print a confusion matrix.
1184+
\index{predict}\index{precision}\index{recall}\index{conf\_mat}
11831185

11841186
```{r 06-predictions-after-tuning, message = FALSE, warning = FALSE}
11851187
cancer_test_predictions <- predict(knn_fit, cancer_test) |>
@@ -1393,24 +1395,8 @@ accs <- accs |> unlist()
13931395
nghbrs <- nghbrs |> unlist()
13941396
fixedaccs <- fixedaccs |> unlist()
13951397
1396-
## get accuracy if we always just guess the most frequent label
1397-
#base_acc <- cancer_irrelevant |>
1398-
# group_by(Class) |>
1399-
# summarize(n = n()) |>
1400-
# mutate(frac = n/sum(n)) |>
1401-
# summarize(mx = max(frac)) |>
1402-
# select(mx)
1403-
#base_acc <- base_acc$mx |> unlist()
1404-
14051398
# plot
14061399
res <- tibble(ks = ks, accs = accs, fixedaccs = fixedaccs, nghbrs = nghbrs)
1407-
#res <- res |> mutate(base_acc = base_acc)
1408-
#plt_irrelevant_accuracies <- res |>
1409-
# ggplot() +
1410-
# geom_line(mapping = aes(x=ks, y=accs, linetype="Tuned K-NN")) +
1411-
# geom_hline(data=res, mapping=aes(yintercept=base_acc, linetype="Always Predict Benign")) +
1412-
# labs(x = "Number of Irrelevant Predictors", y = "Model Accuracy Estimate") +
1413-
# scale_linetype_manual(name="Method", values = c("dashed", "solid"))
14141400
14151401
plt_irrelevant_accuracies <- ggplot(res) +
14161402
geom_line(mapping = aes(x=ks, y=accs)) +
@@ -1533,7 +1519,7 @@ Therefore we will continue the rest of this section using forward selection.
15331519
15341520
### Forward selection in R
15351521

1536-
We now turn to implementing forward selection in R.
1522+
We now turn to implementing forward selection in R.\index{variable selection!implementation}
15371523
Unfortunately there is no built-in way to do this using the `tidymodels` framework,
15381524
so we will have to code it ourselves. First we will use the `select` function to extract a smaller set of predictors
15391525
to work with in this illustrative example&mdash;`Smoothness`, `Concavity`, `Perimeter`, `Irrelevant1`, `Irrelevant2`, and `Irrelevant3`&mdash;as

0 commit comments

Comments
 (0)