{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# MNIST with numpy estimators\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv') \ntest = pd.read_csv('../input/digit-recognizer/test.csv')\ntrain.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Decrease arrays sizes\nX = train.drop('label', axis=1).astype(np.int32)\ny = train['label'].astype(np.int32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#define tensors\nfeat_cols = [tf.feature_column.numeric_column(\"x\", shape=[28, 28])]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classifier = tf.estimator.DNNClassifier(\n    feature_columns=feat_cols,\n    hidden_units=[256,32],\n    optimizer=tf.train.AdamOptimizer(1e-4),\n    n_classes=10,\n    dropout=0.1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Training data"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_input_fn = tf.estimator.inputs.numpy_input_fn(\n    x={\"x\": X_train.values},\n    y=y_train.values,\n    num_epochs=None,\n    batch_size=50,\n    shuffle=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classifier.train(input_fn=train_input_fn, steps=100000)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_input_fn = tf.estimator.inputs.numpy_input_fn(\n    x={\"x\": X_test.values},\n    num_epochs=1,\n    shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"note_predictions = list(classifier.predict(input_fn=test_input_fn))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Check, which parameter to extract from predictions\nnote_predictions[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds  = []\nfor pred in note_predictions:\n    preds.append(pred['class_ids'][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint(accuracy_score(y_test,preds))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Predict on test data"},{"metadata":{"trusted":true},"cell_type":"code","source":"fin_test_input_fn = tf.estimator.inputs.numpy_input_fn(\n    x={\"x\": test.values},\n    num_epochs=1,\n    shuffle=False)\nnote_fin_predictions = list(classifier.predict(input_fn=fin_test_input_fn))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_preds  = []\nfor pred in note_fin_predictions:\n    final_preds.append(pred['class_ids'][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'ImageId':range(1, len(final_preds)+1), 'Label':final_preds})\nsubmission.to_csv('submission.csv', index = False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.3"}},"nbformat":4,"nbformat_minor":1}