{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Generator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Create a list of parties that participate in elections. It is better if the parties' names have a fictional nature, but somehow relates to your country's politics:" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "general_list_of_parties = ['Harmony Party', 'Red Party', 'Justice and Truth', 'Adventure Alliance', 'Animal Friends Party', 'Go Greens!', 'Yo-ho-ho Pirate Party']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Generate ballots of all formats. We used 2 formats: ranked ballots with party names and positional ranking with numbers:" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13000" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.seed(42)\n", "\n", "# correct ballots\n", "import random\n", "import numpy as np\n", "# here you can assign some distribution to party votes, e.g. based on the last elections. \n", "# If you want to underline the ambiguity about the best system for counting votes, it is better to make probabilities almost equal.\n", "prob_weights = np.array([0.20, 0.20, 0.20, 0.19, 0.19, 0.01, 0.01])\n", "\n", "# generating votes_names\n", "votes = []\n", "for i in range(3,8):\n", " for k in range(random.randint(1000,1500)):\n", " votes.append([str(i) for i in list(np.random.choice(general_list_of_parties, size=i, replace=False, p=prob_weights))])\n", "\n", "# generating votes_ranks\n", "votes2 = []\n", "for i in range(2,8):\n", " for k in range(random.randint(1000,1500)):\n", " comb = [str(i) for i in list(np.random.choice(general_list_of_parties, size=i, replace=False, p=prob_weights))]\n", " newlist = []\n", " for party in general_list_of_parties:\n", " if party in comb:\n", " newlist.append(comb.index(party)+1)\n", " else:\n", " newlist.append(0)\n", " votes2.append(newlist)\n", "\n", "votes += votes2\n", "random.shuffle(votes)\n", "\n", "len(votes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Generate spoiled ballots" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "101" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# incorrect ballots\n", "general_list_of_parties_d = ['Go Grass!', 'Advertisement Party', 'Pink Party', 'Justice and Teeth', 'SDP', 'Party like a Racoon', 'Pickle Ricks', 'Princess Party']\n", "\n", "names_d = []\n", "for i in range(3,8):\n", " for k in range(random.randint(5, 25)):\n", " names_d.append([str(i) for i in list(np.random.choice(general_list_of_parties, size=8, replace=True, p=[1/7] * 7))])\n", "\n", "len(names_d)\n", "\n", "ranks_d = []\n", "\n", "for k in range(random.randint(20, 40)):\n", " sample = list(np.random.choice(general_list_of_parties, size=7, replace=False, p=prob_weights))\n", " comb = [str(i) for i in sample]\n", " newlist = []\n", " for party in general_list_of_parties:\n", " newlist.append(comb.index(party)+2)\n", " ranks_d.append(newlist)\n", "\n", "for k in range(random.randint(20, 40)):\n", " sample = list(np.random.choice(general_list_of_parties, size=random.randint(3,7), replace=False, p=prob_weights))\n", " comb = [str(i) for i in sample]\n", " newlist = []\n", " for party in general_list_of_parties:\n", " if party in comb:\n", " newlist.append(0)\n", " else:\n", " newlist.append(0)\n", " ranks_d.append(newlist)\n", "\n", "for k in range(random.randint(20, 40)):\n", " sample = list(np.random.choice(general_list_of_parties, size=random.randint(3,7), replace=False, p=prob_weights))\n", " comb = [str(i) for i in sample]\n", " newlist = []\n", " for party in general_list_of_parties:\n", " if party in comb:\n", " newlist.append(0)\n", " else:\n", " newlist.append(0)\n", " newlist.append(random.randint(0,1))\n", " ranks_d.append(newlist)\n", "\n", "len(ranks_d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Bind and shuffle all the ballots, save as `.csv` file" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13159" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "votes = votes + names_d + ranks_d\n", "random.shuffle(votes)\n", "len(votes)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "# saving data\n", "import csv\n", "with open(\"votes.csv\", \"w\") as f:\n", " wr = csv.writer(f)\n", " wr.writerows(votes)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }