Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 50 lines (42 sloc) 1.524 kb
85a65ad5 »
2012-04-29 Stupid linear decay thing I wanted to try
1 train <- read.csv('data/TrainingData.csv')
2 submit <- read.csv('data/SubmissionZerosExceptNAs.csv')
3
4 library(plyr);
5 library(zoo);
6 library(foreach);
7 library(doMC);
8
9 registerDoMC(4);
10
11 rmedian <- function (x) median(x, na.rm=T);
12 rmax <- function (x) max(x, na.rm=T);
13 rmin <- function (x) min(x, na.rm=T);
14
15 medians <- colwise(rmedian, is.numeric)(train);
16 mins <- colwise(rmin, is.numeric)(train);
17 maxes <- colwise(rmax, is.numeric)(train);
18
19 pred <- ddply(train, .(chunkID), function (data) {
20 df <- data.frame(position_within_chunk=c(193:264));
21 data <- join(data.frame(position_within_chunk=c(1:192)), data, by='position_within_chunk', match='first');
22 for (var in colnames(data)) {
23 if (substr(var, 1, 6) == 'target') {
24 d <- data[,var];
25 if (all(is.na(d))) {
26 df[,var] <- rep(medians[[var]], 72);
27 } else {
28 max.nona <- max(which(!is.na(d)));
29 alpha <- (df$position_within_chunk - max.nona) / (264 - max.nona)
30 df[,var] <- alpha * rmedian(d) + (1 - alpha) * d[max.nona];
31 }
32 }
33 }
34 return(df)
35 }, .progress='text', .parallel=T);
36
37 pred <- join(submit[,1:5], pred, by=c('chunkID', 'position_within_chunk'), match='first');
38
39 for (target in colnames(submit)[6:44]) {
40 idxs <- which(submit[,target] > -999999);
41 submit[idxs, target] <- pred[idxs, target];
42 submit[,target] <- na.fill(submit[,target], medians[target]);
43 }
44
45 argv <- commandArgs(T);
46 filename <- argv[1];
47 if (length(filename) == 0) {
48 filename <- 'predictions/scratch.csv';
49 }
50 write.csv(submit, filename);
Something went wrong with that request. Please try again.