{"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":"2022-07-29T03:33:52.548464Z","iopub.execute_input":"2022-07-29T03:33:52.549498Z","iopub.status.idle":"2022-07-29T03:33:52.561101Z","shell.execute_reply.started":"2022-07-29T03:33:52.549457Z","shell.execute_reply":"2022-07-29T03:33:52.560147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/stanford-sentiment-treebank/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:33:53.522340Z","iopub.execute_input":"2022-07-29T03:33:53.523363Z","iopub.status.idle":"2022-07-29T03:33:53.674603Z","shell.execute_reply.started":"2022-07-29T03:33:53.523320Z","shell.execute_reply":"2022-07-29T03:33:53.673342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = df['target'].tolist()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:35:25.785709Z","iopub.execute_input":"2022-07-29T03:35:25.786119Z","iopub.status.idle":"2022-07-29T03:35:25.793301Z","shell.execute_reply.started":"2022-07-29T03:35:25.786083Z","shell.execute_reply":"2022-07-29T03:35:25.792011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Vectorize all sentences","metadata":{}},{"cell_type":"code","source":"from sklearn.feature_extraction.text import TfidfVectorizer","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:34:24.182133Z","iopub.execute_input":"2022-07-29T03:34:24.182570Z","iopub.status.idle":"2022-07-29T03:34:24.189201Z","shell.execute_reply.started":"2022-07-29T03:34:24.182536Z","shell.execute_reply":"2022-07-29T03:34:24.188242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer = TfidfVectorizer(\n    stop_words = 'english'\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:35:12.056102Z","iopub.execute_input":"2022-07-29T03:35:12.056514Z","iopub.status.idle":"2022-07-29T03:35:12.061907Z","shell.execute_reply.started":"2022-07-29T03:35:12.056479Z","shell.execute_reply":"2022-07-29T03:35:12.060848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = vectorizer.fit_transform(df['sentence'].tolist())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:35:41.568063Z","iopub.execute_input":"2022-07-29T03:35:41.568465Z","iopub.status.idle":"2022-07-29T03:35:42.541810Z","shell.execute_reply.started":"2022-07-29T03:35:41.568429Z","shell.execute_reply":"2022-07-29T03:35:42.540568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Params: {vectorizer.get_params()}, vocab size: {len(vectorizer.vocabulary_)}')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:36:32.407423Z","iopub.execute_input":"2022-07-29T03:36:32.407808Z","iopub.status.idle":"2022-07-29T03:36:32.414523Z","shell.execute_reply.started":"2022-07-29T03:36:32.407775Z","shell.execute_reply":"2022-07-29T03:36:32.412982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train a Decision Tree model","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:37:24.509252Z","iopub.execute_input":"2022-07-29T03:37:24.509675Z","iopub.status.idle":"2022-07-29T03:37:24.764191Z","shell.execute_reply.started":"2022-07-29T03:37:24.509642Z","shell.execute_reply":"2022-07-29T03:37:24.763311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = DecisionTreeClassifier(\n    random_state = 42,\n    criterion = 'entropy'\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:37:52.050706Z","iopub.execute_input":"2022-07-29T03:37:52.051090Z","iopub.status.idle":"2022-07-29T03:37:52.056658Z","shell.execute_reply.started":"2022-07-29T03:37:52.051058Z","shell.execute_reply":"2022-07-29T03:37:52.055495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:38:03.644924Z","iopub.execute_input":"2022-07-29T03:38:03.645343Z","iopub.status.idle":"2022-07-29T03:38:34.410776Z","shell.execute_reply.started":"2022-07-29T03:38:03.645306Z","shell.execute_reply":"2022-07-29T03:38:34.409339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating submission","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv('../input/stanford-sentiment-treebank/test.csv')\n\ndf_test.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:38:40.214568Z","iopub.execute_input":"2022-07-29T03:38:40.214986Z","iopub.status.idle":"2022-07-29T03:38:40.243175Z","shell.execute_reply.started":"2022-07-29T03:38:40.214950Z","shell.execute_reply":"2022-07-29T03:38:40.242274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = vectorizer.transform(df_test['sentence'].tolist())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:39:39.431827Z","iopub.execute_input":"2022-07-29T03:39:39.432273Z","iopub.status.idle":"2022-07-29T03:39:39.461732Z","shell.execute_reply.started":"2022-07-29T03:39:39.432219Z","shell.execute_reply":"2022-07-29T03:39:39.460886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:39:47.497977Z","iopub.execute_input":"2022-07-29T03:39:47.498393Z","iopub.status.idle":"2022-07-29T03:39:47.506253Z","shell.execute_reply.started":"2022-07-29T03:39:47.498356Z","shell.execute_reply":"2022-07-29T03:39:47.505078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:39:50.687392Z","iopub.execute_input":"2022-07-29T03:39:50.687781Z","iopub.status.idle":"2022-07-29T03:39:50.697542Z","shell.execute_reply.started":"2022-07-29T03:39:50.687751Z","shell.execute_reply":"2022-07-29T03:39:50.696531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'id' : df_test['id'].tolist(),\n    'target' : preds\n})","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:40:30.243249Z","iopub.execute_input":"2022-07-29T03:40:30.243643Z","iopub.status.idle":"2022-07-29T03:40:30.250058Z","shell.execute_reply.started":"2022-07-29T03:40:30.243613Z","shell.execute_reply":"2022-07-29T03:40:30.249014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:40:35.768520Z","iopub.execute_input":"2022-07-29T03:40:35.768931Z","iopub.status.idle":"2022-07-29T03:40:35.782618Z","shell.execute_reply.started":"2022-07-29T03:40:35.768899Z","shell.execute_reply":"2022-07-29T03:40:35.781340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T03:40:52.652735Z","iopub.execute_input":"2022-07-29T03:40:52.653151Z","iopub.status.idle":"2022-07-29T03:40:52.663535Z","shell.execute_reply.started":"2022-07-29T03:40:52.653121Z","shell.execute_reply":"2022-07-29T03:40:52.662722Z"},"trusted":true},"execution_count":null,"outputs":[]}]}