{"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\"))\ndata=pd.read_csv('../input/train.csv')\ntest=pd.read_csv('../input/test.csv')\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.shape,test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8ece50fee343e95efab47b3ea55ab1c37b73d50"},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7003b39ba116ad7e58cf8668ec87baf0e974b72f"},"cell_type":"code","source":"from wordcloud import WordCloud\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78b1978b985740a2a60b062b9bd0747c0f20de60"},"cell_type":"code","source":"doc=data['question_text'].str.lower()\ndoc_clean=doc.str.replace('[^a-z ]','')\nsincere_index=data[data['target']==0].index\nwc=WordCloud().generate(' '.join(doc_clean.loc[sincere_index]))\nplt.imshow(wc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85585bac1ca35d02d3fef4b6923a8c67ae09672e"},"cell_type":"code","source":"doc=data['question_text'].str.lower()\ndoc_clean=doc.str.replace('[^a-z ]','')\ninsincere_index=data[data['target']==1].index\nwc=WordCloud().generate(' '.join(doc_clean.loc[insincere_index]))\nplt.imshow(wc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3735febe9bee13b15eecafa68280e0feadc98d1"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain,validate=train_test_split(data,test_size=0.3,random_state=100)\n(train.shape, validate.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27bd526dcdb8675828916190f38e2a302647991f"},"cell_type":"code","source":"train['target'].value_counts()/train.shape[0],validate['target'].value_counts()/validate.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37afbe95a6f7967305c72e2571b221e87aeac725"},"cell_type":"code","source":"from sklearn.feature_extraction.text import CountVectorizer\ndtm_func=CountVectorizer(stop_words='english',min_df=.5)\ndtm_func.fit(doc_clean.loc[train.index])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c91a2e258d32f41ef1d39088fc3c8394a4bcf50c"},"cell_type":"code","source":"dtm_matrix=dtm_func.transform(doc_clean.loc[train.index])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2d9f8f46dd733daf10615e947ab48e25f223931"},"cell_type":"code","source":"validate_dtm_matrix=dtm_func.transform(doc_clean.loc[validate.index])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7bd4d985a40c6918328e5c563c446f14f874d88d"},"cell_type":"code","source":"train_x=dtm_matrix\ntrain_y=train['target']\nvalidate_x=validate_dtm_matrix\nvalidate_y=validate['target']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca9b29bde0d0f9002b83324ce18dad5dc93e90f5"},"cell_type":"code","source":"validate_dtm_matrix","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d624882c2e16584c20d58bb1f09e4a053bb53e6e"},"cell_type":"markdown","source":"from sklearn.tree import DecisionTreeClassifier\ndt_model=DecisionTreeClassifier(max_depth=30,random_state=100)\ndt_model.fit(train_x,train_y)"},{"metadata":{"trusted":true,"_uuid":"79b06c863594b70e1813b2672e11fb29be2ec0d7"},"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\ndt_model=RandomForestClassifier(n_estimators=300,max_depth=30,random_state=100)\ndt_model.fit(train_x,train_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58086105c9d3860897b9b1269a3e13e6bab8a608"},"cell_type":"markdown","source":"pred_validate=dt_model.predict_proba(validate_x)"},{"metadata":{"_uuid":"e9ab98570bcb2dcbaf1f9cd0cdf0a3b35265b31f"},"cell_type":"markdown","source":"from sklearn.metrics import accuracy_score, f1_score\nprint('Accuracy score: %.2f'% accuracy_score(validate_y,pred_validate))\nprint('F1 score: %.2f'% f1_score(validate_y,pred_validate))"},{"metadata":{"_uuid":"e666da1b8057ebbee50d6d65747b2b66537403e3"},"cell_type":"markdown","source":"from sklearn.tree import DecisionTreeClassifier\n\nmax_depth=[]\nf1_score1=[]\nfor i in np.arange(4,40,2):\n    dt_model=DecisionTreeClassifier(max_depth=i,random_state=100)\n    dt_model.fit(train_x,train_y)\n    max_depth.append(i)\n    pred_validate=dt_model.predict(validate_x)\n    score=f1_score(validate_y,pred_validate)\n    f1_score1.append(score)\nplt.plot(max_depth,f1_score1)\n    \n    "},{"metadata":{"_uuid":"ce8206e93018c29a21a101f43dabdc41774bbcc4","trusted":true},"cell_type":"code","source":"pred_validate=dt_model.predict_proba(validate_x)\nthreshold=[]\nf1_score1=[]\nrecall=[]\nfor i in np.arange(0.1,0.9,.01):\n    threshold.append(i)\n    x=np.where(pred_validate>i,1,0)\n    score=f1_score(validate_y,x[:,1])\n    recall.append(metrics.recall_score(validate_y,x[:,1]))\n    f1_score1.append(score)\nplt.plot(threshold,f1_score1)\nplt.plot(threshold,recall,label='recall')\nplt.legend(loc='best')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40309f87dcf9837c45742c69666caf314e19bf47"},"cell_type":"code","source":"pred_validate=dt_model.predict_proba(validate_x)\nx=np.where(pred_validate>.133034,1,0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ab204a51f7bbd2ac706c304e703fef20682b2e3"},"cell_type":"code","source":"pd.Series(x[:,1]).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d915b30a5dc90138f41d691d910fd31e6f626c7a"},"cell_type":"code","source":"validate_y.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"930db5b3e7032af3a21f0b5d984d979c49944aa0"},"cell_type":"code","source":"doc_clean_test=test['question_text'].str.lower().str.replace('[^a-z ]','')\ntest_dtm_matrix=dtm_func.transform(doc_clean_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d8d7e19c2a98db5a1b76856774b7ae1e84aa7b4"},"cell_type":"code","source":"pred_test=dt_model.predict_proba(test_dtm_matrix)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6afb24326a59cc2f40c319df96bf5f532acde2ff"},"cell_type":"code","source":"submission=pd.Series(np.where(pred_test[:,1]>.133034,1,0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37b8b20214e89a64661a41b064c5a7768b4a6a43"},"cell_type":"code","source":"submission=pd.DataFrame({'qid':test['qid'],'prediction':submission})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5eac22f0e80f57a7aa1c32c7de70e759135d671c"},"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a25a3000991c5c6213b71f3aac8fc207dee1b1f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c43f625afbb4fa555269408ba326e713d7d8a69"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e9aa3f034162b6ef45b0497448336e23e33d5061"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2fb1248bd23e93f9ea37aaf78e64ae4bb46febf7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca04194e49662641ecc433585218db9aa3ecf5e0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77f72c069ebfa9b720be3e1e84fd63fdeb422156"},"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}