Last active
December 5, 2016 02:11
-
-
Save tvorogme/6dbfb8046b43612959dd2a45b682e15a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"from collections import defaultdict, Counter\n", | |
"from tqdm import tqdm_notebook" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"events = pd.read_csv('course-217-events.csv')\n", | |
"events.sort_values(\"time\", inplace=True)\n", | |
"structure = pd.read_csv('course-217-structure.csv')\n", | |
"structure.sort_values([\"module_position\", \"lesson_position\", \"step_position\"], inplace=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"step_line = list(structure.step_id.values)\n", | |
"\n", | |
"def next_step(step):\n", | |
" if step != step_line[-1]:\n", | |
" return step_line[step_line.index(step)+1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# сет возращений\n", | |
"return_count = defaultdict(set)\n", | |
"\n", | |
"# вспомогательный список нужен для нормализации результата\n", | |
"steps_by_user = defaultdict(set)\n", | |
"\n", | |
"for line in tqdm_notebook(events.groupby('user_id').step_id):\n", | |
" for i, step in enumerate(list(line[1])):\n", | |
" # Запомним то, сколько юзеров входило в сет\n", | |
" steps_by_user[step].add(line[0])\n", | |
" try:\n", | |
" # индекс следующего степа меньше текущего в списке до текущего индекса\n", | |
" if list(line[1])[:i].index(step) < i - list(line[1])[:i][::-1].index(next_step(step)) - 1:\n", | |
" return_count[step].add(line[0]) \n", | |
" except:\n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(41684, 0.7460106382978723),\n", | |
" (41604, 0.7297605473204105),\n", | |
" (41097, 0.6809314033983638),\n", | |
" (41481, 0.6759465478841871),\n", | |
" (42593, 0.6662468513853904),\n", | |
" (38872, 0.6616337591947348),\n", | |
" (41686, 0.6571815718157181),\n", | |
" (39735, 0.6548804780876494),\n", | |
" (39740, 0.6538882803943045),\n", | |
" (39717, 0.6459863945578231)]" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Counter({i: len(return_count[i]) / len(steps_by_user[i]) for i in steps_by_user.keys()}).most_common(10)" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [default]", | |
"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.2" | |
}, | |
"widgets": { | |
"state": { | |
"484891e1dd0547049fb113218fda4059": { | |
"views": [ | |
{ | |
"cell_index": 5 | |
} | |
] | |
} | |
}, | |
"version": "1.2.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment