File size: 5,642 Bytes
bfe6b6c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
{
"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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" <th>text</th>\n",
" <th>label</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Live at Truthdig: Robert Scheer and Thomas Fra...</td>\n",
" <td>Live at Truthdig: Robert Scheer and Thomas Fra...</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>The Mirage of a Return to Manufacturing Greatn...</td>\n",
" <td>Half a century ago, harvesting California’s 2....</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>British PM expected to offer to fill post-Brex...</td>\n",
" <td>(Reuters) - The British government has told Ge...</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Checkmating Obama</td>\n",
" <td>Originally published by the Jerusalem Post . \\...</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Thirty-eight injured in police charges in Cata...</td>\n",
" <td>MADRID (Reuters) - Emergency services have att...</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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
}
|