-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tutorial Notebooks #95
Open
jennjwang
wants to merge
3
commits into
main
Choose a base branch
from
notebooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
135 changes: 135 additions & 0 deletions
135
notebooks/.ipynb_checkpoints/load_clean-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This is a tutorial on loading in an election dataset and cleaning ballots.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from votekit.cvr_loaders import load_blt\n", | ||
"import votekit.cleaning as clean" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's first load in our cvr into a preference profile\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# need to make the scottish election data importable\n", | ||
"\n", | ||
"pp, seats = load_blt(\"...\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we can clean the ballots from this election.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"If we want to remove a candidate, we can call remove_noncands() from the package as shown below.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\n", | ||
"cleaned_pp = clean.remove_noncands(pp, [\"Graham HUTCHISON (C)\"])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can also write our own cleaning rule with the helper function clean_profile(). The following example is a cleaning rule truncates the ballot to n-ranks.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from votekit.cleaning import clean_profile\n", | ||
"from votekit.pref_profile import PreferenceProfile\n", | ||
"from votekit.ballot import Ballot\n", | ||
"\n", | ||
"\n", | ||
"def truncate(n: int, pp: PreferenceProfile):\n", | ||
" def truncate_ballot(ballot: Ballot):\n", | ||
" return Ballot(ranking=ballot.ranking[:n], weight=ballot.weight)\n", | ||
"\n", | ||
" pp_clean = clean_profile(pp=pp, clean_ballot_func=truncate_ballot)\n", | ||
" return pp_clean" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cleaned_pp = truncate(n=3, pp=pp)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Our preference profile is now cleaned, and we can save it as an csv for future use.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cleaned_pp.to_csv('path/to/save')" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.9.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This tutorial shows you how to simulate elections using VoteKit.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "ModuleNotFoundError", | ||
"evalue": "No module named 'votekit.elections'", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | ||
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", | ||
"Cell \u001b[0;32mIn[12], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39m# from votekit.utils import make_ballot\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mvotekit\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39melections\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39melection_types\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39melections\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mvotekit\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mpref_profile\u001b[39;00m \u001b[39mimport\u001b[39;00m PreferenceProfile\n\u001b[1;32m 4\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mvotekit\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39melections\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mtransfers\u001b[39;00m \u001b[39mimport\u001b[39;00m fractional_transfer\n", | ||
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'votekit.elections'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# from votekit.utils import make_ballot\n", | ||
"import votekit.elections.election_types as elections\n", | ||
"from votekit.pref_profile import PreferenceProfile\n", | ||
"from votekit.elections.transfers import fractional_transfer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's first build our preference profile with synthetic ballots\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"b1 = make_ballot(ranking=[\"A\", \"D\", \"E\", \"C\", \"B\"], weight=18)\n", | ||
"b2 = make_ballot(ranking=[\"B\", \"E\", \"D\", \"C\", \"A\"], weight=12)\n", | ||
"b3 = make_ballot(ranking=[\"C\", \"B\", \"E\", \"D\", \"A\"], weight=10)\n", | ||
"b4 = make_ballot(ranking=[\"D\", \"C\", \"E\", \"B\", \"A\"], weight=4)\n", | ||
"b5 = make_ballot(ranking=[\"E\", \"B\", \"D\", \"C\", \"A\"], weight=4)\n", | ||
"b6 = make_ballot(ranking=[\"E\", \"C\", \"D\", \"B\", \"A\"], weight=2)\n", | ||
"pp = PreferenceProfile(ballots=[b1, b2, b3, b4, b5, b6])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we can define our set of elections to simulate.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"num_seats = 1\n", | ||
"election_borda = elections.Borda(pp, seats=num_seats, score_vector=None)\n", | ||
"election_irv = elections.STV(pp, fractional_transfer, seats=num_seats)\n", | ||
"election_plurality = elections.Plurality(\n", | ||
" pp, seats=num_seats, ballot_ties=False)\n", | ||
"election_seq = elections.SequentialRCV(pp, seats=num_seats)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's run the elections. Running the elections will generate an election state, from which we can get the winners.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"election_state_borda = election_borda.run_election()\n", | ||
"election_state_irv = election_irv.run_election()\n", | ||
"election_state_plurality = election_plurality.run_election()\n", | ||
"election_state_seq = election_seq.run_election()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"outcome_borda = election_state_borda.get_all_winners()\n", | ||
"outcome_irv = election_state_irv.get_all_winners()\n", | ||
"outcome_plurality = election_state_plurality.get_all_winners()\n", | ||
"outcome_seq = election_state_seq.get_all_winners()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We should expect different results for different elections.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print(outcome_borda)\n", | ||
"print(outcome_irv)\n", | ||
"print(outcome_plurality)\n", | ||
"print(outcome_seq)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "csci1470 Python 3.9.12", | ||
"language": "python", | ||
"name": "csci1470" | ||
}, | ||
"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.9.12" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets keep the .ipynb_checkpoints and dist/ folder out of the main branch so the repo doesn't get cluttered !