08_Machine_Learning Flashcards

(5 cards)

1
Q

Front

A

Back

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you fit a decision tree in R with rpart?

A

Use rpart() with method = ‘class’ (classification) or ‘anova’ (regression).

Code:
library(rpart); library(rpart.plot)
fit <- rpart(Species ~ ., data = iris, method=’class’)
rpart.plot(fit)
predict(fit, newdata = iris, type=’class’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you run k-means clustering?

A

Use kmeans(x, centers, nstart). Scale numeric features first.

Code:
x <- scale(iris[,1:4])
set.seed(1)
km <- kmeans(x, centers = 3, nstart = 25)
table(km$cluster, iris$Species)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you train a model with caret::train()?

A

Unified interface for resampling and tuning.

Code:
library(caret)
ctrl <- trainControl(method=’cv’, number=5)
fit <- train(Species ~ ., data=iris, method=’rf’, trControl=ctrl)
confusionMatrix(fit)

Notes:
method names: ‘rf’, ‘rpart’, ‘glmnet’, etc. Use tuneGrid for hyperparameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you evaluate classification predictions?

A

Use confusionMatrix() and yardstick metrics.

Code:
library(caret)
pred <- predict(fit, newdata=iris)
confusionMatrix(pred, iris$Species)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly