{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"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-03-31T04:25:10.179506Z","iopub.execute_input":"2023-03-31T04:25:10.180556Z","iopub.status.idle":"2023-03-31T04:25:10.222464Z","shell.execute_reply.started":"2023-03-31T04:25:10.180490Z","shell.execute_reply":"2023-03-31T04:25:10.221029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## get the clean data for train\ntrain = pd.read_csv(\"/kaggle/input/ai4vozer-k1a1-supervised-classification-model/clean_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-03-31T04:25:36.588432Z","iopub.execute_input":"2023-03-31T04:25:36.589019Z","iopub.status.idle":"2023-03-31T04:25:38.647523Z","shell.execute_reply.started":"2023-03-31T04:25:36.588969Z","shell.execute_reply":"2023-03-31T04:25:38.646139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## turn data into a countvectorizer\nfrom sklearn.naive_bayes import MultinomialNB\nfrom sklearn.feature_extraction.text import CountVectorizer\ncount = CountVectorizer(binary=False,max_features=4000)\nX_train = train['cleaned']\nX_train_counted = count.fit_transform(X_train).toarray()\nprint(X_train_counted[0])","metadata":{"execution":{"iopub.status.busy":"2023-03-31T04:28:02.405041Z","iopub.execute_input":"2023-03-31T04:28:02.405514Z","iopub.status.idle":"2023-03-31T04:28:06.183054Z","shell.execute_reply.started":"2023-03-31T04:28:02.405471Z","shell.execute_reply":"2023-03-31T04:28:06.182006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" from sklearn import preprocessing\nle = preprocessing.LabelEncoder()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T04:30:32.923657Z","iopub.execute_input":"2023-03-31T04:30:32.924508Z","iopub.status.idle":"2023-03-31T04:30:32.929530Z","shell.execute_reply.started":"2023-03-31T04:30:32.924459Z","shell.execute_reply":"2023-03-31T04:30:32.928078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## fit data to model\ny_train = le.fit(train['type'])\nprint(y_train.classes_)\ny_train = le.transform(train['type'])\nmnb = MultinomialNB(alpha = 1, fit_prior = False, class_prior = None)\nmnb.fit(X_train_counted,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T04:31:49.467856Z","iopub.execute_input":"2023-03-31T04:31:49.469228Z","iopub.status.idle":"2023-03-31T04:31:54.982251Z","shell.execute_reply.started":"2023-03-31T04:31:49.469159Z","shell.execute_reply":"2023-03-31T04:31:54.980865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## convert cleaned_test to data of countvectorizer\ntest = pd.read_csv(\"/kaggle/input/ai4vozer-k1a1-supervised-classification-model/clean_test.csv\")\nX_test = test['cleaned']\nX_test_counted = count.transform(X_test).toarray()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T04:33:02.625208Z","iopub.execute_input":"2023-03-31T04:33:02.626308Z","iopub.status.idle":"2023-03-31T04:33:03.947306Z","shell.execute_reply.started":"2023-03-31T04:33:02.626224Z","shell.execute_reply":"2023-03-31T04:33:03.945716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## make prediction\npred = mnb.predict(X_test_counted)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T04:35:42.684514Z","iopub.execute_input":"2023-03-31T04:35:42.684935Z","iopub.status.idle":"2023-03-31T04:35:42.740216Z","shell.execute_reply.started":"2023-03-31T04:35:42.684899Z","shell.execute_reply":"2023-03-31T04:35:42.738357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## form a csv file that has 2 cols: Id and Category\n### first inverse transform the prediction\npred_cat = le.inverse_transform(pred)\npred = pd.DataFrame({\"Category\":pred_cat})\npred['Id'] = test['Id']","metadata":{"execution":{"iopub.status.busy":"2023-03-31T04:35:43.787458Z","iopub.execute_input":"2023-03-31T04:35:43.788672Z","iopub.status.idle":"2023-03-31T04:35:43.799178Z","shell.execute_reply.started":"2023-03-31T04:35:43.788624Z","shell.execute_reply":"2023-03-31T04:35:43.797774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.to_csv(\"pred.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T04:37:31.506432Z","iopub.execute_input":"2023-03-31T04:37:31.506851Z","iopub.status.idle":"2023-03-31T04:37:31.517519Z","shell.execute_reply.started":"2023-03-31T04:37:31.506817Z","shell.execute_reply":"2023-03-31T04:37:31.515891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.head(4)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T04:37:36.033681Z","iopub.execute_input":"2023-03-31T04:37:36.034084Z","iopub.status.idle":"2023-03-31T04:37:36.053496Z","shell.execute_reply.started":"2023-03-31T04:37:36.034051Z","shell.execute_reply":"2023-03-31T04:37:36.052518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}