加载中…
个人资料
bicloud
bicloud
  • 博客等级:
  • 博客积分:0
  • 博客访问:378,075
  • 关注人气:492
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

Python 指数移动平均 商品浏览趋势预测

(2012-05-31 09:20:51)
标签:

杂谈

分类: python

商品浏览次数,商品销量预测

base line方法

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
from math import log, sqrt

def str2list(strDateNum, separator):
    dDateNum = {}
    date = []
    view = []
    for i in strDateNum.split(separator):
        d, v = i.split(':')
        dDateNum.setdefault(int(d), int(v))
   
    l = sorted([(k, v) for k, v in dDateNum.iteritems()])
   
    for item in l:
        date.append(item[0])
        view.append(item[1])
       
    return date, view

def calc_month_trend(strDateNum):
    date, view = str2list(strDateNum, ',')
    trend2 = sum(view[-15 : ])
    trend1 = 1.0 * sum(view[-30 : -15])
    month_trend = trend2 - trend1
   
    date_str = '[%s]' % ','.join(map(str, date))
    view_str = '[%s]' % ','.join(map(str, view))
   
    return month_trend, date_str, view_str

def calc_daily_trend(strDateNum, Total):
    date, view = str2list(strDateNum, ',')
    y2 = view[-1]
    y1 = view[-8]
    weekly_view = sum(view[-8 : -1])
    slope = y2 - y1
    trend = round(slope  * (1.0 + log(1.0 +int(weekly_view))))
    error = round(1.0 / sqrt(int(Total)), 2)
    return trend, error

if __name__ == '__main__':
   
    #main()
    strList = '2:3,1:4,5:4,34:3,4:56,33:4,45:23,23:5,12:1,\
    13:2,14:3,9:0,10:3,100:1,101:2,103:3,104:4,105:5,106:6,\
    107:7,108:8,109:9,110:10,111:12,\
    112:12,113:13,114:1,115:34,116:16,117:17'
    a1, a2 = str2list(strList, ',')
    print a1
    print a2
    month_trend, date_str, view_str  = calc_month_trend(strList)
    print month_trend
    print date_str
    print view_str
 
    daily_trend, error = calc_daily_trend(strList, 50)
    print daily_trend
    print error

 

测试结果:

[1, 2, 4, 5, 9, 10, 12, 13, 14, 23, 33, 34, 45, 100, 101, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117]
[4, 3, 56, 4, 0, 3, 1, 2, 3, 5, 4, 3, 23, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 12, 13, 1, 34, 16, 17]
43.0
[1,2,4,5,9,10,12,13,14,23,33,34,45,100,101,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117]
[4,3,56,4,0,3,1,2,3,5,4,3,23,1,2,3,4,5,6,7,8,9,10,12,12,13,1,34,16,17]
39.0
0.14

PS:

 

实际数据可以进行检验,这里主要是简单的测试

一种基本的趋势预测方法,base line方法

经过实际检验,目前采用指数移动平均方法处理误差较小(注意小技巧,周末的数据需要另外处理)

数据量大的情况下,采用python + hadoop streaming方式处理,速度很快

指数移动平均法http://en.wikipedia.org/wiki/Moving_average

这个用来做业务团队KPI预测,譬如成交量,销量等等

 

指数移动平均结果

Day 01 value=143.00 2-bar EMA=143.000000 and SMA=143.000000
Day 02 value=161.00 2-bar EMA=152.000000 and SMA=152.000000
Day 03 value=328.00 2-bar EMA=310.400000 and SMA=244.500000
Day 04 value=198.00 2-bar EMA=209.240000 and SMA=263.000000
Day 05 value=166.00 2-bar EMA=170.324000 and SMA=182.000000
Day 06 value=130.00 2-bar EMA=134.032400 and SMA=148.000000
Day 07 value=125.00 2-bar EMA=125.903240 and SMA=127.500000
Day 08 value=128.00 2-bar EMA=127.790324 and SMA=126.500000
Day 09 value=133.00 2-bar EMA=132.479032 and SMA=130.500000
Day 10 value=162.00 2-bar EMA=159.047903 and SMA=147.500000
Day 11 value=124.00 2-bar EMA=127.504790 and SMA=143.000000
Day 12 value=131.00 2-bar EMA=130.650479 and SMA=127.500000
Day 13 value=132.00 2-bar EMA=131.865048 and SMA=131.500000
Day 14 value=125.00 2-bar EMA=125.686505 and SMA=128.500000
Day 15 value=109.00 2-bar EMA=110.668650 and SMA=117.000000
Day 16 value=151.00 2-bar EMA=146.966865 and SMA=130.000000
Day 17 value=111.00 2-bar EMA=114.596687 and SMA=131.000000
Day 18 value=103.00 2-bar EMA=104.159669 and SMA=107.000000
Day 19 value=170.00 2-bar EMA=163.415967 and SMA=136.500000
Day 20 value=117.00 2-bar EMA=121.641597 and SMA=143.500000
Day 21 value=93.00 2-bar EMA=95.864160 and SMA=105.000000
Day 22 value=88.00 2-bar EMA=88.786416 and SMA=90.500000
Day 23 value=82.00 2-bar EMA=82.678642 and SMA=85.000000
Day 24 value=100.00 2-bar EMA=98.267864 and SMA=91.000000
Day 25 value=71.00 2-bar EMA=73.726786 and SMA=85.500000
Day 26 value=80.00 2-bar EMA=79.372679 and SMA=75.500000
Day 27 value=95.00 2-bar EMA=93.437268 and SMA=87.500000
Day 28 value=122.00 2-bar EMA=119.143727 and SMA=108.500000
Day 29 value=67.00 2-bar EMA=72.214373 and SMA=94.500000
Day 30 value=78.00 2-bar EMA=77.421437 and SMA=72.500000

扩展为数据产品,进行信息纰漏

image

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有