public int GetLogisticRegression(DidiDBDetail detailData, DidiPerData fixData, LogitCoef LogitCoefData) { log.Info("GetLogisticRegression data :{0}", DateTime.Now); float t2 = LogitCoefData.listLogitCoef.Where(w => w.coef_name == "(Intercept):2").Select(s => s.coef).FirstOrDefault(); t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "click:2").Select(s => s.coef).FirstOrDefault() * fixData.click; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "roi:2").Select(s => s.coef).FirstOrDefault() * fixData.roi; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "imp_rank1:2").Select(s => s.coef).FirstOrDefault() * fixData.imp_rank1; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "pc_score:2").Select(s => s.coef).FirstOrDefault() * fixData.pc_score; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "ctr_ind:2").Select(s => s.coef).FirstOrDefault() * fixData.ctr_ind; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "cpc_ind:2").Select(s => s.coef).FirstOrDefault() * fixData.cpc_ind; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "d_limt_ind:2").Select(s => s.coef).FirstOrDefault() * fixData.d_limt_ind; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "d_spend_ind:2").Select(s => s.coef).FirstOrDefault() * fixData.d_spend_ind; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "price_limt_ind:2").Select(s => s.coef).FirstOrDefault() * fixData.price_limit_ind; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "cpc_mm:2").Select(s => s.coef).FirstOrDefault() * fixData.cpc_mm; t2 = t2 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "cpc_p:2").Select(s => s.coef).FirstOrDefault() * fixData.cpc_p; float t3 = LogitCoefData.listLogitCoef.Where(w => w.coef_name == "(Intercept):3").Select(s => s.coef).FirstOrDefault(); t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "click:3").Select(s => s.coef).FirstOrDefault() * fixData.click; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "roi:3").Select(s => s.coef).FirstOrDefault() * fixData.roi; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "imp_rank1:3").Select(s => s.coef).FirstOrDefault() * fixData.imp_rank1; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "pc_score:3").Select(s => s.coef).FirstOrDefault() * fixData.pc_score; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "ctr_ind:3").Select(s => s.coef).FirstOrDefault() * fixData.ctr_ind; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "cpc_ind:3").Select(s => s.coef).FirstOrDefault() * fixData.cpc_ind; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "d_limt_ind:3").Select(s => s.coef).FirstOrDefault() * fixData.d_limt_ind; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "d_spend_ind:3").Select(s => s.coef).FirstOrDefault() * fixData.d_spend_ind; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "price_limt_ind:3").Select(s => s.coef).FirstOrDefault() * fixData.price_limit_ind; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "cpc_mm:3").Select(s => s.coef).FirstOrDefault() * fixData.cpc_mm; t3 = t3 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "cpc_p:3").Select(s => s.coef).FirstOrDefault() * fixData.cpc_p; float t4 = LogitCoefData.listLogitCoef.Where(w => w.coef_name == "(Intercept):4").Select(s => s.coef).FirstOrDefault(); t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "click:4").Select(s => s.coef).FirstOrDefault() * fixData.click; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "roi:4").Select(s => s.coef).FirstOrDefault() * fixData.roi; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "imp_rank1:4").Select(s => s.coef).FirstOrDefault() * fixData.imp_rank1; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "pc_score:4").Select(s => s.coef).FirstOrDefault() * fixData.pc_score; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "ctr_ind:4").Select(s => s.coef).FirstOrDefault() * fixData.ctr_ind; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "cpc_ind:4").Select(s => s.coef).FirstOrDefault() * fixData.cpc_ind; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "d_limt_ind:4").Select(s => s.coef).FirstOrDefault() * fixData.d_limt_ind; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "d_spend_ind:4").Select(s => s.coef).FirstOrDefault() * fixData.d_spend_ind; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "price_limt_ind:4").Select(s => s.coef).FirstOrDefault() * fixData.price_limit_ind; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "cpc_mm:4").Select(s => s.coef).FirstOrDefault() * fixData.cpc_mm; t4 = t4 + LogitCoefData.listLogitCoef.Where(w => w.coef_name == "cpc_p:4").Select(s => s.coef).FirstOrDefault() * fixData.cpc_p; double prob2 = Math.Exp(t2) / (1 + Math.Exp(t2) + Math.Exp(t3) + Math.Exp(t4)); double prob3 = Math.Exp(t3) / (1 + Math.Exp(t2) + Math.Exp(t3) + Math.Exp(t4)); double prob4 = Math.Exp(t4) / (1 + Math.Exp(t2) + Math.Exp(t3) + Math.Exp(t4)); double prob1 = 1 - prob2 - prob3 - prob4; double[] maxProb = { prob1, prob2, prob3, prob4 }; var max = maxProb.Max(); var result = Array.IndexOf(maxProb, max); log.Info("GetLogisticRegression data end :{0}", DateTime.Now); return result + 1; }
public float GetLinearRegression(DidiDBDetail detailData, DidiPerData fixData, LinearCoef LinearCoefData, int logisticRegression) { log.Info("GetLinearRegression data :{0}", DateTime.Now); if (logisticRegression == 1 || logisticRegression == 3) { Dictionary<string, float > dicLinear = new Dictionary<string, float>(); string direction = logisticRegression == 1 ? "Increase" : "Decrease"; dicLinear = LinearCoefData.ListCoef.Where(w => w.promo_strgy == detailData.promo_strgy && w.keyword_cat == detailData.keyword_cat && w.direction == direction).Select(s => new { s.coef_name, s.coef }).ToDictionary(x => x.coef_name, x=>x.coef); var ddd = LinearCoefData.ListCoef.Where(w => w.coef_type == 1).Select(s => s.coef).FirstOrDefault(); float f_per_p = dicLinear["click"] * fixData.click + dicLinear["imp"] * fixData.imp + dicLinear["conv"] * fixData.conv + dicLinear["roi"] * fixData.roi + dicLinear["imp_rank1"] * fixData.imp_rank1 + dicLinear["pc_score"] * fixData.pc_score + dicLinear["ctr"] * fixData.ctr + dicLinear["ctr_ind"] * fixData.ctr_ind + dicLinear["cpc"] * fixData.cpc + dicLinear["cpc_ind"] * fixData.cpc_ind + dicLinear["d_spend_ind"] * fixData.d_spend_ind + dicLinear["cpc_limt_ind"] * fixData.cpc_limit_ind; //float f_per_p = dicLinear["click"] * fixData.click; //f_per_p = f_per_p + dicLinear["imp"] * fixData.imp; //f_per_p = f_per_p + dicLinear["conv"] * fixData.conv; //f_per_p = f_per_p + dicLinear["roi"] * fixData.roi; //f_per_p = f_per_p + dicLinear["imp_rank1"] * fixData.imp_rank1; // f_per_p = f_per_p + dicLinear["pc_score"] * fixData.pc_score; //f_per_p = f_per_p + dicLinear["ctr"] * fixData.ctr; //f_per_p = f_per_p + dicLinear["ctr_ind"] * fixData.ctr_ind; //f_per_p = f_per_p + dicLinear["cpc"] * fixData.cpc; //f_per_p = f_per_p + dicLinear["cpc_ind"] * fixData.cpc_ind; //f_per_p = f_per_p + dicLinear["d_spend_ind"] * fixData.d_spend_ind; //f_per_p = f_per_p + dicLinear["cpc_limt_ind"] * fixData.cpc_limit_ind; log.Info("GetLinearRegression data end :{0}", DateTime.Now); return f_per_p; } else { log.Info("GetLinearRegression data end nan :{0}", DateTime.Now); return float.NaN; } }
// POST api/<controller> public HttpResponseMessage Post(DidiDbData didi) { log.Info("get post request :{0}", DateTime.Now); //取算法參數 Func func = new Func(); GetDataArgs gatArgs = new GetDataArgs(); LinearCoef LinearCoefData = gatArgs.GetDataArgsByFile(); LogitCoef LogitCoefData = gatArgs.GetLogitCoefArgsByFile(); log.Info("get file data :{0}", DateTime.Now); try { var didiData = didi; string sqlStr = ""; string didiToJson = JsonConvert.SerializeObject(didi); //func.writeFile(didiToJson); DidiPerData fixData = new DidiPerData(); List<DidiDBDetail> listDidiData = new List<DidiDBDetail>(); foreach (var di in didiData.dididbdetail) { if (di.promo_id == "203200334" && di.keyword == "电炉丝免邮资") { string asdadasdas = "asdasdas"; } //click : 点击量 float click = di.click; fixData.click = click; //roi : 投入产出比 float roi = di.roi > 10000 ? 10000 : di.roi; fixData.roi = roi; //imp : 展现量 float imp = di.imp; fixData.imp = imp; //imp_rank1 : 调整后的平均展现排名 float imp_rank = float.IsNaN(di.imp_rank) ? 0 : di.imp_rank; float imp_rank1 = 1 / imp_rank; imp_rank1 = float.IsInfinity(imp_rank1) ? 0 : imp_rank1; fixData.imp_rank1 = imp_rank1; //pc_soire : 计算机质量分数 float pc_soire = di.pc_score; fixData.pc_score = pc_soire; //conv : 转化率 float conv = di.conv; fixData.conv = conv; //ctr : 点击率 float ctr = di.ctr; fixData.ctr = ctr; //ctr_ind : 点击率Index float ctr_ind = 0; float all_ctr = 0; all_ctr = di.all_ctr == -1 ? float.NaN : di.all_ctr; if (float.IsNaN(all_ctr)) { ctr_ind = 1; } else { ctr_ind = di.ctr / all_ctr; ctr_ind = float.IsNaN(ctr_ind) ? 1 : ctr_ind; ctr_ind = float.IsInfinity(ctr_ind) ? 1000 : ctr_ind; } fixData.ctr_ind = ctr_ind; //cpc : 平均点击花费 float cpc = di.cpc; fixData.cpc = cpc; //cpc_ind : 平均点击花费index float cpc_ind = 0; float all_price = 0; all_price = di.all_price == -1 ? float.NaN : di.all_price; if (float.IsNaN(all_price)) { cpc_ind = 1; } else { cpc_ind = di.cpc / all_price; cpc_ind = float.IsNaN(cpc_ind) ? 1 : cpc_ind; cpc_ind = float.IsInfinity(cpc_ind) ? 1000 : cpc_ind; } fixData.cpc_ind = cpc_ind; //d_limt_ind : 计画日限额index float d_limt_ind = di.daily_limt / di.yes_cpc; d_limt_ind = float.IsInfinity(d_limt_ind) ? 1000 : d_limt_ind; d_limt_ind = di.daily_limt == 20000000 ? 1000 : d_limt_ind; fixData.d_limt_ind = d_limt_ind; //d_spend_ind : 计画花费index float d_spend_ind = di.yes_spend / di.daily_limt; fixData.d_spend_ind = d_spend_ind; // cpc_limit_ind :平均点击花费限价index float cpc_limit_ind = 0; if (di.price_limt == 0) { cpc_limit_ind = 0; } else { if (di.cpc <= di.price_limt) { cpc_limit_ind = di.cpc / di.price_limt; } else { cpc_limit_ind = 1000; } } cpc_limit_ind = float.IsNaN(cpc_limit_ind) ? 0 : cpc_limit_ind; cpc_limit_ind = float.IsInfinity(cpc_limit_ind) ? 1000 : cpc_limit_ind; fixData.cpc_limit_ind = cpc_limit_ind; //price_limit_ind : 調價限價index float price_limit_ind = 0; if (di.price_limt == 0) { price_limit_ind = 0; } else { if (di.price_now <= di.price_limt) { price_limit_ind = di.price_now / di.price_limt; } else { price_limit_ind = 10; } } price_limit_ind = float.IsNaN(price_limit_ind) ? 0 : price_limit_ind; price_limit_ind = float.IsInfinity(price_limit_ind) ? 0 : price_limit_ind; fixData.price_limit_ind = price_limit_ind; //cpc_mm : 平均点击花费平均index float cpc_mm = di.cpc / di.cat_cpc; cpc_mm = float.IsNaN(cpc_mm) ? 0 : cpc_mm; cpc_mm = float.IsInfinity(cpc_mm) ? 0 : cpc_mm; fixData.cpc_mm = cpc_mm; //cpc_p : 平均点击花费综合index float cpc_p = di.cpc >= di.price_now ? 1 : 0; float price_temp = di.price_now > di.price_limt ? 40 : 0; float cpc_mm_temp = cpc_mm >= 0.5 ? 10 : 0; cpc_p = cpc_p + price_temp + cpc_mm_temp; cpc_p = float.IsNaN(cpc_p) ? 0 : cpc_p; fixData.cpc_p = cpc_p; //f_per_cat : 操作类型 int f_per_cat = 0; switch (di.output) { case "加价": f_per_cat = 1; break; case "删除": f_per_cat = 2; break; case "减价": f_per_cat = 3; break; case "保留": f_per_cat = 4; break; } fixData.f_per_cat = f_per_cat; //f_per2 : 调价幅度(%) float f_per2 = ((di.price_new - di.price_now) / di.price_now ) *100; fixData.f_per2 = f_per2; //f_per_cat_p : Migo操作类型预测 int f_per_cat_p_int = func.GetLogisticRegression(di, fixData, LogitCoefData); string f_per_cat_p = ""; switch (f_per_cat_p_int) { case 1: f_per_cat_p = "加价"; break; case 2: f_per_cat_p = "删除"; break; case 3: f_per_cat_p = "减价"; break; default: f_per_cat_p = "NA"; break; } di.f_per_cat_p = f_per_cat_p; //f_per_p : Migo调价幅度预测 float f_per_p = func.GetLinearRegression(di, fixData, LinearCoefData, f_per_cat_p_int); f_per_p = float.IsNaN(f_per_p) ? 0 : f_per_p; di.f_per_p = f_per_p; //寫DB //func.InsertDb(di); //寫SQL sqlStr = sqlStr + "insert into sdata (plan_id, daily_limt, yes_spend, yes_cpc, cat_id, cat_name, cat_cpc, promo_id, promo_strgy, promo_item_cat, promo_imp, promo_click, promo_ctr, promo_tot_spend, promo_cpc, promo_rev, promo_amt, promo_saved, promo_conv, promo_roi, keyword, price_now, pc_score, mb_score, price_limt, keyword_cat, imp, click, ctr, tot_spend, cpc, rev, amt, saved, conv, roi, imp_rank, all_imp, all_click, all_price, all_ctr, all_compe, all_conv, keyword2, price_new, output, adj_p, f_per_cat_p, f_per_p)"; sqlStr = sqlStr + "VALUES ("; sqlStr = sqlStr + "'" + di.plan_id + "'," + di.daily_limt + "," + di.yes_spend + "," + di.yes_cpc + ",'" + di.cat_id + "','" + di.cat_name + "'," + di.cat_cpc + ",'" + di.promo_id + "','" + di.promo_strgy + "','" + di.promo_item_cat + "'," + di.promo_imp + "," + di.promo_click + "," + di.promo_ctr + "," + di.promo_tot_spend + "," + di.promo_cpc + "," + di.promo_rev + "," + di.promo_amt + "," + di.promo_saved + "," + di.promo_conv + "," + di.promo_roi + ",'" + di.keyword + "'," + di.price_now + "," + di.pc_score + "," + di.mb_score + "," + di.price_limt + ",'" + di.keyword_cat + "'," + di.imp + "," + di.click + "," + di.ctr + "," + di.tot_spend + "," + di.cpc + "," + di.rev + "," + di.amt + "," + di.saved + "," + di.conv + "," + di.roi + "," + di.imp_rank + "," + di.all_imp + "," + di.all_click + "," + di.all_price + "," + di.all_ctr + "," + di.all_compe + "," + di.all_conv + ",'" + di.keyword2 + "'," + di.price_new + ",'" + di.output + "'," + di.adj_p + ",'" + di.f_per_cat_p + "'," + di.f_per_p + ")"; sqlStr = sqlStr + "\n"; //SQL寫TXT //func.writeFile(@"d:\dididididi.txt", sqlStr); //string fileName = @"d:\" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + ".txt"; //string fileName = @"d:\dididididi4.txt"; //func.WriteLog(@fileName, sqlStr); //func.InsertZMQ(sqlStr); //一個寶貝的資料先存起來 listDidiData.Add(di); } //一個寶貝的資料先存起來,再一起進DB func.InsertDb(listDidiData); //SQL寫Queue //func.IsertActiveQueue(sqlStr); //func.IsertMSQueue(sqlStr); //func.InsertNetMQ(sqlStr); //SQL寫TXT //func.WriteLog(@"d:\dididididiWriteLog.txt", sqlStr); log.Info("response data :{0}", DateTime.Now); string json = JsonConvert.SerializeObject(didiData); //資料存下來 //func.writeFile(json); var result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StringContent(json); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return result; } catch (Exception ex) { log.Error("error :{0}", ex.Message); return null; } }