{"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 # 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 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":{"iopub.status.busy":"2023-11-04T08:26:47.079499Z","iopub.execute_input":"2023-11-04T08:26:47.080312Z","iopub.status.idle":"2023-11-04T08:26:47.089463Z","shell.execute_reply.started":"2023-11-04T08:26:47.080280Z","shell.execute_reply":"2023-11-04T08:26:47.088546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ------------------------------------------------------","metadata":{}},{"cell_type":"markdown","source":"* [portal](https://www.kaggle.com/code/maxim9012/digit-recognizer)","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.model_selection import train_test_split\n\nfrom keras.preprocessing.text import Tokenizer as token\nimport tensorflow\n\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\nfrom sklearn.ensemble import RandomForestClassifier\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.models import Sequential\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.layers import Dense, LSTM, Embedding, Bidirectional\nfrom tensorflow.keras.models import load_model\nfrom nltk.corpus import stopwords\nfrom string import punctuation\nimport re\nimport pickle as p","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:26:47.091332Z","iopub.execute_input":"2023-11-04T08:26:47.091618Z","iopub.status.idle":"2023-11-04T08:26:47.111016Z","shell.execute_reply.started":"2023-11-04T08:26:47.091594Z","shell.execute_reply":"2023-11-04T08:26:47.110220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"# Load data\ntrain_df = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:26:47.112062Z","iopub.execute_input":"2023-11-04T08:26:47.112357Z","iopub.status.idle":"2023-11-04T08:26:49.898693Z","shell.execute_reply.started":"2023-11-04T08:26:47.112333Z","shell.execute_reply":"2023-11-04T08:26:49.897608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:26:49.900444Z","iopub.execute_input":"2023-11-04T08:26:49.900804Z","iopub.status.idle":"2023-11-04T08:26:51.690875Z","shell.execute_reply.started":"2023-11-04T08:26:49.900773Z","shell.execute_reply":"2023-11-04T08:26:51.690047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:26:51.693640Z","iopub.execute_input":"2023-11-04T08:26:51.694233Z","iopub.status.idle":"2023-11-04T08:26:53.840755Z","shell.execute_reply.started":"2023-11-04T08:26:51.694201Z","shell.execute_reply":"2023-11-04T08:26:53.839775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = train_df['label']\ntrain_df = train_df.drop(columns='label')","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:26:53.841991Z","iopub.execute_input":"2023-11-04T08:26:53.842307Z","iopub.status.idle":"2023-11-04T08:26:53.926119Z","shell.execute_reply.started":"2023-11-04T08:26:53.842281Z","shell.execute_reply":"2023-11-04T08:26:53.925013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"green = '#52BE80'\nred = '#EC7063'\nsns.countplot(x=target, palette=[green, red])","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:26:53.928014Z","iopub.execute_input":"2023-11-04T08:26:53.928405Z","iopub.status.idle":"2023-11-04T08:26:54.172483Z","shell.execute_reply.started":"2023-11-04T08:26:53.928370Z","shell.execute_reply":"2023-11-04T08:26:54.171613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train_df, target, test_size=0.2, random_state=64)\nprint('Shape of train', X_train.shape)\nprint('Shape of Validation', X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:26:54.173922Z","iopub.execute_input":"2023-11-04T08:26:54.174620Z","iopub.status.idle":"2023-11-04T08:26:54.387636Z","shell.execute_reply.started":"2023-11-04T08:26:54.174583Z","shell.execute_reply":"2023-11-04T08:26:54.386629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc = RandomForestClassifier(criterion='entropy', n_estimators=700)\n\nrfc.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:26:54.389014Z","iopub.execute_input":"2023-11-04T08:26:54.389391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filemodelname = 'answer.csv'\n\nwith open(filemodelname, 'wb') as handle:\n    p.dump(rfc, handle, protocol=p.HIGHEST_PROTOCOL)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(input, model):\n    with open(model, 'rb') as handle:\n        model = p.load(handle)\n        inn = []\n        val_pred = model.predict(input)\n        arr = np.array(val_pred)\n        unique, counts = np.unique(arr, return_counts=True)\n        val_pred = dict(zip(unique, counts))\n        print(max(val_pred))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val = train_test_split(test_df, test_size=0.2, random_state=64)\nX_train","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inpt = X_train\narr = predict(inpt, '/kaggle/working/answer.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}