พยากรณ์การเลิกใช้บริการด้วย decision tree

Machine Learning สำหรับการตลาดด้วย Python

Karolis Urbonas

Head of Analytics & Science, Amazon

แนะนำ decision tree

กฎของ Decision Tree บนชุดข้อมูลการรอดชีวิตบน Titanic

Machine Learning สำหรับการตลาดด้วย Python

ขั้นตอนการสร้างโมเดล

  1. แบ่งข้อมูลเป็นชุด training และ testing
  2. กำหนดโมเดล
  3. Fit โมเดลด้วยข้อมูล training
  4. พยากรณ์ค่าจากข้อมูล testing
  5. วัดประสิทธิภาพโมเดลบนข้อมูล testing
Machine Learning สำหรับการตลาดด้วย Python

การ fit โมเดล

นำเข้าโมดูล decision tree

from sklearn.tree import DecisionTreeClassifier

กำหนดโมเดล Decision Tree

mytree = DecisionTreeClassifier()

Fit โมเดลด้วยข้อมูล training

treemodel = mytree.fit(train_X, train_Y)
Machine Learning สำหรับการตลาดด้วย Python

การวัดความแม่นยำของโมเดล

from sklearn.metrics import accuracy_score

pred_train_Y = mytree.predict(train_X) pred_test_Y = mytree.predict(test_X)
train_accuracy = accuracy_score(train_Y, pred_train_Y) test_accuracy = accuracy_score(test_Y, pred_test_Y)
print('Training accuracy:', round(train_accuracy,4)) print('Test accuracy:', round(test_accuracy, 4))
Training accuracy: 0.9973
Test accuracy: 0.7196
Machine Learning สำหรับการตลาดด้วย Python

การวัด precision และ recall

from sklearn.metrics import precision_score, recall_score

train_precision = round(precision_score(train_Y, pred_train_Y), 4) test_precision = round(precision_score(test_Y, pred_test_Y), 4)
train_recall = round(recall_score(train_Y, pred_train_Y), 4) test_recall = round(recall_score(test_Y, pred_test_Y), 4)
print('Training precision: {}, Training recall: {}'.format(train_precision, train_recall)) print('Test precision: {}, Test recall: {}'.format(train_recall, test_recall))
Training precision: 0.9993, Training recall: 0.9906
Test precision: 0.9906, Test recall: 0.4878
Machine Learning สำหรับการตลาดด้วย Python

การปรับค่าพารามิเตอร์ความลึกของ tree

depth_list = list(range(2,15))
depth_tuning = np.zeros((len(depth_list), 4))
depth_tuning[:,0] = depth_list

for index in range(len(depth_list)): mytree = DecisionTreeClassifier(max_depth=depth_list[index]) mytree.fit(train_X, train_Y) pred_test_Y = mytree.predict(test_X)
depth_tuning[index,1] = accuracy_score(test_Y, pred_test_Y) depth_tuning[index,2] = precision_score(test_Y, pred_test_Y) depth_tuning[index,3] = recall_score(test_Y, pred_test_Y)
col_names = ['Max_Depth','Accuracy','Precision','Recall'] print(pd.DataFrame(depth_tuning, columns=col_names))
Machine Learning สำหรับการตลาดด้วย Python

เลือกความลึกที่เหมาะสม

การปรับค่า Max Depth

Machine Learning สำหรับการตลาดด้วย Python

เลือกความลึกที่เหมาะสม

การปรับค่า Max Depth

Machine Learning สำหรับการตลาดด้วย Python

มาสร้าง decision tree กันเถอะ!

Machine Learning สำหรับการตลาดด้วย Python

Preparing Video For Download...