{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":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)\nfrom sklearn.naive_bayes import MultinomialNB # Using multinomial because our dataset is discrete\nfrom sklearn.model_selection import train_test_split # Splitting test and train data\nfrom sklearn.metrics import accuracy_score # To calculate accuracy\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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":19,"outputs":[{"output_type":"stream","text":"['sample_submission.csv', 'test.csv', 'train.csv']\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Importing data set and preprocessing\ndataset = pd.read_csv(\"../input/train.csv\")\nX = dataset.iloc[:, dataset.columns != 'label'].values\ny = dataset.iloc[:, 0].values\ny = y.reshape(-1, 1) # Convert vector to matrix of [1,0]\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)","execution_count":20,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# MultinomialNB object creation and fitting our model\nmnb = MultinomialNB()\nmnb.fit(X_train, y_train)","execution_count":21,"outputs":[{"output_type":"stream","text":"/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py:761: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n  y = column_or_1d(y, warn=True)\n","name":"stderr"},{"output_type":"execute_result","execution_count":21,"data":{"text/plain":"MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Prediction\ny_pred = mnb.predict(X_test)\n\n#Accuracy Calculation\naccuracy = accuracy_score(y_test, y_pred)\nprint(accuracy)\n\n#Submission \ndataset_test = pd.read_csv(\"../input/test.csv\")\nx_question = dataset_test.values\ny_submit = mnb.predict(x_question)","execution_count":22,"outputs":[{"output_type":"stream","text":"0.8291428571428572\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"#New pandas dataframe for submission\ndf_to_submit = pd.DataFrame()\ndf_to_submit = pd.DataFrame(columns=[ 'Labels'])\ndf_to_submit['Labels'] = y_submit\nprint(df_to_submit)","execution_count":23,"outputs":[{"output_type":"stream","text":"       Labels\n0           2\n1           0\n2           9\n3           4\n4           2\n5           7\n6           0\n7           3\n8           0\n9           3\n10          5\n11          7\n12          4\n13          0\n14          4\n15          3\n16          3\n17          1\n18          9\n19          0\n20          9\n21          1\n22          8\n23          5\n24          7\n25          9\n26          2\n27          7\n28          7\n29          7\n...       ...\n27970       5\n27971       0\n27972       4\n27973       8\n27974       0\n27975       3\n27976       6\n27977       0\n27978       1\n27979       8\n27980       3\n27981       1\n27982       8\n27983       0\n27984       4\n27985       5\n27986       2\n27987       2\n27988       8\n27989       6\n27990       7\n27991       6\n27992       1\n27993       9\n27994       7\n27995       9\n27996       7\n27997       3\n27998       9\n27999       2\n\n[28000 rows x 1 columns]\n","name":"stdout"}]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}