2017-02-16 89 views
-3

我正在使用協作過濾構建產品推薦引擎(R)。爲了讓更多的利潤項目位於推薦的頂部,我們開發了一個如圖1所示的靈活的業務規則。業務規則應該用於對推薦人輸出進行排序。按照靈活條件排序

+---------------+----------+-----------------+ 
| Sort Priority | Level 1 | Level 2   | 
+---------------+----------+-----------------+ 
| 1    | Brand | Versatile Foods | 
+---------------+----------+-----------------+ 
|    |   | Agro   | 
+---------------+----------+-----------------+ 
|    |   | Specialty Foods | 
+---------------+----------+-----------------+ 
|    |   |     | 
+---------------+----------+-----------------+ 
| 2    | Category | Dairy   | 
+---------------+----------+-----------------+ 
|    |   | Produce   | 
+---------------+----------+-----------------+ 
|    |   | Seafood   | 
+---------------+----------+-----------------+ 
|    |   |     | 
+---------------+----------+-----------------+ 
| 3    | Seasonal | Y    | 
+---------------+----------+-----------------+ 
|    |   | N    | 
+---------------+----------+-----------------+ 
      figure 1 

業務規則:當排序表中,品牌欄目應優先於 類別應優先於季節性。這由列排序優先級的值決定。

當在品牌欄中進行分類時,多功能食品優先於農業和農業 優於特種食品。 如果品牌列中的值在規則中未出現 ,則該值必須按字母順序排序。

相同的排序邏輯應適用於規則定義中的每個條目。

隨着推薦算法的發展。業務規則可以被更改/編輯爲具有更少或更多的級別。對於例如額外的1級條目可能在未來加入,也就是說,類型(猶太,素食,清真)等規則將查找如下:

+---------------+----------+-----------------+ 
| Sort Priority | Level 1 | Level 2   | 
+---------------+----------+-----------------+ 
| 1    | Brand | Versatile Foods | 
+---------------+----------+-----------------+ 
|    |   | Agro   | 
+---------------+----------+-----------------+ 
|    |   | Specialty Foods | 
+---------------+----------+-----------------+ 
|    |   |     | 
+---------------+----------+-----------------+ 
| 2    | Category | Dairy   | 
+---------------+----------+-----------------+ 
|    |   | Produce   | 
+---------------+----------+-----------------+ 
|    |   | Seafood   | 
+---------------+----------+-----------------+ 
|    |   |     | 
+---------------+----------+-----------------+ 
| 3    | Type  | Kosher   | 
+---------------+----------+-----------------+ 
|    |   | Halal   | 
+---------------+----------+-----------------+ 
|    |   | Vegan   | 
+---------------+----------+-----------------+ 
|    |   |     | 
+---------------+----------+-----------------+ 
| 4    | Seasonal | Y    | 
+---------------+----------+-----------------+ 
|    |   | N    | 
+---------------+----------+-----------------+ 
      figure 2 

我需要幫助作爲R將排序構建腳本上述表格(加載到數據框中)通過上述業務規則。 我想解決的真正問題是,我不希望每次將新條目添加到規則時更改代碼。

輸入數據(由推薦引擎輸出)將是這種類型的東西(圖3)。

+-----+-----------------+----------+----------+ 
| SKU | Brand   | Category | Seasonal | 
+-----+-----------------+----------+----------+ 
| 1 | Versatile Foods | Dairy | Y  | 
+-----+-----------------+----------+----------+ 
| 2 | Agro   | Produce | Y  | 
+-----+-----------------+----------+----------+ 
| 3 | Specialty Foods | Seafood | N  | 
+-----+-----------------+----------+----------+ 
| 4 | Agro   | Produce | N  | 
+-----+-----------------+----------+----------+ 
| 5 | Specialty Foods | Organic | Y  | 
+-----+-----------------+----------+----------+ 
| 6 | Agro   | Meat  | N  | 
+-----+-----------------+----------+----------+ 
| 7 | Versatile Foods | Seafood | N  | 
+-----+-----------------+----------+----------+ 
| 8 | USA Bread  | Bakery | Y  | 
+-----+-----------------+----------+----------+ 
| 9 | Specialty Foods | Seafood | N  | 
+-----+-----------------+----------+----------+ 
| 10 | Versatile Foods | Seafood | N  | 
+-----+-----------------+----------+----------+ 
        figure 3 

通過如圖1所示的規則定義,腳本的輸出應該與下表類似。
請注意,品牌=美國麪包在業務規則中沒有發生的情況被放置在排序列表的底部。
另外,對於商品4和商品6,由於在商業規則中沒有找到條目「肉類」,但「商品」是「條目」,因此類別爲'生產'的記錄放置在類別爲'肉類'的記錄上方。

+-----+-----------------+----------+----------+ 
| SKU | Brand   | Category | Seasonal | 
+-----+-----------------+----------+----------+ 
| 1 | Versatile Foods | Dairy | Y  | 
+-----+-----------------+----------+----------+ 
| 7 | Versatile Foods | Seafood | N  | 
+-----+-----------------+----------+----------+ 
| 10 | Versatile Foods | Seafood | N  | 
+-----+-----------------+----------+----------+ 
| 2 | Agro   | Produce | Y  | 
+-----+-----------------+----------+----------+ 
| 4 | Agro   | Produce | N  | 
+-----+-----------------+----------+----------+ 
| 6 | Agro   | Meat  | N  | 
+-----+-----------------+----------+----------+ 
| 3 | Specialty Foods | Seafood | N  | 
+-----+-----------------+----------+----------+ 
| 9 | Specialty Foods | Seafood | N  | 
+-----+-----------------+----------+----------+ 
| 5 | Specialty Foods | Organic | Y  | 
+-----+-----------------+----------+----------+ 
| 8 | USA bread  | Bakery | Y  | 
+-----+-----------------+----------+----------+ 
       figure 4 
+0

我編輯了我的問題,以便更清楚。 –

+0

我編輯了我的問題。有人可以暫停嗎? –

回答

0

您可以使用因子編碼來排序,但無論您想要什麼。例如:

> lvl <- c('Versatile Foods', 'Agro', 'Specialty Foods') 
> lvl <- append(lvl, sort(setdiff(unique(df$Brand), lvl))) 
> 
> df$Brand <- factor(df$Brand, levels=lvl) 
> 
> lvl <- c("Dairy", "Produce", "Seafood") 
> lvl <- append(lvl, sort(setdiff(unique(df$Category), lvl))) 
> 
> df$Category <- factor(df$Category, levels=lvl) 
> 
> df$Seasonal <- factor(df$Seasonal, levels=c('Y', 'N')) 
> 
> 
> df[order(df$Brand, df$Category, df$Seasonal), ] 
    SKU   Brand Category Seasonal 
1 1 Versatile Foods Dairy  Y 
7 7 Versatile Foods Seafood  N 
10 10 Versatile Foods Seafood  N 
2 2   Agro Produce  Y 
4 4   Agro Produce  N 
6 6   Agro Produce  N 
3 3 Specialty Foods Seafood  N 
9 9 Specialty Foods Seafood  N 
5 5 Specialty Foods Organic  Y 
8 8  USA Bread Bakery  Y 
+0

謝謝。這將部分解決我的問題。但是,業務規則是動態的,可能有更多級別,例如類型(猶太教,素食主義者,清真食品等) –

+0

我以爲你想要額外的東西排序阿爾法在最後? –

+0

如果我的評論不清楚,我很抱歉。我的意思是說,我們可能會改變商業規則,我們可能會在規則末尾添加Type(猶太教,素食主義者,清真食品)。我不想在df [order(df $ Brand,df $ Category,df $ Seasonal,df $ type)]中對這些因素進行硬編碼,因爲列表可能一直在變化很大。 –

0

該方法涉及定義排序等級表,然後在與主表合併後使用新列執行排序。

library(dplyr) 
rank <- data_frame(Brand = c('Versatile Foods','Agro','Specialty Foods'), 
        Brand_rank = c(1,2,3)) 
df <- left_join(df, rank, on="Brand") %>% 
    arrange(Brand_rank, Brand, Category, Seasonal) %>% 
    select(-Brand_rank) 
df 
# A tibble: 10 × 4 
# SKU   Brand Category Seasonal 
# <dbl>   <chr> <chr> <chr> 
#1  1 Versatile Foods Dairy  Y 
#2  7 Versatile Foods Seafood  N 
#3  10 Versatile Foods Seafood  N 
#4  4   Agro Produce  N 
#5  6   Agro Produce  N 
#6  2   Agro Produce  Y 
#7  5 Specialty Foods Organic  Y 
#8  3 Specialty Foods Seafood  N 
#9  9 Specialty Foods Seafood  N 
#10  8  USA Bread Bakery  Y