{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom keras.layers import Dense , Embedding , SimpleRNN\nfrom keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\n\nimport os\nimport string\nimport nltk\nfrom nltk.corpus import stopwords\nfrom nltk.tokenize import word_tokenize\n\nnltk.download('stopwords')\nnltk.download('punkt')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-12T21:41:48.604767Z","iopub.execute_input":"2023-05-12T21:41:48.605217Z","iopub.status.idle":"2023-05-12T21:41:57.858562Z","shell.execute_reply.started":"2023-05-12T21:41:48.605187Z","shell.execute_reply":"2023-05-12T21:41:57.857624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')\ntest = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')\nsubmission= pd.read_csv('/kaggle/input/quora-insincere-questions-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:41:57.860756Z","iopub.execute_input":"2023-05-12T21:41:57.861536Z","iopub.status.idle":"2023-05-12T21:42:03.288483Z","shell.execute_reply.started":"2023-05-12T21:41:57.861500Z","shell.execute_reply":"2023-05-12T21:42:03.287498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:06.834044Z","iopub.execute_input":"2023-05-12T21:42:06.834403Z","iopub.status.idle":"2023-05-12T21:42:06.854087Z","shell.execute_reply.started":"2023-05-12T21:42:06.834375Z","shell.execute_reply":"2023-05-12T21:42:06.853249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:07.883429Z","iopub.execute_input":"2023-05-12T21:42:07.883788Z","iopub.status.idle":"2023-05-12T21:42:08.593105Z","shell.execute_reply.started":"2023-05-12T21:42:07.883759Z","shell.execute_reply":"2023-05-12T21:42:08.592077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:08.963538Z","iopub.execute_input":"2023-05-12T21:42:08.964342Z","iopub.status.idle":"2023-05-12T21:42:08.974578Z","shell.execute_reply.started":"2023-05-12T21:42:08.964310Z","shell.execute_reply":"2023-05-12T21:42:08.973566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:09.891906Z","iopub.execute_input":"2023-05-12T21:42:09.894506Z","iopub.status.idle":"2023-05-12T21:42:10.100208Z","shell.execute_reply.started":"2023-05-12T21:42:09.894468Z","shell.execute_reply":"2023-05-12T21:42:10.099074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Null in Train:\" , train.isnull().sum())\nprint('Null in Test:', test.isnull().sum()) ","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:10.912921Z","iopub.execute_input":"2023-05-12T21:42:10.913275Z","iopub.status.idle":"2023-05-12T21:42:11.803493Z","shell.execute_reply.started":"2023-05-12T21:42:10.913247Z","shell.execute_reply":"2023-05-12T21:42:11.802560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef preprocess_text(text):\n    # Convert text to lowercase\n    text = text.lower()\n    \n    # Tokenize text into individual words\n    words = word_tokenize(text)\n    \n    # Remove stopwords\n    words = [word for word in words if word not in stopwords.words('english')]\n    \n    # Remove punctuation\n    words = [word for word in words if word not in string.punctuation]\n    \n    # Join the words back into a single string\n    preprocessed_text = ' '.join(words)\n    \n    return preprocessed_text","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:12.296479Z","iopub.execute_input":"2023-05-12T21:42:12.296930Z","iopub.status.idle":"2023-05-12T21:42:12.309052Z","shell.execute_reply.started":"2023-05-12T21:42:12.296894Z","shell.execute_reply":"2023-05-12T21:42:12.307801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:13.058415Z","iopub.execute_input":"2023-05-12T21:42:13.058806Z","iopub.status.idle":"2023-05-12T21:42:13.076357Z","shell.execute_reply.started":"2023-05-12T21:42:13.058775Z","shell.execute_reply":"2023-05-12T21:42:13.075340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train[:10000 ]","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:14.175116Z","iopub.execute_input":"2023-05-12T21:42:14.175793Z","iopub.status.idle":"2023-05-12T21:42:14.180524Z","shell.execute_reply.started":"2023-05-12T21:42:14.175758Z","shell.execute_reply":"2023-05-12T21:42:14.179423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:15.099801Z","iopub.execute_input":"2023-05-12T21:42:15.100751Z","iopub.status.idle":"2023-05-12T21:42:15.109544Z","shell.execute_reply.started":"2023-05-12T21:42:15.100694Z","shell.execute_reply":"2023-05-12T21:42:15.108207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['question_text'] = df['question_text'].apply(lambda d : preprocess_text(d))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:16.073249Z","iopub.execute_input":"2023-05-12T21:42:16.073602Z","iopub.status.idle":"2023-05-12T21:42:35.643503Z","shell.execute_reply.started":"2023-05-12T21:42:16.073569Z","shell.execute_reply":"2023-05-12T21:42:35.642526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train['question_text'][:10000]\ny = train['target'][:10000].values\n\nmax_words = 10000\ntokenizer =  Tokenizer(num_words=max_words)\ntokenizer.fit_on_texts(X)\nsequences = tokenizer.texts_to_sequences(X)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:35.652599Z","iopub.execute_input":"2023-05-12T21:42:35.652989Z","iopub.status.idle":"2023-05-12T21:42:36.247792Z","shell.execute_reply.started":"2023-05-12T21:42:35.652928Z","shell.execute_reply":"2023-05-12T21:42:36.246853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_sequence_length = max(len(seq) for seq in sequences )\nX = pad_sequences(sequences , maxlen=max_sequence_length )","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:36.250485Z","iopub.execute_input":"2023-05-12T21:42:36.250856Z","iopub.status.idle":"2023-05-12T21:42:36.304789Z","shell.execute_reply.started":"2023-05-12T21:42:36.250825Z","shell.execute_reply":"2023-05-12T21:42:36.303975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train , x_test , y_train , y_test =train_test_split(X, y , shuffle=True , test_size=0.2 )","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:36.305977Z","iopub.execute_input":"2023-05-12T21:42:36.306924Z","iopub.status.idle":"2023-05-12T21:42:36.313386Z","shell.execute_reply.started":"2023-05-12T21:42:36.306892Z","shell.execute_reply":"2023-05-12T21:42:36.312503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model =  Sequential()\nmodel.add(Embedding(max_words , 32 , input_length= max_sequence_length))\nmodel.add(SimpleRNN(32))\nmodel.add(Dense(1 , activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:36.314887Z","iopub.execute_input":"2023-05-12T21:42:36.315261Z","iopub.status.idle":"2023-05-12T21:42:40.248427Z","shell.execute_reply.started":"2023-05-12T21:42:36.315230Z","shell.execute_reply":"2023-05-12T21:42:40.247453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:42:40.249634Z","iopub.execute_input":"2023-05-12T21:42:40.249984Z","iopub.status.idle":"2023-05-12T21:42:40.267492Z","shell.execute_reply.started":"2023-05-12T21:42:40.249953Z","shell.execute_reply":"2023-05-12T21:42:40.266693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nmodel.fit(X, y, epochs=10, batch_size=32)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:43:02.730576Z","iopub.execute_input":"2023-05-12T21:43:02.731226Z","iopub.status.idle":"2023-05-12T21:46:26.026407Z","shell.execute_reply.started":"2023-05-12T21:43:02.731194Z","shell.execute_reply":"2023-05-12T21:46:26.025305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}