Skip to content

Instantly share code, notes, and snippets.

@cytms
Created April 1, 2014 08:35
Show Gist options
  • Save cytms/9910238 to your computer and use it in GitHub Desktop.
Save cytms/9910238 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"\u4f7f\u7528\u8005\u884c\u70ba\u95dc\u9023\u5206\u6790"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline --no-import-all\n",
"\n",
"from pylab import *\n",
"import prettyplotlib as ppl\n",
"from prettyplotlib import plt\n",
"import numpy as np\n",
"import MySQLdb\n",
"import math\n",
"from datetime import datetime, timedelta\n",
"\n",
"db = MySQLdb.connect(host=\"localhost\", user=\"root\", db=\"cookpad_production\")\n",
"cur = db.cursor()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cur.execute(\"\\\n",
"select b.id, b.ts, b.act from \\\n",
" ((select user_id id, created_at ts, 1 as act from favorites) \\\n",
" union (select follower_id id, created_at ts, 2 as act from follows) \\\n",
" union (select user_id id, created_at ts, 3 as act from recipes)) b order by b.ts \\\n",
" \")\n",
"\n",
"behaviors = cur.fetchall()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# counter = 0\n",
"user_seq_behavior = {}\n",
"for behavior in behaviors:\n",
" #if counter == 10000:\n",
" # break\n",
" #else:\n",
" # counter += 1\n",
" # print str(behavior[0]) + u'\\t' + str(behavior[1] + timedelta(hours=8)) + u'\\t' + str(behavior[2])\n",
" if behavior[0] in user_seq_behavior:\n",
" if user_seq_behavior[behavior[0]][len(user_seq_behavior[behavior[0]])-1] != behavior[2]:\n",
" user_seq_behavior[behavior[0]].append(behavior[2]) \n",
" else:\n",
" user_seq_behavior[behavior[0]] = [behavior[2]]\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"result = numpy.zeros(9).reshape((3, 3))\n",
"total = [0, 0, 0]\n",
"for uid in user_seq_behavior.keys():\n",
" tmp = -1\n",
" for i in range(len(user_seq_behavior[uid])):\n",
" if tmp != -1:\n",
" result[tmp][user_seq_behavior[uid][i]-1] += 1\n",
" tmp = user_seq_behavior[uid][i]-1\n",
" total[user_seq_behavior[uid][i]-1] += 1\n",
"\n",
"print result\n",
"print total"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 0. 38619. 9231.]\n",
" [ 96944. 0. 5436.]\n",
" [ 10944. 1243. 0.]]\n",
"[230535, 168099, 16896]\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print u'\u6536\u85cf\u5f8c\u8ffd\u8e64\u7684\u6a5f\u7387 ' + str(result[0][1]/total[0])\n",
"print u'\u6536\u85cf\u5f8c\u64b0\u5beb\u7684\u6a5f\u7387 ' + str(result[0][2]/total[0])\n",
"print u'\u8ffd\u8e64\u5f8c\u6536\u85cf\u7684\u6a5f\u7387 ' + str(result[1][0]/total[1])\n",
"print u'\u8ffd\u8e64\u5f8c\u64b0\u5beb\u7684\u6a5f\u7387 ' + str(result[1][2]/total[1])\n",
"print u'\u64b0\u5beb\u5f8c\u6536\u85cf\u7684\u6a5f\u7387 ' + str(result[2][0]/total[2])\n",
"print u'\u64b0\u5beb\u5f8c\u8ffd\u8e64\u7684\u6a5f\u7387 ' + str(result[2][1]/total[2])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\u6536\u85cf\u5f8c\u8ffd\u8e64\u7684\u6a5f\u7387 0.167519031817\n",
"\u6536\u85cf\u5f8c\u64b0\u5beb\u7684\u6a5f\u7387 0.0400416422669\n",
"\u8ffd\u8e64\u5f8c\u6536\u85cf\u7684\u6a5f\u7387 0.576707773395\n",
"\u8ffd\u8e64\u5f8c\u64b0\u5beb\u7684\u6a5f\u7387 0.0323380864848\n",
"\u64b0\u5beb\u5f8c\u6536\u85cf\u7684\u6a5f\u7387 0.647727272727\n",
"\u64b0\u5beb\u5f8c\u8ffd\u8e64\u7684\u6a5f\u7387 0.0735677083333\n"
]
}
],
"prompt_number": 17
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment