Skip to content

Commit

Permalink
ajout du reducer à 2 entrée
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Sep 14, 2015
1 parent a7a9c66 commit b1da7a7
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ _doc/notebooks/population/*.db3
_doc/notebooks/population/*.txt
_doc/notebooks/population/*.gz
_doc/notebooks/population/*.nc
_doc/notebooks/population/*.zip
README
build_help.bat
_doc/sphinxdoc/source/blog/*.rst
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 1,
"metadata": {
"collapsed": true
},
Expand All @@ -131,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {
"collapsed": false
},
Expand All @@ -157,7 +157,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 3,
"metadata": {
"collapsed": true
},
Expand All @@ -170,7 +170,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 4,
"metadata": {
"collapsed": false
},
Expand All @@ -183,7 +183,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 5,
"metadata": {
"collapsed": true
},
Expand All @@ -201,7 +201,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 6,
"metadata": {
"collapsed": true
},
Expand All @@ -214,7 +214,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 7,
"metadata": {
"collapsed": true
},
Expand All @@ -232,7 +232,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 8,
"metadata": {
"collapsed": true
},
Expand All @@ -252,7 +252,7 @@
},
{
"cell_type": "code",
"execution_count": 71,
"execution_count": 9,
"metadata": {
"collapsed": true
},
Expand All @@ -273,7 +273,7 @@
},
{
"cell_type": "code",
"execution_count": 72,
"execution_count": 10,
"metadata": {
"collapsed": false
},
Expand All @@ -287,7 +287,7 @@
},
{
"cell_type": "code",
"execution_count": 73,
"execution_count": 11,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -355,12 +355,239 @@
"4 1960 Y04 4 71.0"
]
},
"execution_count": 73,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Un reducer à deux entrées même si cela n'a pas beaucoup de sens ici :"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class ReducerMediane2:\n",
" def __init__(self):\n",
" self.indicateur = []\n",
" def step(self, value, value2):\n",
" if value >= 0:\n",
" self.indicateur.append(value)\n",
" if value2 >= 0:\n",
" self.indicateur.append(value2)\n",
" def finalize(self):\n",
" self.indicateur.sort()\n",
" return self.indicateur[len(self.indicateur)//2]\n",
" \n",
"cnx.create_aggregate(\"ReducerMediane2\", 2, ReducerMediane2)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>annee</th>\n",
" <th>age</th>\n",
" <th>age_num</th>\n",
" <th>mediane2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1960</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>66.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1960</td>\n",
" <td>Y01</td>\n",
" <td>1</td>\n",
" <td>74.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1960</td>\n",
" <td>Y02</td>\n",
" <td>2</td>\n",
" <td>73.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1960</td>\n",
" <td>Y03</td>\n",
" <td>3</td>\n",
" <td>72.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1960</td>\n",
" <td>Y04</td>\n",
" <td>4</td>\n",
" <td>71.3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" annee age age_num mediane2\n",
"0 1960 None NaN 66.7\n",
"1 1960 Y01 1 74.0\n",
"2 1960 Y02 2 73.2\n",
"3 1960 Y03 3 72.3\n",
"4 1960 Y04 4 71.3"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"\"\"SELECT annee,age,age_num, ReducerMediane2(valeur, valeur+1) AS mediane2 FROM mortalite \n",
" WHERE indicateur==\"LIFEXP\" AND genre==\"F\"\n",
" GROUP BY annee,age,age_num\"\"\"\n",
"df = pandas.read_sql(query, cnx)\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Il n'est apparemment pas possible de retourner deux résultats mais on peut utiliser une ruse qui consise à les concaténer dans une chaîne de caracères."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class ReducerQuantile:\n",
" def __init__(self):\n",
" self.indicateur = []\n",
" def step(self, value):\n",
" if value >= 0:\n",
" self.indicateur.append(value)\n",
" def finalize(self):\n",
" self.indicateur.sort()\n",
" q1 = self.indicateur[len(self.indicateur)//4]\n",
" q2 = self.indicateur[3*len(self.indicateur)//4]\n",
" n = len(self.indicateur)\n",
" return \"%f;%f;%s\" % (q1,q2,n)\n",
" \n",
"cnx.create_aggregate(\"ReducerQuantile\", 1, ReducerQuantile)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>annee</th>\n",
" <th>age</th>\n",
" <th>age_num</th>\n",
" <th>quantiles</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1960</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>4.400000;72.800000;20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1960</td>\n",
" <td>Y01</td>\n",
" <td>1</td>\n",
" <td>73.000000;74.000000;10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1960</td>\n",
" <td>Y02</td>\n",
" <td>2</td>\n",
" <td>72.100000;73.200000;10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1960</td>\n",
" <td>Y03</td>\n",
" <td>3</td>\n",
" <td>71.200000;72.300000;10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1960</td>\n",
" <td>Y04</td>\n",
" <td>4</td>\n",
" <td>70.300000;71.300000;10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" annee age age_num quantiles\n",
"0 1960 None NaN 4.400000;72.800000;20\n",
"1 1960 Y01 1 73.000000;74.000000;10\n",
"2 1960 Y02 2 72.100000;73.200000;10\n",
"3 1960 Y03 3 71.200000;72.300000;10\n",
"4 1960 Y04 4 70.300000;71.300000;10"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"\"\"SELECT annee,age,age_num, ReducerQuantile(valeur) AS quantiles FROM mortalite \n",
" WHERE indicateur==\"LIFEXP\" AND genre==\"F\"\n",
" GROUP BY annee,age,age_num\"\"\"\n",
"df = pandas.read_sql(query, cnx)\n",
"df.head()"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -91,7 +91,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
Expand Down

0 comments on commit b1da7a7

Please sign in to comment.