<jupyter_start><jupyter_text>Decision tree algorithmเป็นการนำข้อมูลมาทำเป็นต้นไม้ตัดสินใจ เพื่อคาดการณ์ทิศทางของผู้ใช้งานว่าจะเป็นผู้ใช้งานรูปแบบใด เริ่มจากการ import ข้อมูลเข้ามา<jupyter_code>import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn import tree
import category_encoders as ce
import matplotlib.pyplot as plt
import graphviz

%matplotlib inline<jupyter_output><empty_output><jupyter_text>ทำการ load ข้อมูล โดยที่จะทำการ format rows ที่เป็น yes no ให้เป็น 0 และ 1<jupyter_code>column_names = [
  'branch',
  'city',
  'customer_type',
  'gender',
  'health_and_beauty',
  'electronic_accessories',
  'home_and_lifestyle',
  'sports_and_travel',
  'food_and_beverages',
  'fashion_accessories',
  'unit_price',
  'quantity',
  'tax',
  'total',
  'payment',
  'cogs',
  'rating'
]

df = pd.read_csv('./attribute1.csv', header=None,
                 skiprows=[0], names=column_names)

df[['health_and_beauty', 'electronic_accessories', 'home_and_lifestyle', 'sports_and_travel', 'food_and_beverages', 'fashion_accessories']] = df[['health_and_beauty', 'electronic_accessories', 'home_and_lifestyle', 'sports_and_travel', 'food_and_beverages', 'fashion_accessories']].eq('Yes').astype(int)

print(df.head())<jupyter_output>branch       city customer_type  gender  health_and_beauty  \
0      A     Yangon        Member  Female                  1   
1      C  Naypyitaw        Normal  Female                  0   
2      A     Yangon        Normal    Male                  0   
3      A     Yangon        Member    Male                  1   
4      A     Yangon        Normal    Male                  0   

   electronic_accessories  home_and_lifestyle  sports_and_travel  \
0                       0                   0                  0   
1                       1                   0                  0   
2                       0                   1                  0   
3                       0                   0                  0   
4                       0                   0                  1   

   food_and_beverages  fashion_accessories  unit_price  quantity  tax  total  \
0                   0                    0          32         7   13    237   
1                   0                    0    [...]<jupyter_text>ตรวจสอบจำนวนข้อมูลในแต่ละคอลัมน์<jupyter_code>for col in column_names:
  print(df[col].value_counts())<jupyter_output>A    340
B    332
C    328
Name: branch, dtype: int64
Yangon       340
Mandalay     332
Naypyitaw    328
Name: city, dtype: int64
Member    501
Normal    499
Name: customer_type, dtype: int64
Female    501
Male      499
Name: gender, dtype: int64
0    848
1    152
Name: health_and_beauty, dtype: int64
0    830
1    170
Name: electronic_accessories, dtype: int64
0    840
1    160
Name: home_and_lifestyle, dtype: int64
0    834
1    166
Name: sports_and_travel, dtype: int64
0    826
1    174
Name: food_and_beverages, dtype: int64
0    822
1    178
Name: fashion_accessories, dtype: int64
83    22
44    22
32    21
79    18
26    18
      ..
84     7
55     7
71     6
73     6
81     5
Name: unit_price, Length: 81, dtype: int64
10    119
1     112
4     109
7     102
5     102
6      98
9      92
2      91
3      90
8      85
Name: quantity, dtype: int64
13    113
20    102
11    101
10     94
12     91
18     89
15     85
17     84
19     84
16     79
14     78
Name: tax, dtype: int64
280[...]<jupyter_text>ทำการทำ train test split ของข้อมูล<jupyter_code>x = df.drop(['rating'], axis=1, inplace=False)
y = df['rating']

x_train, x_test, y_train, y_test = train_test_split(
    x, y, test_size=0.3, random_state=112233)<jupyter_output><empty_output><jupyter_text>ทำการ encode categorical data ให้เป็น number<jupyter_code>encoder = ce.OrdinalEncoder(
    cols=['branch', 'city', 'customer_type', 'gender', 'payment'])

x_train = encoder.fit_transform(x_train)
x_test = encoder.transform(x_test)

print(x_train.head())<jupyter_output>branch  city  customer_type  gender  health_and_beauty  \
399       1     1              1       1                  0   
995       2     2              2       1                  1   
808       1     1              2       2                  1   
936       2     2              2       2                  1   
476       3     3              2       2                  0   

     electronic_accessories  home_and_lifestyle  sports_and_travel  \
399                       1                   0                  0   
995                       0                   0                  0   
808                       0                   0                  0   
936                       0                   0                  0   
476                       0                   0                  1   

     food_and_beverages  fashion_accessories  unit_price  quantity  tax  \
399                   0                    0          44         9   12   
995                   0                    0      [...]<jupyter_text>สร้าง Decision tree จาก Entropy (information gain) method<jupyter_code>clf_entropy = DecisionTreeClassifier(
    criterion='entropy', max_depth=3, random_state=112233)

clf_entropy.fit(x_train, y_train)<jupyter_output><empty_output><jupyter_text>ทำการวัดผลโมเดล<jupyter_code>y_predicted_entropy = clf_entropy.predict(x_test)<jupyter_output><empty_output><jupyter_text>ดูผลลัพธ์ของการวัดผลสำหรับ test set<jupyter_code>print('Model accuracy score with criterion entropy index: {0:0.4f}'. format(
    accuracy_score(y_test, y_predicted_entropy)))

y_predicted_train_entropy = clf_entropy.predict(x_train)

print(y_predicted_train_entropy)<jupyter_output>['medium' 'medium' 'medium' 'medium' 'high' 'medium' 'medium' 'medium'
 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium'
 'medium' 'medium' 'high' 'medium' 'high' 'medium' 'medium' 'medium'
 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium'
 'medium' 'high' 'high' 'high' 'medium' 'medium' 'medium' 'medium'
 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium'
 'high' 'medium' 'medium' 'medium' 'medium' 'medium' 'high' 'medium'
 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium'
 'medium' 'medium' 'medium' 'high' 'medium' 'medium' 'medium' 'medium'
 'medium' 'medium' 'high' 'medium' 'medium' 'medium' 'medium' 'medium'
 'medium' 'high' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium'
 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium'
 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'medium' 'high'
 'medium' 'medium' 'high' 'high' 'medium' 'medium' 'medium' 'medium'
 'medi[...]<jupyter_text>ดูผลลัพธ์ของการวัดผลของ train set<jupyter_code>print('Training-set accuracy score: {0:0.4f}'. format(
    accuracy_score(y_train, y_predicted_train_entropy)))

print('Training set score: {:.4f}'.format(clf_entropy.score(x_train, y_train)))

print('Test set score: {:.4f}'.format(clf_entropy.score(x_test, y_test)))<jupyter_output>Training set score: 0.5129
Test set score: 0.4500<jupyter_text>ทำการ plot tree จาก train model<jupyter_code>plt.figure(figsize=(12, 8))

tree.plot_tree(clf_entropy.fit(x_train, y_train))<jupyter_output><empty_output><jupyter_text>ทำการนำกราฟไป plot ผ่าน graphviz<jupyter_code>dot_data = tree.export_graphviz(clf_entropy, out_file=None, feature_names=x_train.columns, class_names=y_train.reset_index(drop=True), filled=True, rounded=True, special_characters=True)

graph = graphviz.Source(dot_data)

graph<jupyter_output><empty_output><jupyter_text>ทำการคิด confusion matrix ออกมาแต่ละ column มีค่า True Positive, True Negative, False Positive, False Negative<jupyter_code>cm = confusion_matrix(y_test, y_predicted_entropy)

print('Confusion matrix\n\n', cm)<jupyter_output>Confusion matrix

 [[ 16   2 108]
 [  4   0  22]
 [ 26   3 119]]<jupyter_text>ทำการ generate classification report<jupyter_code>print(classification_report(y_test, y_predicted_entropy))<jupyter_output>precision    recall  f1-score   support

        high       0.35      0.13      0.19       126
         low       0.00      0.00      0.00        26
      medium       0.48      0.80      0.60       148

    accuracy                           0.45       300
   macro avg       0.28      0.31      0.26       300
weighted avg       0.38      0.45      0.37       300
{'fraction_non_alphanumeric': 0.09813694078557018, 'fraction_numerical': 0.056841105968909456, 'mean_word_length': 2.2781019058732013, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '624994', 'license': 'MIT', 'n_tokens_mistral': 3276, 'n_tokens_neox': 3048, 'n_words': 683, 'path': 'supermarket_sales.ipynb', 'repo_name': 'grindarius/ai', 'size': '8427'}