{"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":"import matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.svm import LinearSVC","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1ccd71b9149bd29fad3460d706b0aedbea389266"},"cell_type":"code","source":"import string\ndef extracting_metafeatures1(textcol, trainDF):\n    trainDF['char_count'] = trainDF[textcol].apply(len)\n    trainDF['word_count'] = trainDF[textcol].apply(lambda x: len(x.split()))\n    #trainDF['word_density'] = trainDF['char_count'] / (trainDF['word_count']+1)\n    trainDF['punctuation_count'] = trainDF[textcol].apply(lambda x: len(\"\".join(_ for _ in x if _ in string.punctuation))) \n    trainDF['title_word_count'] = trainDF[textcol].apply(lambda x: len([wrd for wrd in x.split() if wrd.istitle()]))\n    trainDF['upper_case_word_count'] = trainDF[textcol].apply(lambda x: len([wrd for wrd in x.split() if wrd.isupper()]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4cb6734e49c590fe0db6824cb82bdc2ee4d360d"},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4d52875b8317ab524b5a2271a3ac91e1b6378fd"},"cell_type":"code","source":"tfidf = TfidfVectorizer(sublinear_tf=True, ngram_range=(1, 2), stop_words='english')\nX_train_dtm = tfidf.fit_transform(train.question_text)\nX_test_dtm = tfidf.transform(test.question_text)\nX_test_dtm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efbd860b671da11439262f04030b483e5d2bd791"},"cell_type":"code","source":"extracting_metafeatures1('question_text',train)\nextracting_metafeatures1('question_text',test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8bdc8fde69db237f8b3cbc29d60ee961ace033dc"},"cell_type":"code","source":"print(train.head())\nprint(test.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"535dfb365d7d8d428c37b3c4d403db18989ea0db"},"cell_type":"code","source":"sns.boxplot(x = 'target', y = 'char_count', data = train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a9ccbe04c27ea018e68b2a0ab57b7af51de9908d"},"cell_type":"code","source":"sns.boxplot(x = 'target', y = 'word_count', data = train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a83ddd801bccb60509a63af2b82e38cf5fcf841"},"cell_type":"code","source":"sns.boxplot(x = 'target', y = 'punctuation_count', data = train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c984010c335d58cf190b4df51c347a1bdb74d1ba"},"cell_type":"code","source":"sns.boxplot(x = 'target', y = 'title_word_count', data = train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8d19e4f0dc77b925ba12f26c0d01cbbffa524e4"},"cell_type":"code","source":"sns.boxplot(x = 'target', y = 'upper_case_word_count', data = train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a25ca5496c388c7783816eb312de8d6672dae3f2"},"cell_type":"code","source":"print(X_train_dtm.shape)\nprint(X_test_dtm.shape)\ntarget = train.target","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"302fec1513cd0f58f68d0fb08a79d575bc8528ba"},"cell_type":"code","source":"from sklearn import preprocessing\nscaler = preprocessing.MinMaxScaler()\nscaled_train = scaler.fit_transform(train[['char_count','word_count','upper_case_word_count','punctuation_count','title_word_count']])\nscaled_test = scaler.fit_transform(test[['char_count','word_count','upper_case_word_count','punctuation_count','title_word_count']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a13dfaee334ac036d4fb1818c568deeb6e6e5b3"},"cell_type":"code","source":"scaled_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"79692faf3d5f11753d61ae6dbfbb76313c90f940"},"cell_type":"code","source":"from scipy.sparse import coo_matrix, hstack\nA = X_train_dtm\nB = coo_matrix(scaled_train)\n\nprint(A.shape)\nprint(B.shape)\nnewtrain = hstack([A,B])\nprint(newtrain.shape)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1274c4837f939630f970592318cff7134566eda8"},"cell_type":"code","source":"C = X_test_dtm\nD = coo_matrix(scaled_test)\n\nprint(C.shape)\nprint(D.shape)\nnewtest = hstack([C,D])\nprint(newtest.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ecd7157d4325daf1fbc8ef74dff34d04ee17bf7"},"cell_type":"code","source":"#Linear SVC\nprint(\"\\nLinear SVC\")\nlogreg = LinearSVC()\n# train the model using X_train_dtm\n%time logreg.fit(newtrain, train.target)\n# make class predictions for X_test_dtm\ny_pred = logreg.predict(newtest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b9f72b9f3c734d75eedf0a7954f43e6ca84f74a"},"cell_type":"code","source":"submission = test[['qid','char_count']]\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c67fea4540f5aff9949dfe2c2073cc12bdd82578"},"cell_type":"code","source":"\nsubmission['prediction'] = y_pred\nsubmission.prediction = submission.prediction.astype(int)\nsubmission.drop('char_count', axis = 1, inplace = True)\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13e1452fdb4aace403bbb388126984d2c479dc06"},"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74e2106a1962ec3f8a073cedc6d995c70c15b86c"},"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}