{"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_minor":4,"nbformat":4,"cells":[{"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\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import confusion_matrix, accuracy_score\n\n# Input data files are available in the read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train.iloc[:, 1:].values.reshape(-1, 28, 28)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train_images[35], cmap='gray')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.iloc[35, 0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train_images, train.iloc[:, 0], test_size=0.3, random_state=0)\n\nprint(X_train.shape)\nprint(X_val.shape)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc = StandardScaler()\nX_train = sc.fit_transform(X_train.reshape(-1, 28*28)).reshape(-1, 28, 28)\nX_val = sc.transform(X_val.reshape(-1, 28*28)).reshape(-1, 28, 28)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[40], cmap='gray')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.iloc[40]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc = RandomForestClassifier(n_estimators=100)\nrfc.fit(X_train.reshape(-1, 28*28), y_train)\ny_pred = rfc.predict(X_val.reshape(-1, 28*28))\nprint('Accuracy Score: ', accuracy_score(y_val, y_pred))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nprint(metrics.classification_report(y_val, y_pred, digits=2))\ncm = confusion_matrix(y_val, y_pred)\nplt.figure(figsize=(10, 7))\nsns.heatmap(cm, annot=True, fmt='d')\nplt.xlabel('Predicted')\nplt.ylabel('Truth')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = test.values.reshape(-1, 28, 28)\ntest_images = sc.transform(test_images.reshape(-1, 28*28)).reshape(-1, 28, 28)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(test_images[50], cmap='gray')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_testrfc = rfc.predict(test_images.reshape(-1, 28*28))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_testrfc[50]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': np.arange(1, len(y_testrfc)+1), 'Label': y_testrfc})\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head(5)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(5, 5, figsize=(10, 10))\nfor i in range(5):\n    for j in range(5):\n        ax[i, j].imshow(test_images[i*5+j], cmap='gray')\n        ax[i, j].set_title(y_testrfc[i*5+j])\n        ax[i, j].axis('off')","metadata":{},"execution_count":null,"outputs":[]}]}