generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path05_from-scratch-model.Rmd
167 lines (114 loc) · 3.01 KB
/
05_from-scratch-model.Rmd
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# From-scratch model
**Learning objectives:**
- Build a tabular model from "scratch"
## Getting Started {-}
- Titanic data from kaggle
- clean notebook from github
- Jeremy uses paperspace, I uploaded to kaggle in the titanic competition
```{r, message=FALSE}
library(tidyverse)
df <- read_csv("titanic/train.csv")
df |>
is.na() |>
colSums()
## only matches with default of read_csv
```
## Cleaning the data {-}
- Impute missing values with mode
- Discussion on imputation
+ good enough for baseline method
+ better than throwing away data
+ Jeremy "doesn't throw out rows and doesn't throw out columns"
```{r}
df <- df |>
replace_na(map(df, \(x)
ifelse(is.numeric(x),
median(x, na.rm = TRUE),
table(x) |> which.max() |> names())))
df |>
is.na() |>
colSums()
summary(df)
```
- skewed data not easily handled by regression, suggest log transform
```{r}
hist(df$Fare)
df$LogFare <- log(df$Fare + 1)
hist(df$LogFare)
```
- dummy variables for categorical variables; fastai creates an other which allows for new levels to show up in testing data
```{r, message = FALSE}
unique(df$Pclass) |> sort()
unique(df$Embarked) |> sort()
df |>
select(Sex, Pclass, Embarked) |>
mutate(Pclass = as.character(Pclass)) |>
model.matrix(object = ~.-1) |>
head()
df <- df |>
fastDummies::dummy_cols(select_columns = c("Sex", "Pclass", "Embarked"))
head(df)
```
```{r}
t_dep <- df$Survived
t_indep <- df |>
select(Age, SibSp, Parch, LogFare, Sex_female:Embarked_S) |>
as.matrix()
head(t_indep)
dim(t_indep)
```
## Setting up linear model {-}
- initialize coefficients with seed
```{r}
set.seed(442)
n_coeff <- ncol(t_indep)
coeffs <- runif(n_coeff) - 0.5
```
- broadcasting in numpy (and R): more concise, readable, optimized. I think it is more strict in python than R
```{r}
(t(t_indep)*coeffs) |>
t() |>
head()
```
- normalize columns: two most common ways is dividing by the maximum or subtract mean divide by standard deviation
```{r}
t_indep <- t(t(t_indep)/apply(t_indep,2,max))
(t(t_indep)*coeffs) |>
t() |>
head()
```
- decide on a loss function
```{r}
preds <- t_indep%*%coeffs
loss <- abs(preds - t_dep) |>
mean()
loss
```
- save useful functions for repetition
```{r}
calc_preds <- function(coeffs, indeps){
indeps%*%coeffs
}
calc_loss<- function(coeffs, indeps, deps){
abs(calc_preds(coeffs, indeps) - deps) |>
mean()
}
```
## Training the linear model {-}
- First, set up the gradient descent step
- Create validation split
- Using sigmoid for binary independent variables on final activation
- Let's experiment with the deep learning code section as suggested
## Jeremy's opinions {-}
- Generally for tabular data, feature engineering requires more thinking than using image data
- Start lazy
- use a framework
## Meeting Videos {-}
### Cohort 1 {-}
`r knitr::include_url("https://www.youtube.com/embed/URL")`
<details>
<summary> Meeting chat log </summary>
```
LOG
```
</details>