Skip to content

Instantly share code, notes, and snippets.

@tvorogme
Last active December 5, 2016 02:11
Show Gist options
  • Save tvorogme/6dbfb8046b43612959dd2a45b682e15a to your computer and use it in GitHub Desktop.
Save tvorogme/6dbfb8046b43612959dd2a45b682e15a 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": true
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"events = pd.read_csv('course-217-events.csv')\n",
"structure = pd.read_csv('course-217-structure.csv')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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>user_id</th>\n",
" <th>action</th>\n",
" <th>step_id</th>\n",
" <th>time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>viewed</td>\n",
" <td>47435</td>\n",
" <td>1465181036</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>viewed</td>\n",
" <td>47438</td>\n",
" <td>1465181034</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>viewed</td>\n",
" <td>47435</td>\n",
" <td>1465181021</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>viewed</td>\n",
" <td>47436</td>\n",
" <td>1465181015</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>viewed</td>\n",
" <td>41605</td>\n",
" <td>1465180037</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" user_id action step_id time\n",
"0 1 viewed 47435 1465181036\n",
"1 1 viewed 47438 1465181034\n",
"2 1 viewed 47435 1465181021\n",
"3 1 viewed 47436 1465181015\n",
"4 1 viewed 41605 1465180037"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"events.head()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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>course_id</th>\n",
" <th>module_id</th>\n",
" <th>module_position</th>\n",
" <th>lesson_id</th>\n",
" <th>lesson_position</th>\n",
" <th>step_id</th>\n",
" <th>step_position</th>\n",
" <th>step_type</th>\n",
" <th>step_cost</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>217</td>\n",
" <td>614</td>\n",
" <td>2</td>\n",
" <td>13228</td>\n",
" <td>2</td>\n",
" <td>38842</td>\n",
" <td>1</td>\n",
" <td>text</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>217</td>\n",
" <td>614</td>\n",
" <td>2</td>\n",
" <td>13228</td>\n",
" <td>2</td>\n",
" <td>39715</td>\n",
" <td>6</td>\n",
" <td>code</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>217</td>\n",
" <td>614</td>\n",
" <td>2</td>\n",
" <td>13228</td>\n",
" <td>2</td>\n",
" <td>39716</td>\n",
" <td>7</td>\n",
" <td>code</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>217</td>\n",
" <td>614</td>\n",
" <td>2</td>\n",
" <td>13228</td>\n",
" <td>2</td>\n",
" <td>39717</td>\n",
" <td>8</td>\n",
" <td>code</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>217</td>\n",
" <td>614</td>\n",
" <td>2</td>\n",
" <td>13228</td>\n",
" <td>2</td>\n",
" <td>39721</td>\n",
" <td>9</td>\n",
" <td>free-answer</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" course_id module_id module_position lesson_id lesson_position step_id \\\n",
"0 217 614 2 13228 2 38842 \n",
"1 217 614 2 13228 2 39715 \n",
"2 217 614 2 13228 2 39716 \n",
"3 217 614 2 13228 2 39717 \n",
"4 217 614 2 13228 2 39721 \n",
"\n",
" step_position step_type step_cost \n",
"0 1 text 0 \n",
"1 6 code 1 \n",
"2 7 code 1 \n",
"3 8 code 1 \n",
"4 9 free-answer 0 "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"structure.head()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"events.sort_values(by='time', inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"costed = {x:y for x,y in zip(structure.step_id, structure.step_cost)}"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Тут храним все скоры\n",
"scores= {}\n",
"# Тут храним топовые значения\n",
"the_best = {}\n",
"\n",
"for event in (events.values):\n",
" user = event[0]\n",
" step_id = event[2]\n",
" \n",
" # Заполним значеия, если их ещё нет\n",
" # Нельзя юзать defaultdict, потому что events[3]\n",
" if (user not in scores.keys()):\n",
" scores[user] = [0, event[3]]\n",
" # Если чувак прошел степ\n",
" # Добавим ему за это денег\n",
" if (event[1] == 'passed'):\n",
" scores[user][0] += step_id_cost[step_id]\n",
" \n",
" # scores.get(user, [0,0])[0] <- замена deffaultdict\n",
" # user not in the_best < он должен там быть\n",
" if (scores.get(user, [0,0])[0] >= 24) and (user not in the_best):\n",
" # Сохранить чувака\n",
" the_best[user] = event[3] - scores[user][1]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Тут мы сортим по скору\n",
"data = pd.DataFrame(list(the_best.items())).sort_values(by=1)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4313,1264,821,1146,1124,2622,713,972,4280,1291,"
]
}
],
"source": [
"# Возьмём топ 10\n",
"for elem in data[:10][0].tolist():\n",
" print(elem, end=',')"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"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"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment