{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input\"))\nimport nltk\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/train.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad93c14c9b9cb9125ee43de4c3154d87fbc8327f"},"cell_type":"code","source":"docs = df['question_text']\nlen(docs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"98024619d433e2f776c022b0e7f6a5cd316055bf"},"cell_type":"code","source":"df_0 = df[df['target']==0]\ndocs_0 = df_0['question_text']\ndf_1 = df[df['target']==1]\ndocs_1 = df_1['question_text']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cbf27de973828c7e3e5b4c8e01e871a198b186c3"},"cell_type":"code","source":"!pip install wordcloud","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c080225cdaed1a73b2a4d640dc940c1e9662c22"},"cell_type":"code","source":"from wordcloud import WordCloud\nstopwords = nltk.corpus.stopwords.words('english')\nwc = WordCloud(background_color = 'white',stopwords=stopwords).generate(' '.join(docs_0))\nplt.figure(figsize=(16,8))\nplt.imshow(wc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2d1eaeb23dbe6cc3ae43caa041f96261c2ec360"},"cell_type":"code","source":"from wordcloud import WordCloud\nstopwords = nltk.corpus.stopwords.words('english')\nwc = WordCloud(background_color = 'white',stopwords=stopwords).generate(' '.join(docs_1))\nplt.figure(figsize=(16,8))\nplt.imshow(wc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"843e0328e04b8c813d706faf7c605b42b2444a15","scrolled":true},"cell_type":"code","source":"docs = docs.str.lower().str.replace('[^a-z ]', '')\ndocs.head()\ndocs.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd6252ea23d937f1439fba906cb869ee384cec2b"},"cell_type":"code","source":"stopwords = nltk.corpus.stopwords.words('english')\nstemmer = nltk.stem.PorterStemmer()\n\ndef clean_sentence(doc):\n    words = doc.split(' ')\n    words_clean = [stemmer.stem(word) for word in words if word not in stopwords]\n    doc_clean = ' '.join(words_clean)\n    return doc_clean\n\ndocs_clean = docs.apply(clean_sentence)\ndocs_clean.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc6550f04a6d0552efb729681a370b8b53f3c5ab"},"cell_type":"code","source":"from sklearn.feature_extraction.text import CountVectorizer\nvectorizer = CountVectorizer().fit(docs_clean)\ndtm = vectorizer.transform(docs_clean)\ndtm","execution_count":null,"outputs":[]},{"metadata":{"scrolled":true,"trusted":true,"_uuid":"cd11427db768b8601189c1b6c7f980e7c5aac473"},"cell_type":"code","source":"non_zero = 8041045\nzeors_count = (1306122 * 178080) - non_zero\nsparsity = zeors_count/(1306122 * 178080) * 100\nsparsity","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e66eaecc310d9d2b019355a436eb13928acf0677"},"cell_type":"code","source":"docs_clean.shape\ndf['target'].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c77a099a73a0e933dce690f118cf96e3d616c11c"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_x,test_x,train_y,test_y = train_test_split(docs_clean,\n                                                 df['target'],\n                                                 test_size = 0.2,\n                                                 random_state = 100)\nvectorizer = CountVectorizer(min_df=50).fit(train_x)\ntrain_x = vectorizer.transform(train_x)\ntest_x = vectorizer.transform(test_x)\ntrain_x.shape,test_x.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ca08ffd924a422e371dec9f3bec451be594a896"},"cell_type":"code","source":"from sklearn.naive_bayes import MultinomialNB\nmodel_mnb1 = MultinomialNB().fit(train_x,train_y)\ntest_pred = model_mnb1.predict(test_x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59813426e472fd224ea9aeb3c9b72024b754d9b7"},"cell_type":"code","source":"from sklearn.metrics import classification_report,f1_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efc043bbdf426dfb803cf3c1708bfc5723bedeac"},"cell_type":"code","source":"f1_score(test_y,test_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49f37fad25291fd483908421868ad772e197b6b4"},"cell_type":"code","source":"df_test = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c366847e377bb3c0f2838e3aa576d6e3be166ae3"},"cell_type":"code","source":"docs = df_test['question_text']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da02da0841fe6321b9c04968ba434941a3523fa7"},"cell_type":"code","source":"docs = docs.str.lower().str.replace('[^a-z ]', '')\ndocs.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38a3870c32dc5eaa96ce2b18d5b33d04a792d501"},"cell_type":"code","source":"stopwords = nltk.corpus.stopwords.words('english')\nstemmer = nltk.stem.PorterStemmer()\n\ndef clean_sentence(doc):\n    words = doc.split(' ')\n    words_clean = [stemmer.stem(word) for word in words if word not in stopwords]\n    doc_clean = ' '.join(words_clean)\n    return doc_clean\n\ndocs_clean = docs.apply(clean_sentence)\ndocs_clean.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c5274ab06b9ef6a3abc2e0c1b9e183f3404b5ad6"},"cell_type":"code","source":"docs_clean = vectorizer.transform(docs_clean)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9297fa43f29f23bc866c67a53dc4118bdec7d1c"},"cell_type":"code","source":"final = model_mnb1.predict(docs_clean)\nfinal = pd.DataFrame({'qid':df_test['qid'],'prediction':final})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a45b96a10cf114d625b0c30fbc5f49086169c706"},"cell_type":"code","source":"final.to_csv('submission.csv',index=False)","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}