建立 KNN 模型的範例

此範例顯示如何在 CUSTOMER_CHURN 範例資料集上建置 KNN 模型。

首先,您會根據 CUSTOMER_CHURN 表格建立 CUSTOMER_CHURN_VIEW 範例資料集,如下所示:

CREATE VIEW CUSTOMER_CHURN_VIEW AS (SELECT CUST_ID, DURATION, CASE WHEN CENSOR=1 THEN 'yes' ELSE 'no' END AS CHURN,
AVG_SPENT_RETAIN_PM, AVG_SQ_SPENT_RETAIN_PM IN_B2B_INDUSTRY,  ANNUAL_REVENUE_MIL TOTAL_EMPLOYEES, 
TOTAL_BUY TOTAL_BUY_FREQ, TOTAL_BUY_FREQ_SQ
FROM CUSTOMER_CHURN);

然後,您可以將 CUSTOMER_CHURN_VIEW 範例資料集分割為訓練資料集和驗證資料集,如下所示:

CALL IDAX.SPLIT_DATA('intable=customer_churn_view, traintable=customer_churn_train, 
testtable=customer_churn_test, id=cust_id, fraction=0.35');

下列呼叫會對 customer_churn_train 資料集執行演算法,並建置 KNN 模型。

CALL IDAX.KNN('model=customer_churn_mdl, intable=customer_churn_train, id=cust_id, target=churn');

PREDICT_KNN 儲存程序預測 CHURN 直欄的值。

下列呼叫顯示如何使值與新交易相關聯。

CALL IDAX.PREDICT_KNN('model= customer_churn_mdl, intable= customer_churn_test, outtable=customer_churn_score');

您可以比較未用來建置 KNN 模型 customer_churn_mdl 的 CUSTOMER_CHURN_TEST 資料集記錄中的流失值 (churn) 與 customer_churn_score 的預測,來驗證前一個步驟的預測:

SELECT s.id, s.class churn_predicted, churn from customer_churn_test i, customer_churn_score s where i.cust_id=s.id;