-
Notifications
You must be signed in to change notification settings - Fork 50
/
exploration_R_script.Rmd
235 lines (154 loc) · 8.01 KB
/
exploration_R_script.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
title: "R Notebook"
output:
html_document: default
html_notebook: default
---
### Introduction
This notebook will help you understand the behaviour of Hotstar users in the given data set. Using, simple bar plots you will understand how the target variable (segment) fairs against given features.
Along with exploration, you will learn how to work with lists in R, which I believe is quite tricky!
To dive deep, check starter scripts.
### Load Libraries and Data set
```{r}
library(data.table)
library(jsonlite)
library(ggplot2)
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 <- 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))
)]
rm(train,test)
```
### Exploring Data
```{r}
# Look at data
head(train_data)
```
```{r}
## Encode Target Variable
train_data[, segment := ifelse(segment == 'neg',0,1)]
train_data[,.N/nrow(train_data), segment]
```
From the table above, we learn that the target variable is imbalanced.
```{r}
#plot segment
ggplot(train_data,aes(segment))+geom_bar(color='black',fill='tomato')+
geom_text(stat='count',aes(label=..count..),vjust=-0.5)
```
Lets see which genres are the most popular among hotstar users.
```{r}
# plot genres
# To plot genres we need to extract values from the data.
train_data[,gen1 := lapply(genres, function(k) str_extract_all(string = k, pattern = "[[:alpha:]]+"))]
train_data[,gen1 := lapply(gen1, unlist, use.names=F)]
genres <- table(unlist(train_data$gen1))
genres <- as.data.table(genres)
setnames(genres, c("V1","N"), c("Name","Count"))
genres <- genres[order(-Count)]
ggplot(genres, aes(x = reorder(Name, -Count), y = Count)) + geom_bar(stat = 'identity',fill='cornflowerblue',color='black')+
coord_flip()+
labs(x = 'Count', y = 'Genres')
```
There are 35 unique genres in the data set. Looks like hotstar has maximum video content on Cricket or we can say hotstar users watch cricket a lot, which is followed by Drama, Romance, Reality. Based on watch behaviour, I think we can safely assume that mode segment of hotstar users is relatively young i.e. 16-25 years
It will be better if we confirm our hypothesis. Let's check the mean segment value by genres. This will help us understand the behaviour of users belonging to pos, neg class.
```{r}
genreData <- data.table(ID = rep(train_data$ID, lapply(train_data$gen1, length)), segment = rep(train_data$segment, lapply(train_data$gen1, length)), Genres = unlist(train_data$gen1))
ggplot(genreData, aes(x = reorder(Genres, -segment), y = segment))+
stat_summary(fun.y = "mean", geom = "bar", color="black",fill="#FF6666")+
coord_flip()+
labs(x = 'segment',y = 'genres')
```
Wow! We see that majority of the people belonging to pos segment watch Family, Romance, Teen shows. Redefining our previous hypothesis, now we can safely assume that pos segment consists of people falling in 18 - 35 age group.
Let's check titles now.
```{r}
train_data[,g2 := lapply(titles, function(k) strsplit(x = k, split = ","))]
train_data[,g2 := lapply(g2, unlist, use.names=F)]
train_data[,g2 := lapply(g2, function(k) str_replace_all(string = k, pattern = "(\\:\\d+)$",replacement = ""))]
shows <- table(unlist(train_data$g2))
shows <- as.data.table(shows)
setnames(shows, c("V1","N"), c("Name","Count"))
shows[, Count := sort(Count, decreasing = T)]
show_sub <- shows[1:50]
ggplot(show_sub, aes(x = Name, y = Count)) + geom_bar(stat = 'identity',fill='tomato',color='black')+
labs(x = 'Count', y = 'Shows', Title = 'Frequency of Titles')+
coord_flip()+
theme_minimal()
```
There are 13,294 videos on Hotstar. We have plotted only top 50.
The most popular video belong to Cricket genre when 'Finch drills Boult'. Rest of the following videos also belong to genre Cricket.
Now, let's see from which city does hotstar viewers come!
```{r}
train_data[, g3 := lapply(cities, function(k) strsplit(x = k, split = ","))]
train_data[,g3 := lapply(g3, unlist, use.names=F)]
train_data[, g3 := lapply(g3, function(k) str_replace_all(string = k, pattern = "(\\:\\d+)$",replacement = ""))]
head(train_data$g3)
train_data[, g3 := lapply(g3, function(k) str_replace_all(string = k, pattern = "navi numbai", replacement = "mumbai"))]
citiesTable <- table(unlist(train_data$g3))
citiesTable <- as.data.table(citiesTable)
setnames(citiesTable, c("V1","N"), c('Name','Count'))
citiesTable <- citiesTable[order(-Count)]
citiesTable <- citiesTable[1:50]
ggplot(citiesTable, aes(x =Name, y = Count))+
geom_bar(stat = 'identity', color = 'black', fill='chartreuse4')+
coord_flip()
```
Based on statistics above about hotstar users i.e. they are cricket addicts. Most of these users, come from Mumbai followed by Delhi, Bangalore, Gurgaon, Chennai etc.
Let's proceed and understand on which days do these users come to watch videos.
```{r}
train_data[,g4 := lapply(dow, function(k) strsplit(x = k, split = ","))]
train_data[,g4 := lapply(g4, unlist, use.names =F)]
train_data[, g4 := lapply(g4, function(k) str_replace_all(string = k, pattern = "(\\:\\d+)$",replacement = ""))]
dowTable <- table(unlist(train_data$g4))
dowTable <- as.data.table(dowTable)
setnames(dowTable,c("V1","N"),c("Days","Count"))
ggplot(dowTable,aes(x = Days, y = Count))+geom_bar(stat = 'identity', color='black', fill='#FF6666')
```
Not much variation in week days. May be hours will be more interesting to look at.
```{r}
train_data[,g5 := lapply(tod, function(k) strsplit(x = k, split = ","))]
train_data[,g5 := lapply(g5, unlist, use.names =F)]
train_data[, g5 := lapply(g5, function(k) str_replace_all(string = k, pattern = "(\\:\\d+)$",replacement = ""))]
todTable <- table(unlist(train_data$g5))
todTable <- as.data.table(todTable)
setnames(todTable,c("V1","N"),c("Hour","Count"))
todTable[,Hour := as.integer(Hour)]
ggplot(todTable,aes(x = reorder(Hour, Hour), y = Count))+
geom_bar(stat = 'identity', color='black', fill='#FF9999')+
coord_flip()
```
Nothing surprising according to the nature of indians. Peak occurs at 9pm and start declining as night dawns. Let's check the mean of target variable by hour.
```{r}
hourTarget <- data.table(segment = rep(train_data$segment, lapply(train_data$g5, length)), hour = as.integer(unlist(train_data$g5)))
ggplot(hourTarget, aes(x = reorder(hour,hour), y = segment))+
stat_summary(fun.y = "mean", geom = "bar",color="black",fill= "#33CCFF")+
coord_flip()+
labs(x = 'segment',y = 'hour')
```
Even though we saw above that pos segment consists of people who mainly watch family, romantic, teen related shows. But this statistics reveals weird behaviour. It says, most of the people from pos segment watch shows at midnight.
But what could they possibly watch ?
Let's see!
```{r}
hourGenre <- data.table(genre = rep(unlist(train_data$gen1), lapply(train_data$g5, length)), hour = unlist(train_data$g5))
hourGenreD <- data.table(hour = rep(hourGenre$hour, lapply(hourGenre$genre, length)),genre = unlist(hourGenre$genre))
hourGenreD[,hour := as.integer(hour)]
head(hourGenreD)
ggplot(hourGenreD,aes(x = reorder(genre,hour), y = hour))+
stat_summary(fun.y = "mean", geom='bar',color='black',fill='#FF9933')+
coord_flip()
```
Sports! They watch sports related shows as the night dawns. Also, they tune in watching Live Shows at night.
## Summary
This notebook is created to help you understand the nature of this data set. Since is data isn't as structured as you would expect, this exploration notebook might provide you useful ideas to create new features. Good luck for the competition!