import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import decomposition
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import MinMaxScaler

np.random.seed(5)

# The competition datafiles are in the directory ../input
# Read competition data files:
train = pd.read_csv("../input/train.csv")
testX  = pd.read_csv("../input/test.csv")

X = train.drop('label', 1)
y = train[['label']]

print("X has {0[0]} rows and {0[1]} columns".format(X.shape))
print("Y has {0[0]} rows and {0[1]} columns".format(y.shape))

n_samples = y.shape[0]

X = X.as_matrix()
y = y.as_matrix().ravel()

scaler = MinMaxScaler()
scaler.fit(X)
X = scaler.transform(X)

max_iter = 25
print("training...")
mlp = MLPClassifier(
	verbose=1,
	random_state=0,
	max_iter=max_iter,
	solver='sgd',
	learning_rate='constant',
	momentum=.9,
	nesterovs_momentum=True,
	learning_rate_init=0.2
)
mlp.fit(X, y)
print("Training set score: %f" % mlp.score(X, y))
print("Training set loss: %f" % mlp.loss_)

testX = scaler.transform(testX)

testy = mlp.predict(testX)

results = pd.DataFrame(testy, columns=['Label'])
results.index += 1

results.to_csv('results.csv', index_label='ImageId')