library(randomForest)
library(readr)

set.seed(0)

train <- read_csv("../input/train.csv")
test <- read_csv("../input/test.csv")

numTrain <- 40000
numTrees <- 100
rows <- sample(1:nrow(train), numTrain)

# Remove pixels that don't help from both train and test
allmisscols <- apply(train,2, function(x)all(x==0))
colswithallmiss <-names(allmisscols[allmisscols>0])
trainDataFrame <- train[, !names(train) %in% colswithallmiss]
testDataFrame <- test[, !names(test) %in% colswithallmiss]

trainLabels <- as.factor(trainDataFrame[,1])
trainRf <- trainDataFrame[,-1]

#RandomForest
rf <- randomForest(trainRf, trainLabels, xtest=testDataFrame, ntree=numTrees)
print(rf) # view results 
importance(rf) # importance of each predictor
predictions <- data.frame(ImageId=1:nrow(test), Label=levels(trainLabels)[rf$test$predicted])
head(predictions)
write_csv(predictions, "rf_benchmark.csv")



