码迷,mamicode.com
首页 > 其他好文 > 详细

购物数据意淫分析(2)

时间:2014-04-27 21:38:05      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:python

在淘宝的大数据比赛中,我又一次提高了自己的思维方式,

前前后后代码写了六个版本,但是最好的结果竟然是我第2次的代码,这个让我很是诧异,

但是也说明了一个问题,当你的对于一面语言比较熟悉时候,你所缺少的就是其他方面的知识了,

  1. 首先是我的数学知识,在分析用户行为时,我们知道浏览次数和购买次数是由一定规律的,这个方面找了数学系的同学问了一些,得到的结论是:你可以进行线性拟合,这是最简单的,但是得到的结果不一定真实,于是推荐我使用高斯分布来做,但是由于自己单枪匹马,所以选了比较简单的线性拟合
  2. 心理学,我们可以从数据中发现,那些经常在淘宝买东西的如果是时间间隔一段就买了同一种商品的,那说明这个人的属于死宅之类的,因为这些东西一般我们旁边就有,还有,就是浏览次数和购买之间的关系,用数学来解答,心理学来分析;多天浏览和购买的关系,
先意淫这些吧,下来上三个版本的代码:
第一版本,简单猜测浏览十五次购买一次:
import time

u_id=[]
b_id=[]
t_id=[]
b_time=[]
t_num0=0
t_num1=0
t_num2=0
t_num3=0
a=True
i=0
j=0
fileread=open(‘t_alibaba_data.csv‘,‘r‘)
while True:
    fileline=fileread.readline()
  #  print  type(fileline)
  #  print fileline,
  #  print  i
    filedian =fileline.find(r‘,‘)
    filedian1=fileline.rfind(r‘,‘)

    b_id1=fileline[filedian+1:filedian1-2]
    b_id.append(b_id1)
    
    u_id1=fileline[:filedian]
    u_id.append(u_id1)
    
    t_id1=fileline[filedian1-1:filedian1]
    t_id.append(t_id1)
    
    b_time1=fileline[filedian1:-2]
    b_time.append(b_time1)


    if not fileline:
        break
output=open(‘taobao.txt‘,‘a‘)
#print u_id
#print b_id
#print t_id
print b_time

ff=0
while True:
    if u_id[i]==u_id[i+1]:
        
        if ff==0:

            output.write(u_id[i])
            output.write(‘   ‘)
        ff=ff+1
        if b_id[i]==b_id[i+1]:
    
            if int(t_id[i])==0:
                    t_num0=t_num0+1
            elif int(t_id[i])==1:
                t_num1=t_num1+1
            elif int(t_id[i])==2:
                t_num2=t_num2+1
            else:
                t_num3=t_num3+1
        else:
            j=j+1
            print  u_id[i],b_id[i],t_num0,t_num1,t_num2,t_num3
            if t_num0>=15 or t_num1>=1 :
                output.write(b_id[i])
                output.write(",")
#            else:
#                output.write(b_id[i])
#                output.write(‘,‘) 
            t_num0=0
            t_num1=0
            t_num2=0
            t_num3=0
    
#    else:
    else:
        output.write(‘\n‘)
        ff=0
#        print  u_id[i],b_id[i],t_num0,t_num1,t_num2,t_num3
    i=i+1
    



第二版本,观察时间和购买行为
#coding:utf-8
import time

u_id=[]
b_id=[]
t_id=[]
b_time=[]
t_num0=0
t_num1=0
t_num2=0
t_num3=0
b_num1=0
b_time4=0
a=True
i=0
j=0
fileread=open(‘t_alibaba_data.csv‘,‘r‘)
while True:
    fileline=fileread.readline()
  #  print  type(fileline)
  #  print fileline,
  #  print  i
    filedian =fileline.find(r‘,‘)
    filedian1=fileline.rfind(r‘,‘)

    b_id1=fileline[filedian+1:filedian1-2]
    b_id.append(b_id1)
    
    u_id1=fileline[:filedian]
    u_id.append(u_id1)
    
    t_id1=fileline[filedian1-1:filedian1]
    t_id.append(t_id1)
    
    b_time1=fileline[filedian1:-2]
    b_time.append(b_time1)


    if not fileline:
        break
output=open(‘taobao.txt‘,‘a‘)
#print u_id
#print b_id
#print t_id
#print b_time

ff=0
while True:
    if u_id[i]==u_id[i+1]:
        
        if ff==0:

            output.write(u_id[i])
            output.write(‘   ‘)
        ff=ff+1
        if b_id[i]==b_id[i+1]:
    
            if int(t_id[i])==0:
                    t_num0=t_num0+1
            elif int(t_id[i])==1:
                t_num1=t_num1+1
            elif int(t_id[i])==2:
                t_num2=t_num2+1
            elif b_time[i]!=b_time[i+1]:
                b_time4=b_time4+1
            else:
                t_num3=t_num3+1
        else:
            j=j+1
            b_num1=b_num1+1
            print  u_id[i],b_id[i],t_num0,t_num1,t_num2,t_num3,b_time4
            if t_num0>=15 and t_num1==0:
                output.write(b_id[i])
                output.write(",")
            if b_time4>=2 and t_num1==0:
                output.write(b_id[i])
                output.write(‘,‘) 
            if t_num0>15 and t_num1>=2:
                output.write(b_id[i])
                output.write(‘,‘) 
            if t_num2>=1 and t_num1==0:
                output.write(b_id[i])
                output.write(‘,‘) 
            if len(b_id)<=3:
                output.write(b_id[i])
                output.write(‘,‘) 

#            if b_num1<=3:
#                output.write(b_id[i])
#                output.write(‘,‘) 
#
#
            t_num0=0
            t_num1=0
            t_num2=0
            t_num3=0
            b_time4=0
    
#    else:
    else:
        output.write(‘\n‘)
        b_num1=b_num1+1
#        print  u_id[i],b_id[i],t_num0,t_num1,t_num2,t_num3
    i=i+1
    b_num1=0



第三版本,使用数学分析
#coding:utf-8
import time
import numpy as np
from scipy import optimize
from math import sqrt

u_id=[]
b_id=[]
t_id=[]
b_time=[]
t_num0=0        #类型
t_num1=0
t_num2=0
t_num3=0
b_num1=0        #品牌个数
b_time4=0       #时间
a=True
i=0
j=0
fileread=open(‘t_alibaba_data.csv‘,‘r‘)
while True:
    fileline=fileread.readline()
  #  print  type(fileline)
  #  print fileline,
  #  print  i
    filedian =fileline.find(r‘,‘)
    filedian1=fileline.rfind(r‘,‘)

    b_id1=fileline[filedian+1:filedian1-2]
    b_id.append(b_id1)
    
    u_id1=fileline[:filedian]
    u_id.append(u_id1)
    
    t_id1=fileline[filedian1-1:filedian1]
    t_id.append(t_id1)
    
    b_time1=fileline[filedian1:-2]
    b_time.append(b_time1)


    if not fileline:
        break
output=open(‘taobao.txt‘,‘a‘)
#print u_id
#print b_id
#print t_id
#print b_time
t_num00=[]
t_num11=[]
t_num22=[]
t_num33=[]
t_time44=[]
cc=0
ff=0
pp=0
while True:
    if u_id[i]==u_id[i+1]:
        
        if ff==0:

            output.write(u_id[i])
            output.write(‘   ‘)
        ff=ff+1
        if b_id[i]==b_id[i+1]:
#            cc=cc+1 
            if int(t_id[i])==0:
                    t_num0=t_num0+1
            elif int(t_id[i])==1:
                t_num1=t_num1+1
            elif int(t_id[i])==2:
                t_num2=t_num2+1
            else:
                t_num3=t_num3+1
            if b_time[i]!=b_time[i+1]:
               # print b_time4
                b_time4=b_time4+1
        else:
            j=j+1
        #    b_num1=b_num1+1
#            print  u_id[i],b_id[i],t_num0,t_num1,t_num2,t_num3,b_time4
#            if b_time4>=3:
#                print b_time4
#                pp=pp+1
#       数据拟合分析部分


            t_num00.append(t_num0)
            t_num11.append(t_num1)
            t_num22.append(t_num2)
            t_num33.append(t_num3)
            t_time44.append(b_time4)

#            if t_num0>=10 :
#                output.write(b_id[i])           #看了15次的没有买的
#                output.write(",")
#            elif b_time4>=3 :
#                output.write(b_id[i])           #多天看的,没有买
#                output.write(‘,‘) 
#           # if t_num0>15 and t_num1>=2:
#           #     output.write(b_id[i])
#           #     output.write(‘,‘) 
#            elif t_num2>=1 :
#                output.write(b_id[i])           #收藏出但是没有买
#                output.write(‘,‘) 
#            elif t_num3>=1 :         #放进购物车但是没有买
#                output.write(b_id[i])
#                output.write(‘,‘) 
#          #  if b_time4>=2 and t_num1>=2:
#          #      output.write(b_id[i])
#          #      output.write(‘,‘) 
#          #  
#            elif t_num1>=1:
#                output.write(b_id[i])           #买过两次
#                output.write(‘,‘) 
#                
                
#            if len(b_id)<=3:
#                output.write(b_id[i])
#                output.write(‘,‘) 

#            if b_num1<=3:
#                output.write(b_id[i])
#                output.write(‘,‘) 
#
#
            t_num0=0
            t_num1=0
            t_num2=0
            t_num3=0
            b_time4=0
#    elif b_num1<=3 and ff!=0:
#        print b_id[i]       
#        output.write(b_id[i])
#        output.write(‘\n‘)
#        ff=0
##    else:
    elif not u_id[i+1]:
        break
#    else:
#
#        output.write(‘\n‘)
#        ff=0
#        print  u_id[i],b_id[i],t_num0,t_num1,t_num2,t_num3
    i=i+1
    b_num1=0



#分析浏览次数和购买的关系   
y=np.array(t_num00)
x=np.array(t_num11)

def residuals(p):
    k,b=p
    return y-(k*x-b)

r=optimize.leastsq(residuals,[1,0])
k,b=r[0]
print "K=",k,"b=",b

#分析收藏和购买的关系
x22=np.array(t_num22)

def residuals(p):
    k,b=p
    return y-(k*x22-b)

r=optimize.leastsq(residuals,[1,0])
k22,b22=r[0]
print "Kt_num22=",k22,"b22=",b22

#分析购物车和购买的关系
x33=np.array(t_num33)

def residuals(p):
    k,b=p
    return y-(k*x33-b)

r=optimize.leastsq(residuals,[1,0])
k33,b33=r[0]
print "kt_num33=",k33,"b33=",b33
#查看天数和购物关系
x44=np.array(t_time44)

def residuals(p):
    k,b=p
    return y-(k*x44-b)

r=optimize.leastsq(residuals,[1,0])
k44,b44=r[0]
print "ktime=",k44,"b44=",b44

print pp
#def sim_pearson()



购物数据意淫分析(2)

标签:python

原文地址:http://blog.csdn.net/wangyu190810/article/details/24597729

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!