统计全国仓库预测的前两周 商品_仓库_个数
rm(list=ls())
w=read.table("F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/item_feature2.csv",header = FALSE,sep=",")
e=unique(w$V2)
x=0
for (i in 1:length(e)){
w1=w[which(w$V2==e[i]),]
t1=w1[which(w1$V1 <= "20151227"),]
t2=t1[t1$V1>="20151214",]
x[i]=sum(t2$V30) #满足两个条件 拆开写
}
y=c(1:length(e))
da=data.frame(e,y,x)
da$y='all'
write.table (da, file ="F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/result01.csv",sep =",",row.names = F,col.names=F,quote =F)
统计区域仓库预测的前两周 商品_仓库_个数
rm(list=ls())
w=read.table("F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/item_store_feature2.csv",header = FALSE,sep=",")
library("stringr")
r1=as.numeric(str_c(w$V2,w$V3,sep='')) #id+仓库号为唯一标示
e=unique(r1)
r=data.frame(r1,w$V31,w$V1)
x=0
for (i in 1:length(e)){
w1=r[which(r$r1==e[i]),]
t1=w1[which(w1$w.V1>="20151214"),]
t2=t1[which(t1$w.V1 <= "20151227"),]
x[i]=sum(t2$w.V3)
}
tmp=data.frame(w$V2,w$V3)
e1=unique.data.frame(tmp)
da=data.frame(e1,e,x)
da2=data.frame(e1,x)
write.table (da2, file ="F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/result02.csv",sep =",",row.names = F,col.names=F,quote =F)
da[which(da$x>300),]$x=da[which(da$x>300),]$x*1.1
da$x=floor(da$x)
write.table (da, file ="F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/result01_09.csv",sep =",",row.names = F,col.names=F,quote =F).
未来两周的成交量
特征提取
最近两周的成交量x3
最近一周的成交量x2
最近三周的成交量x4
前一个月的成交量x5
最近一天的成交量x1
全国7天间隔的序列
rm(list=ls())
d=seq(as.Date("2014/10/06"),as.Date("2015/12/27"), by="day") #生成时间序列
library("stringr")
d=as.numeric(str_replace(str_replace(d,"-",""),"-","")) #替换替换匹配的部分library("stringr")
d=t(as.matrix(as.data.frame(d)))
d=matrix(d,ncol=7,byrow = TRUE) #将矩阵按行切割成14列
w=read.table("F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/item_feature2.csv",header = FALSE,sep=",")
e=unique(w$V2)
x=matrix(rep(0,64),nrow=length(e),ncol=64)
x1=0
x2=0
x3=0
x4=0
x5=0
for (i in 1:length(e)){
w1=w[which(w$V2==e[i]),]
x1[i]=sum(w1[which(w1$V1>="20151227"),]$V30)
for (j in 1:64){
t1=w1[which(w1$V1>=d[j,1]),]
t2=t1[which(t1$V1 <= d[j,7]),]
x[i,j]=sum(t2$V30)
}
x2[i]=x[i,64]
x3[i]=x[i,64]+x[i,63]
x4[i]=x[i,64]+x[i,63]+x[i,62]
x5[i]=x[i,57]+x[i,58]+x[i,59]+x[i,60]
}
x[is.na(x)]=0
y=c(1:length(e))
da=data.frame(e,y,x1,x2,x3,x4,x5)
da$y='all'
write.table (da, file ="F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/all_feature.csv",sep =",",row.names = F,col.names=F,quote =F)
训练模型
预测前一天的成交量x1-V3 x1[i]=sum(w1[which(w1$V1>="20151213"),]$V30) -12.13s-双12后一天
预测前一周的成交量x2-V4 x[i,62] 12.07-12.13
预测前两周的成交量x3-V5 x[i,62]+x[i,61] 11.30-12.13 含双12
预测前三周的成交量x4-V6 x[i,62]+x[i,61]+x[i,60] 11.23-12.13 含双12
预测前一个月(28天)的成交量x5-V7 x[i,55]+x[i,56]+x[i,57]+x[i,58] 10.19-11.15含有双11
预测的两周的成交量x6-V8 x[i,64]+x[i,63] 12.14-12.27
rm(list=ls())
d=seq(as.Date("2014/10/06"),as.Date("2015/12/27"), by="day") #生成时间序列
library("stringr")
d=as.numeric(str_replace(str_replace(d,"-",""),"-","")) #替换替换匹配的部分library("stringr")
d=t(as.matrix(as.data.frame(d)))
d=matrix(d,ncol=7,byrow = TRUE) #将矩阵按行切割成14列
w=read.table("F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/item_feature2.csv",header = FALSE,sep=",")
e=unique(w$V2)
x=matrix(rep(0,64),nrow=length(e),ncol=64)
x1=0
x2=0
x3=0
x4=0
x5=0
x6=0
for (i in 1:length(e)){
w1=w[which(w$V2==e[i]),]
tt1=w1[which(w1$V1>="20151213"),]
tt2=tt1[which(tt1$V1<="20151213"),]
x1[i]=sum(tt2$V30) #修改sum
for (j in 1:64){
t1=w1[which(w1$V1>=d[j,1]),]
t2=t1[which(t1$V1 <= d[j,7]),]
x[i,j]=sum(t2$V30)
}
x2[i]=x[i,62]
x3[i]=x[i,62]+x[i,61]
x4[i]=x[i,62]+x[i,61]+x[i,60]
x5[i]=x[i,55]+x[i,56]+x[i,57]+x[i,58]
x6[i]=x[i,64]+x[i,63]
}
x[is.na(x)]=0
y=c(1:length(e))
da=data.frame(e,y,x1,x2,x3,x4,x5,x6)
da$y='all'
write.table (da, file ="F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/all_feature_train.csv",sep =",",row.names = F,col.names=F,quote =F)
计算相关性--直接用命令cor(矩阵)就可以了呀。矩阵的第(i,j)个元素就是第i与第j个变量的相关系数
da2=data.frame(x1,x2,x3,x4,x5,x6)
cor(da2)
cor(x1,x6)
cor.test(x1,x6)
1、构建前几天的时间特征---eg:1day 1week 2week 1month 3month 6month 9month 1year ....
时间角度:每一个月前10天中间10天的数据,最后10天..
用户角度:四个类目下不同时间维度下的消费特征每个用户14个月不同天的数据
预测角度:取不同的数据时间段进行预测,对于你得到的预测数据进行一个加权融合
每月每天新增用户的数量
2、计算其相关性---MATLAB平台的库函数Corrcoef,得出的各字段之间的相关性分析--R cor(x,y) cor.test(x,y)
rm(list=ls())
w=read.table("F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/all_feature_train.csv",header = FALSE,sep=",")
w1=w
da=data.frame(w1$V3,w1$V4,w1$V5,w1$V6,w1$V7,w1$V8)
#数据归一化--消除量纲对数据结构的影响
#dad=scale(da,center=F,scale=T)
dad=da
colnames(dad) <- c("V1","V2","V3","V4","V5","V6")
dad=as.data.frame(dad)
dad1=dad[c(1:(length(dad$V1)-10)),]
dad2=dad[c((length(dad$V1)-9):length(dad$V1)),]
library("forecast")
V1=dad1$V1
V2=dad1$V2
V3=dad1$V3
V4=dad1$V4
V5=dad1$V5
V6=dad1$V6
r=lm(V6~V1+I(V2)+I(V3)+I(V4)+I(V5))
p=floor(predict(r,dad2))
t=dad2$V6
data.frame(p,t)
w[954,]
17449 all--受双11影响特别大的--直接lm预测不好--top2week ok
2806 all
2980 all--直接lm预测-出负数--top2week也不好
V1 V2 V3 V4 V5 V6 V7 V8
954 17449 all 11 194 244 325 1797 168
955 19417 all 11 178 247 311 161 137
956 26977 all 6 58 94 117 267 103
957 2806 all 26 716 843 979 2340 781
958 28093 all 2 11 15 24 27 28
959 2980 all 22 410 501 624 5102 126
960 30541 all 2 34 55 65 137 37
961 3346 all 2 7 19 22 63 47
962 3424 all 37 1155 1528 1870 2617 718
963 4969 all 4 53 92 100 91 54
p t 2w t-p t-2w
954 48 168 194 120 -26 --受双11影响特别大的--直接lm预测不好--top2week ok
955 164 137 178 -27 -41 --差距不大
956 71 103 58 32 45 --差距不大
957 256 781 716 525 65 --受双11影响特别大的--直接lm预测不好--top2week ok
958 17 28 11 11 17 --差距不大
959 -4 126 410 130 -284 --直接lm预测-出负数--top2week也不好
960 25 37 34 12 3 --差距不大
961 20 47 7 27 40 --差距不大
962 591 718 1155 127 -437 --受双12影响特别大的--直接lm预测不好--top2week也不好
963 63 54 53 -9 1 --差距不大
找出异常值
boxplot.stats(t1)
预测一周21-27 x6----构造大量特征值用降维度方法判断选什么
预测前一天的成交量x1 12.20
预测前一周的成交量x2 12.14-12.20
预测前两周的成交量x3 12.07-12.20 含双12 (去双12,去双11)
预测前三周的成交量x4 11.30-12.20 含双12
预测前一个月(28天)的成交量x5 11.23-12.20 含双12
预测一周的成交量x6 12.21-12.27
划分7天的周期
rm(list=ls())
d=seq(as.Date("2014/10/06"),as.Date("2015/12/27"), by="day") #生成时间序列
library("stringr")
d=as.numeric(str_replace(str_replace(d,"-",""),"-","")) #替换替换匹配的部分library("stringr")
#difftime(as.Date("2014-4-25"),as.Date("2014-4-20")) #时间差
d=t(as.matrix(as.data.frame(d)))
d=matrix(d,ncol=7,byrow = TRUE) #将矩阵按行切割成14列
w=read.table("F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/item_feature2.csv",header = FALSE,sep=",")
#去双11 双12
w[which(w$V1==20151212),]$V30=0
w[which(w$V1==20151111),]$V30=0
#----
e=unique(w$V2)
x=matrix(rep(0,64),nrow=length(e),ncol=64)
for (i in 1:length(e)){
w1=w[which(w$V2==e[i]),]
for (j in 1:64){
t1=w1[which(w1$V1>=d[j,1]),]
t2=t1[which(t1$V1<= d[j,7]),]
x[i,j]=sum(t2$V30)
}
}
#x[is.na(x)]=0
y=c(1:length(e))
da=data.frame(e,y,x)
da$y='all'
x1=0
for (i in 1:length(e)){
w1=w[which(w$V2==e[i]),]
tt1=w1[which(w1$V1>="20151220"),]
tt2=tt1[which(tt1$V1<="20151220"),]
x1[i]=sum(tt2$V30)
}
x2=da$X63
x3=da$X62+da$X63
x4=da$X61+da$X62+da$X63
x5=da$X60+da$X61+da$X62+da$X63
x6=da$X64
dad=data.frame(da$e,da$y,x1,x2,x3,x4,x5,x6)
write.table (dad, file ="F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/all_feature_train22.csv",sep =",",row.names = F,col.names=F,quote =F)
rm(list=ls())
w=read.table("F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/all_feature_train22.csv",header = FALSE,sep=",")
w1=w
da=data.frame(w1$V3,w1$V4,w1$V5,w1$V6,w1$V7,w1$V8)
#数据归一化--消除量纲对数据结构的影响
#dad=scale(da,center=F,scale=T)
dad=da
colnames(dad) <- c("V1","V2","V3","V4","V5","V6")
dad=as.data.frame(dad)
dad1=dad[c(1:(length(dad$V1)-10)),]
dad2=dad[c((length(dad$V1)-9):length(dad$V1)),]
library("forecast")
V1=dad1$V1
V2=dad1$V2
V3=dad1$V3
V4=dad1$V4
V5=dad1$V5
V6=dad1$V6
r=lm(V6~V1+I(V2)+I(V3)+I(V4)+I(V5))
p=floor(predict(r,dad2))
t=dad2$V6
data.frame(p,t,abs(p-t),abs(dad2$V2-t))
p=floor(predict(r,dad))
t=dad$V6
data.frame(p,t,abs(p-t))
cor(da2)
w[904,]
ggg=read.table("F:/笔记学习/天池比赛/菜鸟需求预测与分仓规划/data2/item_feature2.csv",header = FALSE,sep=",")
t1=ggg[which(ggg$V2==2806),]
t1[which(t1$V1=="20151212"),]
954 76 90 14 12
955 66 69 3 1
956 41 48 7 7
957 258 478 220 175
958 12 16 4 4
959 93 57 36 12
960 19 19 0 1
961 9 36 27 25
962 321 404 83 90
963 14 34 20 14
去掉双11 12
954 69 90 21 12
955 72 69 3 1
956 43 48 5 7
957 195 478 283 175
958 11 16 5 4
959 67 57 10 12
960 28 19 9 1
961 9 36 27 25
962 268 404 136 90
963 21 34 13 14
预测的数据n2w
1w Double, 07-13
2w Double, 30-13
3w Double, 23-13
1m Double, 16-13
2m Double, 10.05-13
1d Double, 12.13
2d Double, 12.12
3d Double, 12.11
n2w Double 2015-12.14--2015.12.27