{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Kannada MNIST with H2O DeepLearning\n\n![](https://miro.medium.com/max/1095/1*s1tZoytg71DUnEKEYWtyNw.png)\n\n[H2O’s](http://docs.h2o.ai/h2o/latest-stable/h2o-docs/welcome.html) is an open source, in-memory, distributed, fast, and scalable machine learning and predictive analytics platform that allows you to build machine learning models on big data and provides easy productionalization of those models in an enterprise environment.\n\nH2O's [Deep Learning](http://docs.h2o.ai/h2o/latest-stable/h2o-docs/data-science/deep-learning.html) is based on a multi-layer feedforward artificial neural network that is trained with stochastic gradient descent using back-propagation. The network can contain a large number of hidden layers consisting of neurons with tanh, rectifier, and maxout activation functions.\n\nAdvanced features such as adaptive learning rate, rate annealing, momentum training, dropout, L1 or L2 regularization, checkpointing, and grid search enable high predictive accuracy. Each compute node trains a copy of the global model parameters on its local data with multi-threading (asynchronously) and contributes periodically to the global model via model averaging across the network.\n"},{"metadata":{},"cell_type":"markdown","source":"## Acknowledgement\n\nArno Candel for his [kernel](https://www.kaggle.com/arnocandel/mnist-with-h2o-deeplearning) on MNIST with H2O DeepLearning"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Starting H2O cluster and Importing the DeepLearning Estimator"},{"metadata":{"trusted":true},"cell_type":"code","source":"import h2o\nprint(h2o.__version__)\nfrom h2o.estimators.deeplearning import H2ODeepLearningEstimator\n\nh2o.init(max_mem_size='16G')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Upload the datasets to the the H2O cluster. \n\nThe data is imported into H2OFrames, which operate similarly in function to pandas DataFrames.  "},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = h2o.import_file(\"/kaggle/input/Kannada-MNIST/train.csv\")\ntest = h2o.import_file(\"/kaggle/input/Kannada-MNIST/test.csv\")\nsubmission = h2o.import_file(\"/kaggle/input/Kannada-MNIST/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":" ## Specify the response and predictor columns"},{"metadata":{"trusted":true},"cell_type":"code","source":"x = train.columns[1:]\ny = 'label'\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Encode the response column as categorical for **multinomial classification"},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain[y] = train[y].asfactor()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Train Deep Learning model \n\nHere nfolds = 3, which means it performs three folds cross validation. To disable cross-validation, use nfolds=0,\nwhich is the default value. More information about the parameters can be found in the [H2O Deep Learning booklet](http://h2o.ai/resources/).\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"dl = H2ODeepLearningEstimator(input_dropout_ratio = 0.2, nfolds=3)\ndl.train(x=x, y=y, training_frame=train)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Extracting the Results"},{"metadata":{"trusted":true},"cell_type":"code","source":"dl.model_performance(xval=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = dl.predict(test)\npreds['p1'].as_data_frame().values.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Sample Submissions"},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')\nsample_submission.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission['label'] = preds['predict'].as_data_frame().values\nsample_submission.to_csv('H2O_DL.csv', index=False)\nsample_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}