{ "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", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titletextlabel
0Live at Truthdig: Robert Scheer and Thomas Fra...Live at Truthdig: Robert Scheer and Thomas Fra...0
1The Mirage of a Return to Manufacturing Greatn...Half a century ago, harvesting California’s 2....1
2British PM expected to offer to fill post-Brex...(Reuters) - The British government has told Ge...1
3Checkmating ObamaOriginally published by the Jerusalem Post . \\...0
4Thirty-eight injured in police charges in Cata...MADRID (Reuters) - Emergency services have att...1
\n", "
" ], "text/plain": [ " title \\\n", "0 Live at Truthdig: Robert Scheer and Thomas Fra... \n", "1 The Mirage of a Return to Manufacturing Greatn... \n", "2 British PM expected to offer to fill post-Brex... \n", "3 Checkmating Obama \n", "4 Thirty-eight injured in police charges in Cata... \n", "\n", " text label \n", "0 Live at Truthdig: Robert Scheer and Thomas Fra... 0 \n", "1 Half a century ago, harvesting California’s 2.... 1 \n", "2 (Reuters) - The British government has told Ge... 1 \n", "3 Originally published by the Jerusalem Post . \\... 0 \n", "4 MADRID (Reuters) - Emergency services have att... 1 " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] } ], "metadata": { "kernelspec": { "display_name": "torch", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.11" } }, "nbformat": 4, "nbformat_minor": 2 }