我正在使用協作過濾構建產品推薦引擎(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
我編輯了我的問題,以便更清楚。 –
我編輯了我的問題。有人可以暫停嗎? –