{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Load the datasets\n", "df_1 = pd.read_csv(\"data_2/WELFake_Dataset.csv\")\n", "df_2 = pd.read_csv(\"data_3/news_articles.csv\")\n", "\n", "# Drop index\n", "df_1.drop(df_1.columns[0], axis=1, inplace=True)\n", "df_1.dropna(inplace=True)\n", "\n", "# Swapping labels around since it originally is the opposite\n", "df_1[\"label\"] = df_1[\"label\"].map({0: 1, 1: 0})\n", "\n", "# Add labels\n", "df_2.drop(\n", " columns=[\n", " \"author\",\n", " \"published\",\n", " \"site_url\",\n", " \"main_img_url\",\n", " \"type\",\n", " \"text_without_stopwords\",\n", " \"title_without_stopwords\",\n", " \"hasImage\",\n", " ],\n", " inplace=True,\n", ")\n", "# Map Real to 1 and Fake to 0\n", "df_2[\"label\"] = df_2[\"label\"].map({\"Real\": 1, \"Fake\": 0})\n", "df_2 = df_2[df_2[\"label\"].isin([1, 0])]\n", "\n", "# Drop rows where the language is not 'english'\n", "df_2 = df_2[df_2[\"language\"] == \"english\"]\n", "df_2.drop(columns=[\"language\"], inplace=True)\n", "\n", "# Convert \"no title\" to empty string\n", "df_2[\"title\"] = df_2[\"title\"].apply(lambda x: \"\" if x == \"no title\" else x)\n", "\n", "df_2.dropna(inplace=True)\n", "\n", "random_1 = df_1.sample(n=500, random_state=42)\n", "random_2 = df_2.sample(n=500, random_state=42)\n", "\n", "# Combine the datasets\n", "df = pd.concat([random_1, random_2], ignore_index=True)\n", "\n", "df[\"label\"] = df[\"label\"].astype(int)\n", "\n", "df.to_csv(\"sampled_data.csv\", index=False)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | title | \n", "text | \n", "label | \n", "
---|---|---|---|
0 | \n", "Live at Truthdig: Robert Scheer and Thomas Fra... | \n", "Live at Truthdig: Robert Scheer and Thomas Fra... | \n", "0 | \n", "
1 | \n", "The Mirage of a Return to Manufacturing Greatn... | \n", "Half a century ago, harvesting California’s 2.... | \n", "1 | \n", "
2 | \n", "British PM expected to offer to fill post-Brex... | \n", "(Reuters) - The British government has told Ge... | \n", "1 | \n", "
3 | \n", "Checkmating Obama | \n", "Originally published by the Jerusalem Post . \\... | \n", "0 | \n", "
4 | \n", "Thirty-eight injured in police charges in Cata... | \n", "MADRID (Reuters) - Emergency services have att... | \n", "1 | \n", "