{"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\")\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68f07e7cfe4c4b0eb4a8f9e74cded9967dab4d57"},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ae72c86f46200cea963c14dc4be5fdd25a578b29"},"cell_type":"markdown","source":" **Target Variable**\n* 0 - sincere\n* 1 - insincere"},{"metadata":{"trusted":true,"_uuid":"6f5e8f7c1e35f96bfccca13b9e74fa06de4f3100"},"cell_type":"code","source":"data.iloc[0]['question_text']  # sincere","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0547a1dfe50204ef7543807f64bbb44190cca024"},"cell_type":"code","source":"data.iloc[22]['question_text']  # insincere","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"362566bfe29aa03d63c3c015cc1ece78ad3fac2d"},"cell_type":"code","source":"data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9d0f950dfd2105184d18cfa3f26c7a64fea3f19f"},"cell_type":"code","source":"data['target'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4dd5847f53740065a312da92be4ad1465148be41"},"cell_type":"markdown","source":"This is a highly imbalanced class. Accuracy wont be the right measure. \nSo, F1 Score is the evaluation metric."},{"metadata":{"trusted":true,"_uuid":"40bf3beb1ac331fca1fc4aca1b43636f2c491133"},"cell_type":"code","source":"data['target'].value_counts() / data.shape[0] * 100","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e353f862890ef548f5975daab7ec5ccac7c10ac3"},"cell_type":"code","source":"from wordcloud import WordCloud\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b3816563ece6c243345053d6f60bd8dd17113d6"},"cell_type":"code","source":"question_string = ' '.join(data['question_text'])\nwc = WordCloud().generate(question_string)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"575ee67f3b26294301ad1935c61633f133c9f754"},"cell_type":"code","source":"plt.imshow(wc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0701ecc78ecec23e8fc47347c10adca3402cf594"},"cell_type":"code","source":"insincere_questions = data[data['target'] == 1]\nwc = WordCloud().generate(' '.join(insincere_questions['question_text']))\nplt.imshow(wc)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8d5cc171a94820726ceb36dd7ba6a1ff7e7ad0a2"},"cell_type":"markdown","source":"**Text Cleaning/ Text Transformation**"},{"metadata":{"trusted":true,"_uuid":"c97f9197a17c23301b382eb8c0a9daa9e73aa8c7"},"cell_type":"code","source":"# Conver all characters to lower case\ndocs = data['question_text'].str.lower()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"548f8beb3c9065fefa6d7baf0b257f14a641d5a0"},"cell_type":"code","source":"docs.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0cebb0a41468ca6c318875af0a9652a51bebfcba"},"cell_type":"code","source":"# Apply regex to retain only alphabets\ndocs = docs.str.replace('[^a-z ]', '')\ndocs.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6620823b1746aee9e74e89831fcd5d6803263bf5"},"cell_type":"code","source":"# Remove commonly used words\nimport nltk\nstopwords = nltk.corpus.stopwords.words('english')\nstopwords","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f08d6573d19c088c3f0c5409a8ef46c5aca289e8"},"cell_type":"code","source":"def remove_stopwords(text):\n    words = nltk.word_tokenize(text)\n    print(words)\n    print('------------')\ndocs.head(2).apply(remove_stopwords)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d47b8bd38af3bad65a0a689250b89bbf68ca07a3"},"cell_type":"code","source":"def remove_stopwords(text):\n    print(text)\n    words = nltk.word_tokenize(text)\n    words = [word for word in words if word not in stopwords] # remove stopwords and add to words\n    print(words)\n    print('------------')\ndocs.head(2).apply(remove_stopwords)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9275a6bfd97b90037aa6693429303b5b5aad07ac"},"cell_type":"code","source":"stemmer = nltk.stem.PorterStemmer()\nstemmer.stem('playing')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"43b5b6bbdc912dccf3c492ae298bd9c5a334283d"},"cell_type":"code","source":"stemmer = nltk.stem.PorterStemmer()\ndef remove_stopwords(text):\n    print(text)\n    words = nltk.word_tokenize(text)\n    words = [stemmer.stem(word) for word in words if word not in stopwords] # remove stopwords and add to words\n    print(words)\n    print('------------')\ndocs.head(2).apply(remove_stopwords)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"stemmer = nltk.stem.PorterStemmer()\ndef remove_stopwords(text):\n    words = nltk.word_tokenize(text)\n    words = [stemmer.stem(word) for word in words if word not in stopwords] # remove stopwords and add to words\n    return ' '.join(words)\ndocs_clean = docs.apply(remove_stopwords)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7196e2b1ebc43f323659858813c3e66c5cc8e037"},"cell_type":"code","source":"docs_clean.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"26d6ecc437b89e0a2ba7496cd73204bde64ea44b"},"cell_type":"code","source":"from sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.model_selection import train_test_split\n\ntrain, validate = train_test_split(docs_clean,\n                                  test_size=0.3,\n                                  random_state=100)\nvectorizer = CountVectorizer()\nvectorizer.fit(train)\ntrain_dtm = vectorizer.transform(train)\nvalidate_dtm = vectorizer.transform(validate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"039487e5e8c98f1079601d4ee63fb23523e642af"},"cell_type":"code","source":"train_dtm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"def518ee807e440c41aeba361219377e62998c7f"},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4594e5f2ff8f06d77b0fbdaa1887cff094eb331b"},"cell_type":"code","source":"perc_non_zero_values = 5628198/(914285*143417) * 100\nperc_non_zero_values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4a49bcf2d554bb32056cc37f5eaa992fa746b44"},"cell_type":"code","source":"pd.DataFrame(train_dtm[:5].toarray(),columns=vectorizer.get_feature_names())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4963af219059513e444f2794260d338a47ee7356"},"cell_type":"code","source":"train_x = train_dtm\nvalidate_x = validate_dtm\n\ntrain_y = data.loc[train.index]['target']\nvalidate_y = data.loc[validate.index]['target']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6aa3479fad7d5ec93e16089c6f5f59783c6ddd49"},"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nmodel_rf = RandomForestClassifier(n_estimators=20,\n                                 random_state=100)\nmodel_rf.fit(train_x,train_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a73346cf544e49889fdf97f9a48727b704147bdc"},"cell_type":"code","source":"validate_pred_class = model_rf.predict(validate_x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60244bb0ae5b2500c88dfeec34a2b15e7eb878ef"},"cell_type":"code","source":"validate_pred_class","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}