-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.1BasicMLAlgorithims.R
More file actions
101 lines (85 loc) · 3.65 KB
/
Copy path2.1BasicMLAlgorithims.R
File metadata and controls
101 lines (85 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 1. Dataset type
library(dslabs)
library(dplyr)
library(lubridate)
library(caret)
data(reported_heights)
dat <- mutate(reported_heights, date_time = ymd_hms(time_stamp)) %>%
filter(date_time >= make_date(2016, 01, 25) & date_time < make_date(2016, 02, 1)) %>%
mutate(type = ifelse(day(date_time) == 25 & hour(date_time) == 8 & between(minute(date_time), 15, 30), "inclass","online")) %>%
select(sex, type)
y <- factor(dat$sex, c("Female", "Male"))
x <- dat$type
# Ex 1.1: What proportion of the inclass group is female?
#What proportion of the online group is female?
dat %>% group_by(type) %>% summarize(prop_female = mean(sex == "Female"))
# Ex 1.2: Assume that for each class type the students are either
#all male or all female, based on the most prevalent sex in each class type
y_hat <- ifelse(x == "online", "Male", "Female") %>%
factor(levels = levels(y))
# Report the accuracy of your prediction of sex based on type
mean(y_hat==y)
# Ex 1.3: sensitivity & specificity
sensitivity(y_hat, y)
specificity(y_hat, y)
# Ex 1.4: prevalence of females
mean(y == "Female")
# 2. Iris Dataset
library(caret)
data(iris)
iris <- iris[-which(iris$Species=='setosa'),]
y <- iris$Species
# Ex 2.1: singular feature in the dataset that yields the greatest
# overall accuracy when predicting species
# predicting virginica if greater than the cutoff and versicolor otherwise
foo <- function(x){
rangedValues <- seq(range(x)[1], range(x)[2], by=0.1)
sapply(rangedValues, function(i){
y_hat <- ifelse(x>i, 'virginica', 'versicolor')
mean(y_hat==train$Species)
})
}
predictions <- apply(train[,-5], 2, foo)
sapply(predictions, max)
# Ex 2.2: use the smart cutoff value from the training data
# to calculate overall accuracy in test data
predictions <- foo(train[,4])
rangedValues <- seq(range(train[,4])[1], range(train[,4])[2], by=0.1)
cutoffs <-rangedValues[which(predictions==max(predictions))]
y_hat <- ifelse(test[,4]>cutoffs[1], 'virginica', 'versicolor')
mean(y_hat==test$Species)
# Ex 2.3: Which feature produces the second highest accuracy?
foo <- function(x){
rangedValues <- seq(range(x)[1], range(x)[2], by=0.1)
sapply(rangedValues, function(i){
y_hat <- ifelse(x>i, 'virginica', 'versicolor')
mean(y_hat==train$Species)
})
}
predictions <- apply(train[,-5],2,foo)
sapply(predictions,max)
# Now we will perform some exploratory data analysis on the data.
plot(iris, pch=21, bg=iris$Species)
# Ex 2.4: Optimize the the cutoffs for Petal.Length and Petal.Width separately in the train dataset
set.seed(76)
test_index <- createDataPartition(y, times=1, p=0.5, list=FALSE)
test <- iris[test_index,]
train <- iris[-test_index,]
petalLengthRange <- seq(range(train$Petal.Length)[1], range(train$Petal.Length)[2],by=0.1)
petalWidthRange <- seq(range(train$Petal.Width)[1], range(train$Petal.Width)[2],by=0.1)
# Then, report the overall accuracy when applied to the test dataset
# by creating a rule that predicts virginica if Petal.Length is greater than the length cutoff AND
# Petal.Width is greater than the width cutoff, and versicolor otherwise
length_predictions <- sapply(petalLengthRange, function(i){
y_hat <- ifelse(train$Petal.Length>i, 'virginica', 'versicolor')
mean(y_hat==train$Species)
})
length_cutoff <- petalLengthRange[which.max(length_predictions)] # 4.6
width_predictions <- sapply(petalWidthRange, function(i){
y_hat <- ifelse(train$Petal.Width>i, 'virginica', 'versicolor')
mean(y_hat==train$Species)
})
width_cutoff <- petalWidthRange[which.max(width_predictions)] # 1.5
y_hat <- ifelse(test$Petal.Length>length_cutoff & test$Petal.Width>width_cutoff, 'virginica', 'versicolor')
# What is the overall accuracy for the test data now?
mean(y_hat==test$Species)