-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.R
320 lines (280 loc) · 9.42 KB
/
preprocessing.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# ************************************************
# parseDataset() :
#
# Read a CSV file and return it as a data frame
#
# INPUT: string - csvFilename - CSV filename
#
# OUTPUT : data frame - df - data frame
# ************************************************
parseDataset<-function(csvFilename){
df<-read.csv(file=csvFilename,encoding="UTF-8",stringsAsFactors = FALSE, na.strings=c("","NA"))
print(paste(csvFilename,"'s number of rows are",nrow(df)))
return(df)
}
# ************************************************
# naValuesPerColumn() :
#
# Read a dataframe and print its NA values per column
#
# INPUT: dataframe - df
naValuesPerColumn<-function(df){
for (i in 1:ncol(df))
cat("Column ", i, "has this many NA values:", sum(is.na(df[,i])), "\n")
}
# ************************************************
# removeFields() :
#
# Remove certain columns from the dataframe
#
# INPUT: data frame - df - data frame with data of the dataset
# vector - fields - column names that should be dropped
#
# OUTPUT : data frame - df with fields removed
# ************************************************
removeFields<-function(df, fields) {
drops <- fields
df <- df[ , !(names(df) %in% drops)]
return (df)
}
# ************************************************
# convertTypes() :
#
# Convert types to the wanted ones
#
# INPUT: data frame - df - dataframe from the dataset
#
# OUTPUT : data frame - df with updated types
# ************************************************
convertTypes<-function(df) {
# Convert to double
df$budget <- as.numeric(df$budget)
df$popularity <- as.double(df$popularity)
# Convert all date values to YYYY format
df$release_date <- year(as.Date(df$release_date))
return(df)
}
# *******************************************************
# classifyMoviePerformance() :
#
# Converts "vote_average" column to 0 or 1, if the
# value is equal or more than blockbuster_threshold
#
# INPUT: data frame - df - dataframe from the dataset
# num - blockbuster_threshold - value above which the movie is
# considered a success
#
# OUTPUT : data frame - df with updated vote_average col
# *******************************************************
classifyMoviePerformance<-function(df, blockbuster_threshold) {
for (i in 1:nrow(df))
{
if (df[i,"vote_average"] >= blockbuster_threshold)
{
df[i,"vote_average"]<-1
}
else
{
df[i,"vote_average"]<-0
}
}
return(df)
}
# ************************************************
# initialPreprocessing() :
#
# Perform semi-preprocessing on csv files
#
# INPUT: config - list of configurations
#
# OUTPUT : data frame - movies - initially processed movies
# ************************************************
initialPreprocessing<-function(config){
# Read the CSV file
df <- parseDataset(config$DATASET_FILENAME)
# Check how many NA values there are, per column
naValuesPerColumn(df)
# Drop problematic columns
movies <- removeFields(df, config$PROBLEMATIC_FIELDS)
cat("Removing problematic columns\n")
# Remove rows with NA fields
movies <- na.omit(movies)
# Update datatypes
movies <- convertTypes(movies)
# Movie is a blockbuster if score is >= 6.5
movies <- classifyMoviePerformance(movies, config$BLOCKBUSTER_THRESHOLD)
# Remove any duplicate movies
movies <- movies[!duplicated(movies[c("original_title","id","imdb_id")]),]
return (movies)
}
# ************************************************
# preprocessing() :
#
# Perform preprocessing and handle outliers
#
# INPUT: data frame - df - data frame of movies
# list - config - list of configurations
#
# OUTPUT : list - datasets - (preprocessed dataset of tracks, preprocessed and scaled dataset of tracks)
# ************************************************
preprocessing<-function(df, config){
# Remove more fields from the df to prepare for training
df <- removeFields(df, config$UNUSED_FIELDS)
# Get field types
field_types <- determineFieldTypes(df, config)
# If ordinals are outliers, replace them with mean values
# If p-value < (1-confidence), reject it
ordinals<-df[,which(field_types=="ORDINAL"),drop=FALSE]
ordinals_outliers_replaced<-determineOutliers(ordinals=ordinals,confidence=config$OUTLIER_CONF)
df[,which(field_types=="ORDINAL")]<-ordinals_outliers_replaced
# Normalisation and z-scaling of ordinals
zscaled <- apply(ordinals_outliers_replaced, MARGIN = 2,
FUN = function(X) (scale(X,center=TRUE,
scale=TRUE)))
# Scale to be [0.0,1.0]
ordinalReadyforML<-rescaleDataframe(as.data.frame(zscaled))
df_scaled<-data.frame(df)
df_scaled[,which(field_types=="ORDINAL")]<-ordinalReadyforML
return (list(movies=df, movies_normalized=df_scaled))
}
# ************************************************
# determineFieldTypes() :
#
# INPUT: data frame - df - semi-preprocessed movies
# list - config - list of configurations
# OUTPUT : vector strings with type
# ************************************************
determineFieldTypes<-function(df, config) {
field_types<-vector()
# For all columns
for(field in 1:(ncol(df))){
entry<-which(data.frame()$name==names(df)[field])
if (length(entry)>0){
field_types[field]<-data.frame()$type[entry]
next
}
# Assign symbolics
if (names(df)[field] %in% config$SYMBOLIC_FIELDS){
field_types[field]<-config$TYPE_SYMBOLIC
}
# Assign nums
else {
field_types[field]<-config$TYPE_NUMERIC
# Assign ordinals and discreets
if (names(df)[field] %in% config$ORDINAL_FIELDS){
field_types[field]<-config$TYPE_ORDINAL
}
else {
field_types[field]<-config$TYPE_DISCREET
}
}
}
# View the field types on the console
ordinal_fields<-names(df)[field_types=="ORDINAL"]
print(paste("ORDINAL FIELDS=",length(ordinal_fields)))
print(ordinal_fields)
discreet_fields<-names(df)[field_types=="DISCREET"]
print(paste("DISCREET FIELDS=",length(discreet_fields)))
print(discreet_fields)
symbolic_fields<-names(df)[field_types=="SYMBOLIC"]
print(paste("SYMBOLIC FIELDS=",length(symbolic_fields)))
print(symbolic_fields)
results<-data.frame(field=names(df),type=field_types)
print(formattable::formattable(results))
return (field_types)
}
# ************************************************
#
# [Based on Lab 4's code]
#
# determineOutliers() :
#
# Determine if a value of a record is an outlier for each field
#
# INPUT: data frame - ordinals - numeric fields only
# double - confidence - Confidence above which is determined an outlier
#
# OUTPUT : data frame - ordinals with any outlier values replaced with the median of the field
# ************************************************
# ChiSquared method
# Uses library(outliers)
# https://cran.r-project.org/web/packages/outliers/outliers.pdf
determineOutliers<-function(ordinals,confidence){
# For every ordinal field in our dataset
for(field in 1:(ncol(ordinals))){
sorted<-unique(sort(ordinals[,field],decreasing=TRUE))
outliers<-which(outliers::scores(sorted,type="chisq",prob=abs(confidence)))
NplotOutliers(sorted,outliers,colnames(ordinals)[field])
# If found records with outlier values
if ((length(outliers>0))){
# If confidence is positive then replace values with their means, otherwise do nothing
if (confidence>0){
outliersGone<-rm.outlier(ordinals[,field],fill=TRUE)
sorted<-unique(sort(outliersGone,decreasing=TRUE))
NplotOutliers(sorted,vector(),colnames(ordinals)[field])
ordinals[,field]<-outliersGone # Put in the values with the outliers replaced by means
print(paste("Outlier field=",names(ordinals)[field],"Records=",length(outliers),"Replaced with MEAN"))
} else {
print(paste("Outlier field=",names(ordinals)[field],"Records=",length(outliers)))
}
}
}
return(ordinals)
}
# ************************************************
#
# [Based on Lab 4's code]
#
# NplotOutliers() :
#
# Scatter plot of field values and colours outliers in red
#
# INPUT: Vector - sorted - points to plot as literal values
# Vector - outliers - list of above points that are considered outliers
# String - fieldName - name of field to plot
#
# OUTPUT : None
# ************************************************
NplotOutliers<-function(sorted,outliers,fieldName){
plot(1:length(sorted),sorted,
pch=1,
xlab="Unique records",
ylab=paste("Sorted values",fieldName),
bty="n")
if (length(outliers)>0)
points(outliers,sorted[outliers],col="red",pch=19)
}
# ************************************************
#
# [Based on Lab 4's code]
#
# Nrescale() :
#
# These are the real values, that we scale between 0-1
# i.e. x-min / (max-min)
#
# INPUT: vector - input - values to scale
#
# OUTPUT : vector - scaled values to [0.0,1.0]
# ************************************************
Nrescale<-function(input){
minv<-min(input)
maxv<-max(input)
return((input-minv)/(maxv-minv))
}
# ************************************************
#
# [Based on Lab 4's code]
#
# rescaleDataframe() :
#
# Rescale dataframe to [0.0,1.0]
#
# INPUT: data frame - df - numeric data frame
#
# OUTPUT : data frame - scaled numeric data frame
# ************************************************
rescaleDataframe<-function(df){
scaled<-sapply(as.data.frame(df),Nrescale)
return(scaled)
}