import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from numpy import genfromtxt, savetxt

# The competition datafiles are in the directory ../input
# Read competition data files:
#train = pd.read_csv("../input/train.csv")
#test  = pd.read_csv("../input/test.csv")

# Write to the log:
#print("Training set has {0[0]} rows and {0[1]} columns".format(train.shape))
#print("Test set has {0[0]} rows and {0[1]} columns".format(test.shape))
# Any files you write to the current directory get shown as outputs


def main():
	#create the training & test sets, skipping the header row with [1:]
	dataset = genfromtxt('../input/train.csv', delimiter=',', dtype='f8')[1:]    
	target = [x[0] for x in dataset]
	train = [x[1:] for x in dataset]
	test = genfromtxt('../input/test.csv', delimiter=',', dtype='f8')[1:]

	#create and train the random forest
	#multi-core CPUs can use: rf = RandomForestClassifier(n_estimators=100, n_jobs=2)
	rf = RandomForestClassifier(n_estimators=100)
	rf.fit(train, target)

	id = range(1,28001)
	savetxt('submission.csv', np.column_stack((id, rf.predict(test))), delimiter=',', header='ImageId,Label', fmt='%u')

if __name__=="__main__":
	main()
