{"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 pandas as pd\nimport numpy as np\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction import DictVectorizer\n\n\ndata = pd.read_csv(\"../input/train.csv\",low_memory=False)\ndata.target = data.target.apply(lambda x: int(x))\n\n#data = numpy.array(data)  #convert array to numpy type array\n\n\ndf_train ,df_test = train_test_split(data,test_size=0.2)\n\n# Y_train = df_train.iloc[0:, 2].values\n# text_train = df_train.iloc[0:, 1].values\n# vect = DictVectorizer()\n# #vec = CountVectorizer()\n# #X_train = vec.fit_transform(text_train)\n# #feature_names = np.asarray(vect.get_feature_names())\n\n\n# Y_test = df_test.iloc[0:, 2].values\n# text_test = df_test.iloc[0:, 1].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d6acdaca6b2536138a599e29dac69a2fae0a870","scrolled":true},"cell_type":"code","source":"from sklearn.feature_extraction.text import TfidfVectorizer\n# import xgboost as xgb\nfrom sklearn.linear_model import LogisticRegression\n\nvect = TfidfVectorizer(ngram_range=(1,3), min_df=2)\nvect.fit(data.question_text)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d8918a863e45e6bdc8a93f37d4208604f563771"},"cell_type":"code","source":"#from sklearn.feature_selection import SelectPercentile, chi2\n#selection = SelectPercentile(percentile=5, score_func=chi2)\n#X_train_selected = selection.fit_transform(vect.transform(df_train.question_text), df_train.target)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"0dfa0413233faf3b71450101c01bc8cf022417d0"},"cell_type":"code","source":"# train classifier\nclf = LogisticRegression(solver='lbfgs', class_weight={1:8, 0:1}) # 'balanced'\nclf.fit(vect.transform(df_train.question_text), df_train.target)#vect.transform(df_train.question_text)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90b9daa8319844b06c18aa8c93617f14450a5422","scrolled":false},"cell_type":"code","source":"# evaluate\nfrom sklearn.metrics import classification_report\ny_preds = clf.predict(vect.transform(df_test.question_text))\nevaluation = classification_report(df_test.target, y_preds, digits=3)\nprint(evaluation)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f6dbcbcbec9ea8131296ed911adcffe936f62e7"},"cell_type":"code","source":"# conduct test experiments\n\ntest_data = pd.read_csv('../input/test.csv')\ntest_labels = clf.predict(vect.transform(test_data.question_text))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f023df7a8619d100fc65ca72c89cf2d649a4d69"},"cell_type":"code","source":"# output the test results\ntest_data['prediction'] = test_labels\nsub_data = test_data[['qid', 'prediction']] \nsub_data.to_csv('./submission.csv', index=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8c487d74d13fa44b5209c8b71eb48774840c831"},"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}