{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c6c79f3c-ef79-4d29-bf1a-96c18d9ddf78", "metadata": {}, "outputs": [], "source": [ "#data set form kaggle :link: https://www.kaggle.com/competitions/fake-news/data?select=train.csv\n", "\n", "import pandas as pd\n", "\n", "# Load the dataset with the correct delimiter\n", "df = pd.read_csv(\"train.csv\")\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "53e8a8a2-0286-4d01-b0db-cce04e9a8571", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | id | \n", "title | \n", "author | \n", "text | \n", "label | \n", "
|---|---|---|---|---|---|
| 0 | \n", "0 | \n", "House Dem Aide: We Didn’t Even See Comey’s Let... | \n", "Darrell Lucus | \n", "House Dem Aide: We Didn’t Even See Comey’s Let... | \n", "1 | \n", "
| 1 | \n", "1 | \n", "FLYNN: Hillary Clinton, Big Woman on Campus - ... | \n", "Daniel J. Flynn | \n", "Ever get the feeling your life circles the rou... | \n", "0 | \n", "
| 2 | \n", "2 | \n", "Why the Truth Might Get You Fired | \n", "Consortiumnews.com | \n", "Why the Truth Might Get You Fired October 29, ... | \n", "1 | \n", "
| 3 | \n", "3 | \n", "15 Civilians Killed In Single US Airstrike Hav... | \n", "Jessica Purkiss | \n", "Videos 15 Civilians Killed In Single US Airstr... | \n", "1 | \n", "
| 4 | \n", "4 | \n", "Iranian woman jailed for fictional unpublished... | \n", "Howard Portnoy | \n", "Print \\nAn Iranian woman has been sentenced to... | \n", "1 | \n", "
| \n", " | id | \n", "title | \n", "author | \n", "text | \n", "
|---|---|---|---|---|
| 0 | \n", "0 | \n", "House Dem Aide: We Didn’t Even See Comey’s Let... | \n", "Darrell Lucus | \n", "House Dem Aide: We Didn’t Even See Comey’s Let... | \n", "
| 1 | \n", "1 | \n", "FLYNN: Hillary Clinton, Big Woman on Campus - ... | \n", "Daniel J. Flynn | \n", "Ever get the feeling your life circles the rou... | \n", "
| 2 | \n", "2 | \n", "Why the Truth Might Get You Fired | \n", "Consortiumnews.com | \n", "Why the Truth Might Get You Fired October 29, ... | \n", "
| 3 | \n", "3 | \n", "15 Civilians Killed In Single US Airstrike Hav... | \n", "Jessica Purkiss | \n", "Videos 15 Civilians Killed In Single US Airstr... | \n", "
| 4 | \n", "4 | \n", "Iranian woman jailed for fictional unpublished... | \n", "Howard Portnoy | \n", "Print \\nAn Iranian woman has been sentenced to... | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 20795 | \n", "20795 | \n", "Rapper T.I.: Trump a ’Poster Child For White S... | \n", "Jerome Hudson | \n", "Rapper T. I. unloaded on black celebrities who... | \n", "
| 20796 | \n", "20796 | \n", "N.F.L. Playoffs: Schedule, Matchups and Odds -... | \n", "Benjamin Hoffman | \n", "When the Green Bay Packers lost to the Washing... | \n", "
| 20797 | \n", "20797 | \n", "Macy’s Is Said to Receive Takeover Approach by... | \n", "Michael J. de la Merced and Rachel Abrams | \n", "The Macy’s of today grew from the union of sev... | \n", "
| 20798 | \n", "20798 | \n", "NATO, Russia To Hold Parallel Exercises In Bal... | \n", "Alex Ansary | \n", "NATO, Russia To Hold Parallel Exercises In Bal... | \n", "
| 20799 | \n", "20799 | \n", "What Keeps the F-35 Alive | \n", "David Swanson | \n", "David Swanson is an author, activist, journa... | \n", "
20800 rows × 4 columns
\n", "| \n", " | id | \n", "title | \n", "author | \n", "text | \n", "
|---|---|---|---|---|
| 0 | \n", "0 | \n", "House Dem Aide: We Didn’t Even See Comey’s Let... | \n", "Darrell Lucus | \n", "House Dem Aide: We Didn’t Even See Comey’s Let... | \n", "
| 1 | \n", "1 | \n", "FLYNN: Hillary Clinton, Big Woman on Campus - ... | \n", "Daniel J. Flynn | \n", "Ever get the feeling your life circles the rou... | \n", "
| 2 | \n", "2 | \n", "Why the Truth Might Get You Fired | \n", "Consortiumnews.com | \n", "Why the Truth Might Get You Fired October 29, ... | \n", "
| 3 | \n", "3 | \n", "15 Civilians Killed In Single US Airstrike Hav... | \n", "Jessica Purkiss | \n", "Videos 15 Civilians Killed In Single US Airstr... | \n", "
| 4 | \n", "4 | \n", "Iranian woman jailed for fictional unpublished... | \n", "Howard Portnoy | \n", "Print \\nAn Iranian woman has been sentenced to... | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 20795 | \n", "20795 | \n", "Rapper T.I.: Trump a ’Poster Child For White S... | \n", "Jerome Hudson | \n", "Rapper T. I. unloaded on black celebrities who... | \n", "
| 20796 | \n", "20796 | \n", "N.F.L. Playoffs: Schedule, Matchups and Odds -... | \n", "Benjamin Hoffman | \n", "When the Green Bay Packers lost to the Washing... | \n", "
| 20797 | \n", "20797 | \n", "Macy’s Is Said to Receive Takeover Approach by... | \n", "Michael J. de la Merced and Rachel Abrams | \n", "The Macy’s of today grew from the union of sev... | \n", "
| 20798 | \n", "20798 | \n", "NATO, Russia To Hold Parallel Exercises In Bal... | \n", "Alex Ansary | \n", "NATO, Russia To Hold Parallel Exercises In Bal... | \n", "
| 20799 | \n", "20799 | \n", "What Keeps the F-35 Alive | \n", "David Swanson | \n", "David Swanson is an author, activist, journa... | \n", "
20800 rows × 4 columns
\n", "Model: \"sequential\"\n",
"\n"
],
"text/plain": [
"\u001b[1mModel: \"sequential\"\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
"┃ Layer (type) ┃ Output Shape ┃ Param # ┃\n",
"┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
"│ embedding (Embedding) │ ? │ 0 (unbuilt) │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ bidirectional (Bidirectional) │ ? │ 0 (unbuilt) │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ dense (Dense) │ ? │ 0 (unbuilt) │\n",
"└─────────────────────────────────┴────────────────────────┴───────────────┘\n",
"\n"
],
"text/plain": [
"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
"┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n",
"┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
"│ embedding (\u001b[38;5;33mEmbedding\u001b[0m) │ ? │ \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ bidirectional (\u001b[38;5;33mBidirectional\u001b[0m) │ ? │ \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
"├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
"│ dense (\u001b[38;5;33mDense\u001b[0m) │ ? │ \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
"└─────────────────────────────────┴────────────────────────┴───────────────┘\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Total params: 0 (0.00 B)\n", "\n" ], "text/plain": [ "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Trainable params: 0 (0.00 B)\n", "\n" ], "text/plain": [ "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Non-trainable params: 0 (0.00 B)\n", "\n" ], "text/plain": [ "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "## Creating model\n", "embedding_vector_features=40\n", "model=Sequential()\n", "model.add(Embedding(voc_size,embedding_vector_features,input_length=sent_length))\n", "model.add(Bidirectional(LSTM(100)))\n", "model.add(Dense(1,activation='sigmoid'))\n", "model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])\n", "print(model.summary())" ] }, { "cell_type": "code", "execution_count": 51, "id": "72c2040b-10a7-4b8d-ab40-e383a294a477", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "x_final = np.array(embedded_docs)\n", "y_final = np.array(y)" ] }, { "cell_type": "code", "execution_count": 53, "id": "cd01098e-825c-4849-9a39-5e90dad9413b", "metadata": {}, "outputs": [], "source": [ "##train test split \n", "from sklearn.model_selection import train_test_split\n", "x_train, x_test , y_train, y_test = train_test_split(x_final, y_final, test_size = 0.2, random_state = 23)" ] }, { "cell_type": "code", "execution_count": 55, "id": "782f3429-8930-48e0-ae94-f00b01aaf916", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m9s\u001b[0m 9ms/step - accuracy: 0.8589 - loss: 0.3046 - val_accuracy: 0.9514 - val_loss: 0.1205\n", "Epoch 2/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 8ms/step - accuracy: 0.9536 - loss: 0.1236 - val_accuracy: 0.9808 - val_loss: 0.0749\n", "Epoch 3/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 9ms/step - accuracy: 0.9731 - loss: 0.0763 - val_accuracy: 0.9875 - val_loss: 0.0500\n", "Epoch 4/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 9ms/step - accuracy: 0.9810 - loss: 0.0549 - val_accuracy: 0.9913 - val_loss: 0.0305\n", "Epoch 5/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 9ms/step - accuracy: 0.9903 - loss: 0.0327 - val_accuracy: 0.9966 - val_loss: 0.0169\n", "Epoch 6/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 10ms/step - accuracy: 0.9935 - loss: 0.0193 - val_accuracy: 0.9962 - val_loss: 0.0154\n", "Epoch 7/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 9ms/step - accuracy: 0.9937 - loss: 0.0194 - val_accuracy: 0.9976 - val_loss: 0.0094\n", "Epoch 8/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 9ms/step - accuracy: 0.9965 - loss: 0.0105 - val_accuracy: 0.9976 - val_loss: 0.0080\n", "Epoch 9/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 10ms/step - accuracy: 0.9984 - loss: 0.0062 - val_accuracy: 0.9993 - val_loss: 0.0029\n", "Epoch 10/10\n", "\u001b[1m650/650\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 9ms/step - accuracy: 0.9980 - loss: 0.0061 - val_accuracy: 0.9986 - val_loss: 0.0038\n" ] }, { "data": { "text/plain": [ "