{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas\ntrain_file_path = \"/kaggle/input/quora-insincere-questions-classification/train.csv\"\ntrain_file = pandas.read_csv(train_file_path)\ntrain_file.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file.info()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\nimport string\nalphanumeric = lambda x: re.sub('\\w*\\d\\w*', ' ', x)\npunc_lower = lambda x: re.sub('[%s]' % re.escape(string.punctuation), ' ', x.lower())\nremove_n = lambda x: re.sub(\"\\n\", \" \", x)\nremove_non_ascii = lambda x: re.sub(r'[^\\x00-\\x7f]',r' ', x)\ntrain_file['question_text'] = train_file['question_text'].map(alphanumeric).map(punc_lower).map(remove_n).map(remove_non_ascii)\ntrain_file['question_text'][0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot\nimport seaborn\nimport numpy\nimport pandas\n\ndata_rate = train_file['target'].sum()/train_file['target'].count()\ndata_rate","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nltk\nfrom nltk.corpus import stopwords\nnltk.download('stopwords')\nset(stopwords.words('english'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.naive_bayes import MultinomialNB, BernoulliNB\nfrom sklearn.svm import LinearSVC\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import f1_score\n\n# train_data_1 = train_file[train_file['target']==1].iloc[:,:]\n# train_data_0 = train_file[train_file['target']==0].iloc[:int(len(train_data_1)/1),:]\n# train_data = pandas.concat([train_data_1,train_data_0],axis=0)\n\ntrain_data = train_file\n\nX_train = train_data.question_text\ny_train = train_data.target\n\ncv = CountVectorizer(ngram_range=((1,1)), stop_words='english')\n\nX_train_cv = cv.fit_transform(X_train)\n\nlr = LogisticRegression()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr.fit(X_train_cv, y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_file_path = \"/kaggle/input/quora-insincere-questions-classification/test.csv\"\ntest_file = pandas.read_csv(test_file_path)\ntest_file.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_file['question_text'] = test_file['question_text'].map(alphanumeric).map(punc_lower).map(remove_n).map(remove_non_ascii)\ntest_file['question_text'][0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_cv = cv.transform(test_file.question_text)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = lr.predict(X_test_cv)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pandas.DataFrame()\ndf['qid'] = test_file['qid']\ndf['prediction'] = result\ndf.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}