<jupyter_start><jupyter_text>주어진 사용자가 어떤 쿠폰을 구입할 지 맞추는 문제사용자에 대해 알 수 있는 정보- 가입일- 해지일- 성별- 나이- 사는 곳- 과거 구매한 쿠폰- 방문(검색)했던 쿠폰<jupyter_code>coupon = pd.read_csv("../data/coupon_list_train.csv",index_col=0)
visit = pd.read_csv("../data/coupon_visit_train.csv")
user = pd.read_csv("../data/user_list.csv",index_col=0)
len(coupon)
VIEW_COUPON_ID_hash = list(set(visit['VIEW_COUPON_ID_hash']))
print('visit테이블의 row수 : ' ,len(visit))
print('visit테이블의 unique한 COUPON_ID 갯수 : ' ,len(VIEW_COUPON_ID_hash))
purchase = pd.read_csv('../data/coupon_detail_train.csv',index_col=0)
Pur_COUPON_ID_hash = list(set(list(purchase['COUPON_ID_hash'])))

print('purchase테이블의 row수 : ' ,len(purchase))
print('purchase테이블의 unique한 COUPON_ID 갯수 : ' ,len(Pur_COUPON_ID_hash))
print("샀지만 visit테이블에 없는 COUPON_ID :")

for ID in Pur_COUPON_ID_hash:
    if ID not in VIEW_COUPON_ID_hash:
        print(ID)
c=0
for ID in list(purchase['COUPON_ID_hash']):
    if ID == '2c27645cf493b4188045f7ebae28e08e':
        c+=1
        
print('샀지만 visit테이블에 없는 COUPON_ID의 갯수 : ',c)
print("visit테이블에 있지만 안 산 COUPON_ID의 유니크한 갯 수 :")
c=0
for ID in VIEW_COUPON_ID_hash:
    if ID not in Pur_COUPON_ID_hash:
        c+=1
c
# print("visit테이블에 있지만 안 산 COUPON_ID의 갯수 :")
# c=0
# for ID in list(visit['VIEW_COUPON_ID_hash']):
#     if ID not in Pur_COUPON_ID_hash:
#         c+=1
# c
pd.set_option('display.max_columns', None)
train1 = pd.merge(visit, coupon, left_on="VIEW_COUPON_ID_hash", right_on="COUPON_ID_hash")
train1 = pd.merge(train1, user, left_on="USER_ID_hash", right_on="USER_ID_hash")
train1.tail()
train1 = train1.drop(['I_DATE', 'PAGE_SERIAL', 'REFERRER_hash',
       'VIEW_COUPON_ID_hash', 'SESSION_ID_hash',
       'PURCHASEID_hash', 'PRICE_RATE',
       'CATALOG_PRICE', 'DISCOUNT_PRICE', 'DISPFROM','DISPEND',
       'VALIDFROM','VALIDEND', 'USABLE_DATE_MON',
       'USABLE_DATE_TUE', 'USABLE_DATE_WED', 'USABLE_DATE_THU',
       'USABLE_DATE_FRI', 'USABLE_DATE_SAT', 'USABLE_DATE_SUN',
       'USABLE_DATE_HOLIDAY', 'USABLE_DATE_BEFORE_HOLIDAY', 'REG_DATE', 'WITHDRAW_DATE',],axis=1)
train1.tail()

# userID = '<KEY>'
# tmp = train1.copy()
# train2 = tmp[tmp['USER_ID_hash'] == userID]

train1 = train1.drop(['USER_ID_hash'],axis=1)
train1
train_y = train1['PURCHASE_FLG']
train_y
train1.columns
category = ['CAPSULE_TEXT', 'GENRE_NAME', 'large_area_name', 'ken_name', 'small_area_name',
       'COUPON_ID_hash', 'SEX_ID' ,'PREF_NAME']
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import LabelEncoder

label_encoder = LabelEncoder()
integer_encoded1 = label_encoder.fit_transform(train1['CAPSULE_TEXT'])
integer_encoded1 = integer_encoded1.reshape(len(integer_encoded1),1)
integer_encoded1
label_encoder = LabelEncoder()
integer_encoded2 = label_encoder.fit_transform(train1['GENRE_NAME'])
integer_encoded2 = integer_encoded2.reshape(len(integer_encoded1),1)
integer_encoded2
label_encoder = LabelEncoder()
integer_encoded3 = label_encoder.fit_transform(train1['large_area_name'])
integer_encoded3 = integer_encoded3.reshape(len(integer_encoded1),1)
integer_encoded3
label_encoder = LabelEncoder()
integer_encoded4 = label_encoder.fit_transform(train1['ken_name'])
integer_encoded4 = integer_encoded4.reshape(len(integer_encoded1),1)
integer_encoded4
label_encoder =LabelEncoder()
integer_encoded5 = label_encoder.fit_transform(train1['small_area_name'])
integer_encoded5 = integer_encoded5.reshape(len(integer_encoded1),1)
integer_encoded5
label_encoder =LabelEncoder()
integer_encoded6 = label_encoder.fit_transform(train1['SEX_ID'])
integer_encoded6 = integer_encoded6.reshape(len(integer_encoded1),1)
integer_encoded6
numeric1 = np.array(train1['DISPPERIOD'])
numeric1 = numeric1.reshape(len(integer_encoded1),1)
numeric1[np.isnan(numeric1)] = 0
numeric1
numeric2 = np.array(train1['VALIDPERIOD'])
numeric2 = numeric2.reshape(len(integer_encoded1),1)
numeric2[np.isnan(numeric2)] = 0
numeric2

numeric3 = np.array(train1['AGE'])
numeric3 = numeric3.reshape(len(integer_encoded1),1)
numeric3[np.isnan(numeric3)] = 0
numeric3
train_x = np.concatenate([integer_encoded1, integer_encoded2, integer_encoded3, numeric1, numeric2,\
                          integer_encoded4, integer_encoded5, integer_encoded6,numeric3], axis=1)
train_x
from sklearn.tree import DecisionTreeClassifier

tree = DecisionTreeClassifier(criterion='entropy', max_depth=3, random_state=0)
tree.fit(train_x, train_y)
import io
import pydot
from IPython.core.display import Image 
from sklearn.tree import export_graphviz

def draw_decision_tree(model):
    dot_buf = io.StringIO() 
    export_graphviz(model, out_file=dot_buf)
    graph = pydot.graph_from_dot_data(dot_buf.getvalue())[0] 
    image = graph.create_png()
    return Image(image)

draw_decision_tree(tree)
print("훈련 세트 정확도: {:.3f}".format(tree.score(train_x, train_y)))
print("특성 중요도:\n{}".format(tree.feature_importances_))

from sklearn.metrics import confusion_matrix
confusion_matrix(train_y, tree.predict(train_x))
train_y
from sklearn.ensemble import RandomForestClassifier
forest = RandomForestClassifier(n_estimators=10, random_state=2)
forest.fit(train_x, train_y)
print("훈련 세트 정확도: {:.3f}".format(forest.score(train_x, train_y)))
print("특성 중요도:\n{}".format(forest.feature_importances_))

confusion_matrix(train_y, forest.predict(train_x))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12857680722891565, 'fraction_numerical': 0.025602409638554216, 'mean_word_length': 7.025679758308157, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '268797', 'license': 'MIT', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2245, 'n_words': 328, 'path': 'js/tree.ipynb', 'repo_name': 'JoonSeongLee/dss7-coupon', 'size': '5339'}