{"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)\nimport nltk\nfrom nltk.tokenize import TreebankWordTokenizer\nfrom tensorflow import keras\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras import Sequential\nfrom keras.layers import Embedding, Dense, LSTM, Dropout\nfrom keras.preprocessing.text import one_hot\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 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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"training_data=pd.read_csv(\"/kaggle/input/quora-insincere-questions-classification/train.csv\")\ntesting_data=pd.read_csv(\"/kaggle/input/quora-insincere-questions-classification/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"punctuations = ['?',\"'\",'$', '&', '/', '[', ']', '>', '%', '=',',','.','\"', ':', ')', '(', '-', '!', '|', ';', \"'\", '$', '&', '/', '[', ']', '>', '%', '=', '#', '*', '+', '\\\\', '•',  \n '·', '_', '{', '}']\n\ndef clear_text(x):\n    x = str(x)\n    for m in punctuations:\n        x = x.replace(m,'')\n    return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_data[\"question_text\"]=training_data[\"question_text\"].apply(lambda x: clear_text(x))\ntesting_data[\"question_text\"]=testing_data[\"question_text\"].apply(lambda x: clear_text(x))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_Train=training_data[\"question_text\"].str.lower()\nX_Test=testing_data[\"question_text\"].str.lower()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_Train=X_Train.tolist()\nX_Test=X_Test.tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"encoded=[]\nfor n in X_Train:\n    encoded.append(one_hot(n,20000))\nencoded_test=[]\nfor m in X_Test:\n    encoded_test.append(one_hot(m,20000))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"number_of_most_frequent_words=1000\nmax_len=70\nX_encoded = pad_sequences(encoded,maxlen=max_len,padding='post' )\nX_test_encoded = pad_sequences(encoded_test,maxlen=max_len,padding='post' )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Y_Train=training_data[\"target\"]\nprint(Y_Train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Embedding(input_dim=20000, output_dim=128, input_length=70))\nmodel.add(LSTM(units=128, dropout=0.2, recurrent_dropout=0.2))\nmodel.add(Dense(1, activation='sigmoid'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result= model.fit(X_encoded, Y_Train,epochs=2)\nprint(result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred=model.predict_proba(X_test_encoded)\nprint(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testing_data['prediction']=pred\nthreshold=0.50\nfor index, row in testing_data.iterrows():\n    if row['prediction']>0.5:\n        testing_data.prediction[index]=int(1)\n    else:\n        testing_data.prediction[index]=int(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testing_data=testing_data.drop(['question_text'], axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testing_data['prediction']=testing_data['prediction'].astype(np.int64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testing_data.to_csv('submission.csv',index=False) ","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}