Skip to content

Instantly share code, notes, and snippets.

@NickRSearcy
Last active March 23, 2016 15:10
Show Gist options
  • Save NickRSearcy/1c9cb79fb6878a65493c to your computer and use it in GitHub Desktop.
Save NickRSearcy/1c9cb79fb6878a65493c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"%pylab inline\n",
"from itertools import combinations"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def calculate_uncertainty(h_list):\n",
" h_set = set(h_list) # make a set that includes only the unique h's\n",
" h_dict = {h: h_list.count(h)/len(h_list) for h in h_set} # make a dict that pairs each unique h with a p(h)\n",
" uncertainties = [-h_dict[h]*log2(h_dict[h]) for h in h_set] #calculate uncertainty for each h\n",
" return sum(uncertainties)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I([2, 4, 6, 8, 10, 12]) = 2.584962500721156\n",
"I([2, 4, 7, 7, 10, 12]) = 2.2516291673878226\n",
"I([2, 2, 2, 8, 8, 8]) = 1.0\n"
]
}
],
"source": [
"H1 = [2, 4, 6, 8, 10, 12]\n",
"H2 = [2, 4, 7, 7, 10, 12]\n",
"H3 = [2, 2, 2, 8, 8, 8]\n",
"for H in [H1, H2, H3]:\n",
" print(\"I({}) = {}\".format(H, calculate_uncertainty(H)))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def calculate_i_g(h_list):\n",
" base_u = calculate_uncertainty(h_list)\n",
" e_u = 0\n",
" for h_prime in combinations(h_list,len(h_list)-1):\n",
" e_u += calculate_uncertainty(h_prime)\n",
" e_u /= len(h_list)\n",
" \n",
" return e_u - base_u"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I_g([2, 4, 6, 8, 10, 12]) = -0.2630344058337939\n",
"I_g([2, 4, 7, 7, 10, 12]) = -0.19636773916712702\n",
"I_g([2, 2, 2, 8, 8, 8]) = -0.029049405545331308\n"
]
}
],
"source": [
"for H in [H1, H2, H3]:\n",
" print(\"I_g({}) = {}\".format(H, calculate_i_g(H)))"
]
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment