{"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)\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":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"data= pd.read_csv('../input/train.csv')\ndata.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a96c6395f896503e9303592026fe78417d91a114"},"cell_type":"code","source":"test=pd.read_csv('../input/test.csv')\ntest.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cacebc2a07142d2a2032cd1d0e55d789a4f6dd4c"},"cell_type":"code","source":"sample=pd.read_csv('../input/sample_submission.csv')\nsample.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51c13729118577aa013738c9849746556893a935"},"cell_type":"code","source":"data.target.value_counts()/data.shape[0]*100","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2451db221a9f8cef602d143e7b1b64a09e4b33c2"},"cell_type":"code","source":"import nltk\nimport wordcloud","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2dfb820c6b5e84d14636fa511c4d7c6db618bbd4"},"cell_type":"code","source":"class_0=data[data.target ==0]\nclass_1=data[data.target==1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca5027002806910d5d6a464ed7d7b789570d2ead"},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7e6abf8a7f6b5ff09e301c216f6e847c99527ce"},"cell_type":"code","source":"wc=wordcloud.WordCloud().generate(' '.join(class_0['question_text']))\nplt.imshow(wc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0c38acc3aeaef6e7427f85b12f465b9d7498506e"},"cell_type":"code","source":"wc=wordcloud.WordCloud().generate(' '.join(class_1['question_text']))\nplt.imshow(wc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8564d03980a7a121569cf74aa6110abf737b4612"},"cell_type":"code","source":"docs= data['question_text'].fillna('').str.lower().str.replace('[^a-z ]','')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8382d6efb4a2ab834555ea6a5138b007441c87f9"},"cell_type":"code","source":"stop_words=nltk.corpus.stopwords.words('english')\njunk_words=['will']\nstop_words.extend(junk_words)\nstemmer=nltk.PorterStemmer()\ndocs_clean= docs.apply(lambda v: ' '.join([stemmer.stem(word) for word in v.split(' ') if word not in stop_words]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07ecad05d43686251eaa686c39dd8c171b023c6c"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain, validate = train_test_split(docs_clean, test_size=0.3, random_state = 100)\ntrain_y=data.loc[train.index]['target']\nvalidate_y=data.loc[validate.index]['target']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4cccd240a85054ee85390e3ddff2ed2a31578dd3"},"cell_type":"code","source":"from sklearn.feature_extraction.text import CountVectorizer\n\ncv= CountVectorizer()\ncv.fit(train)\ntrain_x_sparse=cv.transform(train)\nvalidate_x_sparse= cv.transform(validate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"104038182f735af4ac84eacd631025d8a74ae634"},"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\ndt_model= DecisionTreeClassifier(max_depth=20, random_state=100)\ndt_model.fit(train_x_sparse, train_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a5762e9047799eeac8b682a5c3807355e934bf0"},"cell_type":"code","source":"pred_class = dt_model.predict(validate_x_sparse)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c3d3f47806ce5dc09ee2fc92c2a5409c0e19a89"},"cell_type":"code","source":"from sklearn.metrics import accuracy_score, f1_score, roc_curve, auc","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5338e1a7564c99c7b8233aab9d4921ab473f1088"},"cell_type":"code","source":"print(accuracy_score(validate_y, pred_class))\nprint(f1_score(validate_y, pred_class))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5febc12624ecdf5ce6de125276886b637495aee3"},"cell_type":"code","source":"pred_probs= pd.DataFrame(dt_model.predict_proba(validate_x_sparse), columns=['Sincere', 'Insincere'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce6ff3b844ddd511f9ebce60acbed3462ce1ae0f"},"cell_type":"code","source":"fpr, tpr, thresholds= roc_curve(validate_y, pred_probs['Insincere'])\nauc_dt=auc(fpr, tpr)\nplt.plot(fpr,tpr)\nplt.legend(['Decision Tree - AUC: %.2f ' % auc_dt])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5398591ca8c5dd06b56c17b7c47f513275fcb7d2"},"cell_type":"code","source":"test= pd.read_csv('../input/test.csv')\ntest_docs=test['question_text'].fillna('').str.lower()\ntest_docs=text= test_docs.str.replace('[^a-z ]','')\ntest_docs_clean= test_docs.apply(lambda v: ' '.join([stemmer.stem(word) for word in v.split(' ') if word not in stop_words]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2bc1fd5f568b168ffaf79f26db001f2026b73244"},"cell_type":"code","source":"test_docs_clean.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e9553bb9d08a48c475ae0d34d1c437d8aa4155f"},"cell_type":"code","source":"test_x = cv.transform(test_docs_clean)\ntest_pred_class=dt_model.predict(test_x)\ntest_pred_class.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cedeaed41e7491af93a468ee2ea163d7ff21c6b"},"cell_type":"code","source":"submission= pd.DataFrame({'qid':test['qid'], 'prediction': test_pred_class})\nsubmission.to_csv('submission.csv',index=False)\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c5dcfe0327add7738f77bf80dd89d2bfc5c7adc7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}