建立 Naive Bayes 模型的範例
此範例顯示如何定義離散化演算法及 bin 數目。
假設 Naive Bayes 分類器的應用程式屬於客戶流失資料集。輸入資料可以包含連續歸因類型和名義屬性類型。所有類型均可自動偵測得知,或透過使用者指定的方法來偵測。若要手動定義類型,請使用 incolumn 及 coldeftype 參數。
客戶流失資料集只包含數值直欄。不過,需要有 VARCHAR 直欄才能建立 Naive Bayes 模型。下列呼叫顯示如何建立一個以客戶流失資料集為基礎並包含 VARCHAR 直欄的視圖。此外,此視圖還包含預測所需的直欄。
CREATE VIEW CUSTOMER_CHURN AS SELECT (CASE WHEN SAMPLES.CUSTOMER_CHURN."CENSOR" = '1' THEN 'YES' ELSE 'NO' END) CHURN, IN_B2B_INDUSTRY, TOTAL_BUY, ANNUAL_REVENUE_MIL, CUST_ID AS ID from SAMPLES.CUSTOMER_CHURN;
Naive Bayes 預測需要一個包含訓練資料的表格。下列呼叫顯示如何從 CUSTOMER_CHURN 視圖建立此表格:
CALL IDAX.SPLIT_DATA('intable=customer_churn, traintable=customer_churn_training, testtable=customer_churn_testing, id=id, fraction=0.65');
下列範例顯示如何定義離散化演算法及 bin 數目:CALL IDAX.NAIVEBAYES('intable=customer_churn_training, disc=ew, bins=20, id=id, target=churn, model=cc_nb_ewd');
CALL IDAX.NAIVEBAYES('intable=customer_churn_training, disc=ef, bins=20, id=id, target=churn, model=cc_nb_efd');
CALL IDAX.NAIVEBAYES('intable=customer_churn_training, disc=ewn, id=id, target=churn, model=cc_nb_ewnd'); 所建立的模型是以表格來表示,其中每一個屬性值類別組合各有一列,並且包含下列直欄:
- attribute
- 屬性
- val
- 值
- class
- 類別
- classvalcount
- 訓練集的類別內相對應的屬性的對應值的出現次數
- classcount
- 訓練集內的類別的出現次數
- attrclasscount
- 訓練集內的類別的非空值出現次數
- totalcount
- 訓練集內的所有實例數目
這些參數足以計算您可以用於 Naive Bayes 預測的先驗類別機率和條件式屬性值機率。
如果要產生測試集預測,您可以使用產生的模型,如下所示:
CALL IDAX.PREDICT_NAIVEBAYES('model=cc_nb_ewd, intable=customer_churn_testing, outtable=customer_churn_nb_ewd, outtableprob = customer_churn_nb_ewd_prob');
CALL IDAX.PREDICT_NAIVEBAYES('model=cc_nb_efd, intable=customer_churn_testing, outtable=customer_churn_nb_efd, outtableprob = customer_churn_nb_efd_prob');
CALL IDAX.PREDICT_NAIVEBAYES('model=cc_nb_ewnd, intable=customer_churn_testing, outtable=customer_churn_nb_ewnd,outtableprob = customer_churn_nb_ewnd_prob');會使用與對應訓練集相同的間隔,將每個模型應用程式的測試集離散化。指定的輸出表格包含產生的預測。它包含 id 直欄和 class 直欄。
此外,還會建立另一個輸出表格,其中 _prob 字尾附加至名稱。在此輸出表格中,每一個實例各有一列,它還包含可能的類別及下列直欄:
- id
- 實例 ID
- class
- 類別
- prob
- 實例類別的 Bayesian 分子
分子是從類別機率及條件式實例屬性值機率產生。
- lnprob
- prob 的自然對數
您可以使用這些機率,以取得模型預測的更佳見解,或修改模型作業。您可以透過正規化,將 Bayesian 分子轉換成正規表示式。正規化表示除以相同實例及所有類別的分子總和。
下列查詢顯示將類別機率新增至每一個實例的範例,及其使用最小熵離散化所產生之第三個模型的預測類別標籤:
SELECT S.id, S.class, S.prob/S.sump as prob
FROM (SELECT id, class, prob, sum(prob) OVER (PARTITION BY id) AS sump FROM customer_churn_nb_ewnd_prob) S, customer_churn_nb_ewnd P
WHERE S.id=P.id AND S.class=P.class; 若要評估所取得預測的品質,您可以計算錯誤分類誤差,如下所示:
CALL IDAX.CERROR('intable=customer_churn_nb_ewd, resulttable=customer_churn_testing, id=id, target=class, resulttarget=churn');
CALL IDAX.CERROR('intable=customer_churn_nb_efd, resulttable=customer_churn_testing, id=id, target=class, resulttarget=churn');
CALL IDAX.CERROR('intable=customer_churn_nb_ewnd, resulttable=customer_churn_testing, id=id, target=class, resulttarget=churn'); 雖然這些模型的行為類似,但第一個模型是根據相等頻率離散化,因此不像另外兩個那麼精確。此外,對於所有模型而言,錯誤分類誤差層次大於 0.37,得出的結論是 Naive Bayes 分類器不十分適合客戶流失資料集。
若要避免預測期間完全沒有取得機率,您可以啟用 m-estimation 技術。
如果要啟用這項技術,請對於 PREDICT_NAIVEBAYES 程序指定 mestimation=TRUE 引數,如下所示:
CALL IDAX.PREDICT_NAIVEBAYES('model=cc_nb_ewnd, intable=customer_churn_testing, mestimation=TRUE, outtable=customer_churn_nb_ewnd_mest');
CALL IDAX.CERROR('pred_table=customer_churn_nb_ewnd_mest, true_table=customer_churn_testing, pred_id=id, true_id=id, pred_column=class, true_column=churn'); 分析顯示此技術幾乎不影響對於 customer_customn 資料產生的預測品質。
若要執行深度預測品質分析,您可以使用混淆矩陣和衍生品質指標。對於第三個模型,請如下所示繼續進行:
CALL IDAX.CONFUSION_MATRIX('intable=customer_churn_testing, resulttable=customer_churn_nb_ewnd, id=id, target=churn, matrixTable=cc_churn_nb_ewnd_cm');
CALL IDAX.CMATRIX_STATS('matrixTable=cc_churn_nb_ewnd_cm'); 其結果使得對於客戶流失資料的 Naive Bayes 預測品質更加明朗化。大的錯誤分類誤差會轉換為將近 0.55 的高誤判率,但也會是將近 0.77 的超高真肯定率,其中客戶流失會被視為正面。
您可以依類型和角色定義所有直欄,如下列陳述式所示:
CALL IDAX.NAIVEBAYES('model=cc_nb1, intable=customer_churn_training, id=id, target=churn');此陳述式等於下列陳述式:
CALL IDAX.NAIVEBAYES('model=cc_nb2, intable=customer_churn_training, incolumn=id:id, target=churn');若要手動定義名義屬性及連續屬性,您可以使用 nom 類型及 cont 類型:
CALL IDAX.NAIVEBAYES('model=cc_nb3, intable=customer_churn_training,incolumn=IN_B2B_INDUSTRY:nom;TOTAL_BUY:cont;CHURN:target;ID:id');您也可以建立一個直欄的內容定義,並將它們傳遞至模型數次:
CALL IDAX.COLUMN_PROPERTIES('intable=customer_churn_training, outtable=cc_columns, coldeftype=cont, incolumn=IN_B2B_INDUSTRY:nom;TOTAL_BUY:cont;ANNUAL_REVENUE_MIL:cont:ignore;CHURN:nom:target;ID:id');
select * from cc_columns;
CALL IDAX.NAIVEBAYES('model=cc_nb4, intable=customer_churn_training,colPropertiesTable=cc_columns');