-
Notifications
You must be signed in to change notification settings - Fork 50
/
StarterRF.R
206 lines (117 loc) · 5.66 KB
/
StarterRF.R
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
path <- #set path
setwd(path)
# Load libraries and Data -------------------------------------------------
library(data.table)
library(jsonlite)
library(purrr)
library(stringr)
library(ranger)
#### Train
train <- fromJSON("train_data.json")
train_data <- data.table(ID = unlist(names(train)))
train_data[, `:=` (genres = unlist(lapply(train, '[',1)),
titles = unlist(lapply(train, '[',2)),
cities = unlist(lapply(train, '[', 3)),
segment = unlist(lapply(train, '[',4)),
dow = unlist(lapply(train, '[',5)),
tod = unlist(lapply(train, '[', 6))
)]
#### Test
test <- fromJSON("test_data.json")
test_data <- data.table(ID = unlist(names(test)))
test_data[,`:=` (genres = unlist(lapply(test, '[',1)),
titles = unlist(lapply(test, '[',2)),
tod = unlist(lapply(test, '[', 3)),
cities = unlist(lapply(test, '[',4)),
dow = unlist(lapply(test, '[',5))
)]
#### Check Train and Test
str(train)
str(test)
### Encode target
train_data[,segment := ifelse(segment == 'neg',0,1)]
# Feature Engineering -----------------------------------------------------
## Creating new column per genres
train_data[,g1 := lapply(genres, function(k) str_extract_all(string = k, pattern = "[[:alpha:]]+"))]
train_data[,g1 := lapply(g1, unlist, use.names=F)]
uniq_genres <- unique(unlist(lapply(train_data$genres, function(k) str_extract_all(string = k, pattern = "[[:alpha:]]+"))))
length(uniq_genres)
toColumns <- function(data, variables){
for(i in variables){
data[,paste0(i,"_gen") := lapply(g1, function(x) any(match(i,x)))]
}
return (data)
}
toColumns(train_data, uniq_genres)
train_data[,g1 := NULL]
## see how it looks
head(train_data)
## encode TRUE and NA
genx <- grep(pattern = "_gen", x = colnames(train_data), value = T)
for(k in genx)
set(train_data, i = which(is.na(train_data[[k]])), j = k, value = 0)
for(k in genx)
set(train_data, i = which(train_data[[k]] == TRUE), j= k ,value = 1)
### make changes in test data
test_data[,g1 := lapply(genres, function(k) str_extract_all(string = k, pattern = "[[:alpha:]]+"))]
test_data[,g1 := lapply(g1, unlist, use.names=F)]
uniq_genres <- unique(unlist(lapply(test_data$genres, function(k) str_extract_all(string = k, pattern = "[[:alpha:]]+"))))
length(uniq_genres)
toColumns <- function(data, variables){
for(i in variables){
data[,paste0(i,"_gen") := lapply(g1, function(x) any(match(i,x)))]
}
return (data)
}
toColumns(test_data, uniq_genres)
test_data[,g1 := NULL]
genx <- grep(pattern = "_gen", x = colnames(test_data), value = T)
for(k in genx)
set(test_data, i = which(is.na(test_data[[k]])), j = k, value = 0)
for(k in genx)
set(test_data, i = which(test_data[[k]] == TRUE), j= k ,value = 1)
## sum watch time from title
train_data[,t1 := lapply(titles, function(k) strsplit(x = k, split = ","))]
train_data[,t1 := lapply(t1, unlist, use.names = F)]
train_data[,t1 := lapply(t1, function(k) gsub(pattern = ".*\\:([0-9]+)",replacement = "\\1",x = k))]
train_data[,t1 := lapply(t1, function(x) paste(x,sep = " ", collapse = "+"))]
train_data[,title_sum := lapply(t1, function(x)eval(parse(text = x)))]
train_data[,title_sum := lapply(title_sum, function(x) ifelse(is_empty(x),0,x))]
train_data[,t1 := NULL]
test_data[,t1 := lapply(titles, function(k) strsplit(x = k, split = ","))]
test_data[,t1 := lapply(t1, unlist, use.names = F)]
test_data[,t1 := lapply(t1, function(k) gsub(pattern = ".*\\:([0-9]+)",replacement = "\\1",x = k))]
test_data[,t1 := lapply(t1, function(x) paste(x,sep = " ", collapse = "+"))]
test_data[,title_sum := lapply(t1, function(x)eval(parse(text = x)))] #12 NA
test_data[,title_sum := lapply(title_sum, function(x) ifelse(is_empty(x),0,x))]
test_data[,t1 := NULL]
## create count variables
train_data[,title_count := lapply(titles, function(x) str_count(string = x, pattern = ":"))]
train_data[,genres_count := lapply(genres, function(x) str_count(string = x, pattern = ":"))]
train_data[,cities_count := lapply(cities, function(x) str_count(string = x, pattern = ":"))]
train_data[,dow_count := lapply(dow, function(x) str_count(string = x, pattern = ":"))]
train_data[,tod_count := lapply(tod, function(x) str_count(string = x, pattern = ":"))]
test_data[,title_count := lapply(titles, function(x) str_count(string = x, pattern = ":"))]
test_data[,genres_count := lapply(genres, function(x) str_count(string = x, pattern = ":"))]
test_data[,cities_count := lapply(cities, function(x) str_count(string = x, pattern = ":"))]
test_data[,dow_count := lapply(dow, function(x) str_count(string = x, pattern = ":"))]
test_data[,tod_count := lapply(tod, function(x) str_count(string = x, pattern = ":"))]
## convert list to vectors - train
pd <- names(train_data)[sapply(train_data, is.list)]
train_data[, (pd) := lapply(.SD, unlist), .SDcols = pd]
pd <- names(test_data)[sapply(test_data, is.list)]
test_data[, (pd) := lapply(.SD, unlist), .SDcols = pd]
## remove variables for modeling - train
test_id <- test_data$ID
train_data[,c('ID','genres','titles','cities','dow','tod') := NULL]
test_data[,c('ID','genres','titles','cities','dow','tod') := NULL]
# Random Forest -----------------------------------------------------------
train_data[,segment := as.factor(segment)]
rf.model <- ranger(segment ~ ., data = train_data, num.trees = 500, mtry = 7, probability = T, replace = F, seed = 1221)
rf.model
rf.predict <- predict(rf.model, data = test_data,num.trees = 500)
#predict value for 1
pred <- rf.predict$predictions[,2]
#create submission file
sub_RF <- data.table(ID = test_id, segment = pred)
fwrite(sub_RF,"starterRF.csv") #~0.79