{"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\n'''for 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","execution":{"iopub.status.busy":"2023-08-18T23:06:21.355607Z","iopub.execute_input":"2023-08-18T23:06:21.355972Z","iopub.status.idle":"2023-08-18T23:06:21.374288Z","shell.execute_reply.started":"2023-08-18T23:06:21.355942Z","shell.execute_reply":"2023-08-18T23:06:21.373372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:21.390488Z","iopub.execute_input":"2023-08-18T23:06:21.391500Z","iopub.status.idle":"2023-08-18T23:06:26.527505Z","shell.execute_reply.started":"2023-08-18T23:06:21.391463Z","shell.execute_reply":"2023-08-18T23:06:26.526403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:26.533342Z","iopub.execute_input":"2023-08-18T23:06:26.536168Z","iopub.status.idle":"2023-08-18T23:06:26.559895Z","shell.execute_reply.started":"2023-08-18T23:06:26.536131Z","shell.execute_reply":"2023-08-18T23:06:26.558822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:26.564661Z","iopub.execute_input":"2023-08-18T23:06:26.567523Z","iopub.status.idle":"2023-08-18T23:06:26.599755Z","shell.execute_reply.started":"2023-08-18T23:06:26.567479Z","shell.execute_reply":"2023-08-18T23:06:26.598775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_accuracy = (1225312/(1225312+80810))**2 + (80810/(1225312+80810))**2\nbase_accuracy*100","metadata":{"execution":{"iopub.status.busy":"2023-08-19T03:35:18.636058Z","iopub.execute_input":"2023-08-19T03:35:18.636428Z","iopub.status.idle":"2023-08-19T03:35:18.644024Z","shell.execute_reply.started":"2023-08-19T03:35:18.636398Z","shell.execute_reply":"2023-08-19T03:35:18.643106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.target.value_counts().plot(kind='barh')","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:26.605728Z","iopub.execute_input":"2023-08-18T23:06:26.608370Z","iopub.status.idle":"2023-08-18T23:06:26.961995Z","shell.execute_reply.started":"2023-08-18T23:06:26.608314Z","shell.execute_reply":"2023-08-18T23:06:26.961003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lens_text = train_data.question_text.apply(lambda x: len(x.split()))","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:26.966498Z","iopub.execute_input":"2023-08-18T23:06:26.968855Z","iopub.status.idle":"2023-08-18T23:06:29.457762Z","shell.execute_reply.started":"2023-08-18T23:06:26.968820Z","shell.execute_reply":"2023-08-18T23:06:29.456740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lens_text.hist(bins = 15)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:29.459121Z","iopub.execute_input":"2023-08-18T23:06:29.459507Z","iopub.status.idle":"2023-08-18T23:06:29.788262Z","shell.execute_reply.started":"2023-08-18T23:06:29.459471Z","shell.execute_reply":"2023-08-18T23:06:29.787260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.quantile(lens_text, 0.980)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:29.789781Z","iopub.execute_input":"2023-08-18T23:06:29.790137Z","iopub.status.idle":"2023-08-18T23:06:29.814047Z","shell.execute_reply.started":"2023-08-18T23:06:29.790103Z","shell.execute_reply":"2023-08-18T23:06:29.813107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEQUENCE_LEN = 35","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:29.815603Z","iopub.execute_input":"2023-08-18T23:06:29.816045Z","iopub.status.idle":"2023-08-18T23:06:29.820928Z","shell.execute_reply.started":"2023-08-18T23:06:29.816012Z","shell.execute_reply":"2023-08-18T23:06:29.819857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split data to train and validation set","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:29.822750Z","iopub.execute_input":"2023-08-18T23:06:29.823446Z","iopub.status.idle":"2023-08-18T23:06:30.438041Z","shell.execute_reply.started":"2023-08-18T23:06:29.823410Z","shell.execute_reply":"2023-08-18T23:06:30.437094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(train_data.question_text.str.lower(), train_data.target, test_size=0.2, random_state=123)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:30.442862Z","iopub.execute_input":"2023-08-18T23:06:30.443145Z","iopub.status.idle":"2023-08-18T23:06:31.249545Z","shell.execute_reply.started":"2023-08-18T23:06:30.443121Z","shell.execute_reply":"2023-08-18T23:06:31.248575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape, x_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:31.250976Z","iopub.execute_input":"2023-08-18T23:06:31.251437Z","iopub.status.idle":"2023-08-18T23:06:31.259376Z","shell.execute_reply.started":"2023-08-18T23:06:31.251402Z","shell.execute_reply":"2023-08-18T23:06:31.258376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.value_counts().plot(kind='barh')","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:31.260754Z","iopub.execute_input":"2023-08-18T23:06:31.261823Z","iopub.status.idle":"2023-08-18T23:06:31.494247Z","shell.execute_reply.started":"2023-08-18T23:06:31.261791Z","shell.execute_reply":"2023-08-18T23:06:31.493363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = np.array(y_train)\ny_val = np.array(y_val)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:31.495630Z","iopub.execute_input":"2023-08-18T23:06:31.496404Z","iopub.status.idle":"2023-08-18T23:06:31.504044Z","shell.execute_reply.started":"2023-08-18T23:06:31.496370Z","shell.execute_reply":"2023-08-18T23:06:31.503075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(x_train)\nx_val = np.array(x_val)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:31.505467Z","iopub.execute_input":"2023-08-18T23:06:31.505965Z","iopub.status.idle":"2023-08-18T23:06:31.567340Z","shell.execute_reply.started":"2023-08-18T23:06:31.505929Z","shell.execute_reply":"2023-08-18T23:06:31.566365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train[10]","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:31.568803Z","iopub.execute_input":"2023-08-18T23:06:31.569183Z","iopub.status.idle":"2023-08-18T23:06:31.575553Z","shell.execute_reply.started":"2023-08-18T23:06:31.569148Z","shell.execute_reply":"2023-08-18T23:06:31.574667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val[10]","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:31.576938Z","iopub.execute_input":"2023-08-18T23:06:31.577783Z","iopub.status.idle":"2023-08-18T23:06:31.589206Z","shell.execute_reply.started":"2023-08-18T23:06:31.577750Z","shell.execute_reply":"2023-08-18T23:06:31.587936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load glove embeddings","metadata":{}},{"cell_type":"code","source":"from zipfile import ZipFile","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:31.590935Z","iopub.execute_input":"2023-08-18T23:06:31.591300Z","iopub.status.idle":"2023-08-18T23:06:31.600757Z","shell.execute_reply.started":"2023-08-18T23:06:31.591268Z","shell.execute_reply":"2023-08-18T23:06:31.599892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeddings = '/kaggle/input/quora-insincere-questions-classification/embeddings.zip'\nglove_file_name = \"glove.840B.300d/glove.840B.300d.txt\"\n# loading the temp.zip and creating a zip object\nwith ZipFile(embeddings, 'r') as z_file:\n    z_file.extract(glove_file_name, path=\"/kaggle/working/embeddings\")","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:06:31.602264Z","iopub.execute_input":"2023-08-18T23:06:31.602695Z","iopub.status.idle":"2023-08-18T23:07:41.351964Z","shell.execute_reply.started":"2023-08-18T23:06:31.602665Z","shell.execute_reply":"2023-08-18T23:07:41.350948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create TextVectorization","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import TextVectorization\nvectorization = TextVectorization(output_sequence_length=SEQUENCE_LEN)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:07:41.355285Z","iopub.execute_input":"2023-08-18T23:07:41.355937Z","iopub.status.idle":"2023-08-18T23:07:53.669835Z","shell.execute_reply.started":"2023-08-18T23:07:41.355903Z","shell.execute_reply":"2023-08-18T23:07:53.668787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorization.adapt(x_train)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:07:53.671336Z","iopub.execute_input":"2023-08-18T23:07:53.671678Z","iopub.status.idle":"2023-08-18T23:12:16.159899Z","shell.execute_reply.started":"2023-08-18T23:07:53.671646Z","shell.execute_reply":"2023-08-18T23:12:16.158814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_size = vectorization.vocabulary_size()\nvocab_size","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:12:16.161784Z","iopub.execute_input":"2023-08-18T23:12:16.163261Z","iopub.status.idle":"2023-08-18T23:12:16.171192Z","shell.execute_reply.started":"2023-08-18T23:12:16.163220Z","shell.execute_reply":"2023-08-18T23:12:16.169961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocabulary = vectorization.get_vocabulary()\nvocabulary = dict((w,i) for i, w in enumerate(vocabulary))","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:12:16.173108Z","iopub.execute_input":"2023-08-18T23:12:16.173472Z","iopub.status.idle":"2023-08-18T23:12:17.199398Z","shell.execute_reply.started":"2023-08-18T23:12:16.173441Z","shell.execute_reply":"2023-08-18T23:12:17.198267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocabulary['hello']","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:12:17.201049Z","iopub.execute_input":"2023-08-18T23:12:17.201446Z","iopub.status.idle":"2023-08-18T23:12:17.209271Z","shell.execute_reply.started":"2023-08-18T23:12:17.201408Z","shell.execute_reply":"2023-08-18T23:12:17.208247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_embedding_matrix_glove(word_index, glove_file_name):\n    def get_coef(word,*arg):\n        return word, np.array(arg, dtype=np.float32)\n    embedding_index=dict(get_coef(*o.split(' ')) for o in open(glove_file_name))\n    all_embs=np.stack(embedding_index.values())\n    embed_mean=all_embs.mean()\n    embed_std=all_embs.std()\n    embed_size = all_embs.shape[1]\n    # create matrix\n    embedding_matrix = np.random.normal(embed_mean,embed_std,(len(word_index)+1,embed_size))\n    for w, i in word_index.items():\n        if w in embedding_index.keys():\n            embedding_vector=embedding_index.get(w)\n            if embedding_vector is not None: embedding_matrix[i] = embedding_vector\n            embedding_matrix[i]=embedding_vector\n    \n    return embedding_matrix","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:12:17.210738Z","iopub.execute_input":"2023-08-18T23:12:17.211539Z","iopub.status.idle":"2023-08-18T23:12:17.221058Z","shell.execute_reply.started":"2023-08-18T23:12:17.211475Z","shell.execute_reply":"2023-08-18T23:12:17.220024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_name=\"/kaggle/working/embeddings/glove.840B.300d/glove.840B.300d.txt\"\n\nembedding_matrix=get_embedding_matrix_glove(vocabulary,file_name)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:12:17.222472Z","iopub.execute_input":"2023-08-18T23:12:17.222991Z","iopub.status.idle":"2023-08-18T23:15:36.793421Z","shell.execute_reply.started":"2023-08-18T23:12:17.222953Z","shell.execute_reply":"2023-08-18T23:15:36.792367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_matrix.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:15:36.795561Z","iopub.execute_input":"2023-08-18T23:15:36.795918Z","iopub.status.idle":"2023-08-18T23:15:36.804815Z","shell.execute_reply.started":"2023-08-18T23:15:36.795885Z","shell.execute_reply":"2023-08-18T23:15:36.803748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_matrix[226218]","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:15:36.806281Z","iopub.execute_input":"2023-08-18T23:15:36.806894Z","iopub.status.idle":"2023-08-18T23:15:36.823119Z","shell.execute_reply.started":"2023-08-18T23:15:36.806862Z","shell.execute_reply":"2023-08-18T23:15:36.821371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Apply vectorization to train and val dataset","metadata":{}},{"cell_type":"code","source":"x_train_vec = vectorization(x_train)\nx_val_vec = vectorization(x_val)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:15:36.829332Z","iopub.execute_input":"2023-08-18T23:15:36.829686Z","iopub.status.idle":"2023-08-18T23:15:41.261381Z","shell.execute_reply.started":"2023-08-18T23:15:36.829662Z","shell.execute_reply":"2023-08-18T23:15:41.260371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_vec.shape, x_val_vec.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:15:41.263269Z","iopub.execute_input":"2023-08-18T23:15:41.263685Z","iopub.status.idle":"2023-08-18T23:15:41.270304Z","shell.execute_reply.started":"2023-08-18T23:15:41.263651Z","shell.execute_reply":"2023-08-18T23:15:41.269402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_vec[10]","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:15:41.271709Z","iopub.execute_input":"2023-08-18T23:15:41.272644Z","iopub.status.idle":"2023-08-18T23:15:41.628418Z","shell.execute_reply.started":"2023-08-18T23:15:41.272609Z","shell.execute_reply":"2023-08-18T23:15:41.627292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create tensorflow dataset to train model\ntrain_ds = tf.data.Dataset.from_tensor_slices((x_train_vec, y_train))\ntrain_ds = train_ds.batch(32).prefetch(tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:15:41.630005Z","iopub.execute_input":"2023-08-18T23:15:41.630740Z","iopub.status.idle":"2023-08-18T23:15:41.656997Z","shell.execute_reply.started":"2023-08-18T23:15:41.630700Z","shell.execute_reply":"2023-08-18T23:15:41.656034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create validation tensorflow dataset\nval_ds = tf.data.Dataset.from_tensor_slices((x_val_vec, y_val))\nval_ds = val_ds.batch(32).prefetch(tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:15:41.658277Z","iopub.execute_input":"2023-08-18T23:15:41.658672Z","iopub.status.idle":"2023-08-18T23:15:41.669938Z","shell.execute_reply.started":"2023-08-18T23:15:41.658634Z","shell.execute_reply":"2023-08-18T23:15:41.668960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Embedding,LSTM, Dense, Dropout, Input\nfrom tensorflow.keras.models import Sequential\n\ndef glove_lstm(embedding_matrix_vocab, vocab_size, embedding_dim,max_words,hidden_units = 64):\n    model = Sequential()\n    \n    model.add(Embedding(vocab_size+1, embedding_dim,\n                        embeddings_initializer=tf.keras.initializers.Constant(embedding_matrix_vocab), \n                        trainable=False, input_length=max_words))\n    '''model.add(Input(shape = (max_words,)))\n    model.add(Embedding(vocab_size+1, embedding_dim,input_length=max_words))'''\n    \n    model.add(LSTM(hidden_units, input_shape=(max_words,embedding_dim)))\n    model.add(Dense(32, activation = \"relu\"))\n    model.add(Dropout(0.5))\n    model.add(Dense(1, activation = 'sigmoid'))\n    model.compile(optimizer='adam', loss=tf.keras.losses.BinaryFocalCrossentropy(), metrics=['accuracy'])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:15:41.674239Z","iopub.execute_input":"2023-08-18T23:15:41.674656Z","iopub.status.idle":"2023-08-18T23:15:41.688602Z","shell.execute_reply.started":"2023-08-18T23:15:41.674617Z","shell.execute_reply":"2023-08-18T23:15:41.687679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_dim = 300\nmodel1 = glove_lstm(embedding_matrix, vocab_size, embedding_dim,SEQUENCE_LEN)\nmodel1.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:25:54.813873Z","iopub.execute_input":"2023-08-18T23:25:54.814261Z","iopub.status.idle":"2023-08-18T23:25:55.420463Z","shell.execute_reply.started":"2023-08-18T23:25:54.814232Z","shell.execute_reply":"2023-08-18T23:25:55.419681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model1.layers:\n    print(layer.input_shape, layer.output_shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:26:05.774859Z","iopub.execute_input":"2023-08-18T23:26:05.775238Z","iopub.status.idle":"2023-08-18T23:26:05.780884Z","shell.execute_reply.started":"2023-08-18T23:26:05.775208Z","shell.execute_reply":"2023-08-18T23:26:05.779579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train model\ncallback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)\nhistory1=model1.fit(\n    train_ds, \n    batch_size=32, \n    epochs=10, \n    validation_data=val_ds,\n    callbacks=[callback]\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:26:09.611979Z","iopub.execute_input":"2023-08-18T23:26:09.612401Z","iopub.status.idle":"2023-08-18T23:46:09.279937Z","shell.execute_reply.started":"2023-08-18T23:26:09.612367Z","shell.execute_reply":"2023-08-18T23:46:09.278935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.plot(history1.history['loss'])\nplt.plot(history1.history['val_loss'])\nplt.title('Loss')\nplt.show()\nplt.plot(history1.history['accuracy'])\nplt.plot(history1.history['val_accuracy'])\nplt.title('Accuracy')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:47:10.535638Z","iopub.execute_input":"2023-08-18T23:47:10.536089Z","iopub.status.idle":"2023-08-18T23:47:11.298812Z","shell.execute_reply.started":"2023-08-18T23:47:10.536054Z","shell.execute_reply":"2023-08-18T23:47:11.297586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.evaluate(x_val_vec, y_val)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:47:15.581229Z","iopub.execute_input":"2023-08-18T23:47:15.581723Z","iopub.status.idle":"2023-08-18T23:47:56.627620Z","shell.execute_reply.started":"2023-08-18T23:47:15.581685Z","shell.execute_reply":"2023-08-18T23:47:56.626582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix\nprediction = model1.predict(x_val_vec)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:48:56.469709Z","iopub.execute_input":"2023-08-18T23:48:56.470619Z","iopub.status.idle":"2023-08-18T23:49:21.216851Z","shell.execute_reply.started":"2023-08-18T23:48:56.470576Z","shell.execute_reply":"2023-08-18T23:49:21.215776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:49:21.219162Z","iopub.execute_input":"2023-08-18T23:49:21.219499Z","iopub.status.idle":"2023-08-18T23:49:21.226310Z","shell.execute_reply.started":"2023-08-18T23:49:21.219474Z","shell.execute_reply":"2023-08-18T23:49:21.225401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict = ((prediction > 0.5)+0).ravel()\ny_predict","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:53:23.373736Z","iopub.execute_input":"2023-08-18T23:53:23.374120Z","iopub.status.idle":"2023-08-18T23:53:23.382291Z","shell.execute_reply.started":"2023-08-18T23:53:23.374091Z","shell.execute_reply":"2023-08-18T23:53:23.381357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnm = confusion_matrix(y_val, y_predict)\nConfusionMatrixDisplay(cnm, display_labels= ['sincere','insincere']).plot()","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:54:13.124965Z","iopub.execute_input":"2023-08-18T23:54:13.125343Z","iopub.status.idle":"2023-08-18T23:54:13.440344Z","shell.execute_reply.started":"2023-08-18T23:54:13.125294Z","shell.execute_reply":"2023-08-18T23:54:13.439273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y_val, y_predict))","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:56:51.408025Z","iopub.execute_input":"2023-08-18T23:56:51.408412Z","iopub.status.idle":"2023-08-18T23:56:51.768267Z","shell.execute_reply.started":"2023-08-18T23:56:51.408380Z","shell.execute_reply":"2023-08-18T23:56:51.767126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')\ntest_ds.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:57:16.063837Z","iopub.execute_input":"2023-08-18T23:57:16.064196Z","iopub.status.idle":"2023-08-18T23:57:17.478276Z","shell.execute_reply.started":"2023-08-18T23:57:16.064169Z","shell.execute_reply":"2023-08-18T23:57:17.477248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=vectorization(test_ds.question_text.str.lower())\npredict_test=model1.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:57:40.385511Z","iopub.execute_input":"2023-08-18T23:57:40.385879Z","iopub.status.idle":"2023-08-18T23:58:22.869997Z","shell.execute_reply.started":"2023-08-18T23:57:40.385848Z","shell.execute_reply":"2023-08-18T23:58:22.868849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds['target']= ((predict_test > 0.5)+0).ravel()","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:58:42.521788Z","iopub.execute_input":"2023-08-18T23:58:42.522148Z","iopub.status.idle":"2023-08-18T23:58:42.528892Z","shell.execute_reply.started":"2023-08-18T23:58:42.522119Z","shell.execute_reply":"2023-08-18T23:58:42.527908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:58:52.181680Z","iopub.execute_input":"2023-08-18T23:58:52.182054Z","iopub.status.idle":"2023-08-18T23:58:52.192857Z","shell.execute_reply.started":"2023-08-18T23:58:52.182025Z","shell.execute_reply":"2023-08-18T23:58:52.191850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_ds = test_ds[['qid','target']]","metadata":{"execution":{"iopub.status.busy":"2023-08-18T23:59:54.642992Z","iopub.execute_input":"2023-08-18T23:59:54.643382Z","iopub.status.idle":"2023-08-18T23:59:54.659794Z","shell.execute_reply.started":"2023-08-18T23:59:54.643343Z","shell.execute_reply":"2023-08-18T23:59:54.658695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_ds.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-19T00:00:02.899497Z","iopub.execute_input":"2023-08-19T00:00:02.899928Z","iopub.status.idle":"2023-08-19T00:00:02.918849Z","shell.execute_reply.started":"2023-08-19T00:00:02.899892Z","shell.execute_reply":"2023-08-19T00:00:02.917789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_ds.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-19T00:44:47.651241Z","iopub.execute_input":"2023-08-19T00:44:47.652278Z","iopub.status.idle":"2023-08-19T00:44:48.653472Z","shell.execute_reply.started":"2023-08-19T00:44:47.652241Z","shell.execute_reply":"2023-08-19T00:44:48.652526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds.question_text[0]","metadata":{"execution":{"iopub.status.busy":"2023-08-19T00:07:13.491919Z","iopub.execute_input":"2023-08-19T00:07:13.492290Z","iopub.status.idle":"2023-08-19T00:07:13.499300Z","shell.execute_reply.started":"2023-08-19T00:07:13.492254Z","shell.execute_reply":"2023-08-19T00:07:13.498253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}