Dự đoán rời bỏ bằng cây quyết định

Machine Learning cho Marketing với Python

Karolis Urbonas

Head of Analytics & Science, Amazon

Giới thiệu về cây quyết định

Quy tắc Cây quyết định trên bộ Titanic Survival

Machine Learning cho Marketing với Python

Các bước mô hình hóa

  1. Chia dữ liệu thành train và test
  2. Khởi tạo mô hình
  3. Huấn luyện trên dữ liệu train
  4. Dự đoán trên dữ liệu test
  5. Đánh giá hiệu suất trên dữ liệu test
Machine Learning cho Marketing với Python

Huấn luyện mô hình

Import module cây quyết định

from sklearn.tree import DecisionTreeClassifier

Khởi tạo mô hình Cây quyết định

mytree = DecisionTreeClassifier()

Huấn luyện trên dữ liệu train

treemodel = mytree.fit(train_X, train_Y)
Machine Learning cho Marketing với Python

Đo độ chính xác (accuracy)

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 cho Marketing với Python

Đo precision và 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 cho Marketing với Python

Tinh chỉnh tham số độ sâu cây

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 cho Marketing với Python

Chọn độ sâu tối ưu

Tinh chỉnh Max Depth

Machine Learning cho Marketing với Python

Chọn độ sâu tối ưu

Tinh chỉnh Max Depth

Machine Learning cho Marketing với Python

Hãy xây dựng một cây quyết định!

Machine Learning cho Marketing với Python

Preparing Video For Download...