105 lines
2.5 KiB
Plaintext
105 lines
2.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6cde7c61",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from pathlib import Path\n",
|
|
"\n",
|
|
"import pandas as pd\n",
|
|
"from sklearn.model_selection import train_test_split"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "96172d31",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"INPUT_FILE = Path(\"raw_data.tsv\")\n",
|
|
"OUTPUT_FILE = Path(\"clean_data.csv\")\n",
|
|
"\n",
|
|
"assert INPUT_FILE.exists()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "9260f649",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"df_input = pd.read_csv(INPUT_FILE, sep=\"\\t\", header=None)\n",
|
|
"df_input.columns = [\"class\", \"text\"]\n",
|
|
"df_input[\"class\"] = df_input[\"class\"].map({\"ham\": 0, \"spam\": 1})"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3758aff2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Remove duplicates\n",
|
|
"\n",
|
|
"print(\"Rows before dropping duplicates:\", len(df_input))\n",
|
|
"df_input = df_input.drop_duplicates(subset=[\"text\"])\n",
|
|
"print(\"Rows after dropping duplicates:\", len(df_input))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "8f25f3e9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Create an 80/20 train/test split\n",
|
|
"\n",
|
|
"train_idx, test_idx = train_test_split(df_input.index, test_size=0.2, random_state=42, stratify=df_input[\"class\"])\n",
|
|
"df_input[\"is_train\"] = 0\n",
|
|
"df_input.loc[train_idx, \"is_train\"] = 1\n",
|
|
"df_input = df_input[[\"is_train\", \"class\", \"text\"]].sort_values(by=\"is_train\")\n",
|
|
"\n",
|
|
"print(\"Train set size:\", (df_input[\"is_train\"] == 1).sum())\n",
|
|
"print(\"Test set size:\", (df_input[\"is_train\"] == 0).sum())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "2bcf7cf3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"df_input[[\"is_train\", \"class\", \"text\"]].to_csv(OUTPUT_FILE, index=False)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": ".venv",
|
|
"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"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|