{"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\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\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.model_selection import cross_validate, StratifiedShuffleSplit\nfrom sklearn.metrics import precision_recall_fscore_support\n\nif __name__ == \"__main__\":\n    # Input data files are available in the read-only \"../input/\" directory\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\n    train_path, test_path = None, None\n    for dirname, _, filenames in os.walk('/kaggle/input'):\n        for filename in filenames:\n            if \"train\" in filename:\n                train_path = os.path.join(dirname, filename)\n            elif \"test\" in filename:\n                test_path = os.path.join(dirname, filename)\n\n# Read in train data\ntrain_df = pd.read_csv(train_path)\n\n# Transform the string labels into numerical\nlabel_encoder = LabelEncoder()\ntrain_df_target = label_encoder.fit_transform(train_df.label.values)\n\ntfidf_vectorizer = TfidfVectorizer(use_idf=True, max_df=0.95)\ntfidf_vectorizer.fit_transform(train_df.body.values)\ntrain_df_feature = tfidf_vectorizer.transform(train_df.body.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fit our model\nfrom sklearn.linear_model import LogisticRegression\n\nlr = LogisticRegression()\n\n# Fit our model\nlr.fit(train_df_feature, train_df_target)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Read in test data and predict\ntest_df = pd.read_csv(test_path)\n\ntest_df_feature = tfidf_vectorizer.transform(test_df.body.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df_prediction = lr.predict(test_df_feature)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df[\"logistic regression predictions\"] = label_encoder.inverse_transform(test_df_prediction)\nlogistic_regression = test_df[[\"id\", \"logistic regression predictions\"]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"logistic_regression = logistic_regression.rename(columns={\"logistic regression predictions\": \"prediction_labels\"})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"logistic_regression.to_csv(\"Logistic Regression Output.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.naive_bayes import MultinomialNB\nnb = MultinomialNB()\nnb.fit(train_df_feature, train_df_target)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df_prediction = nb.predict(test_df_feature)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df[\"naive bayes predictions\"] = label_encoder.inverse_transform(test_df_prediction)\nnaive_bayes = test_df[[\"id\", \"naive bayes predictions\"]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"naive_bayes = naive_bayes.rename(columns={\"naive bayes predictions\": \"prediction_labels\"})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"naive_bayes.to_csv(\"Naive Bayes Output.csv\", index=False)","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":4}