<jupyter_start><jupyter_text>Load Dataset<jupyter_code># train
train = pd.read_csv("/Users/dennyoh/Documents/datascience/titanic_ML_kaggle/train.csv", index_col="PassengerId")

print(train.shape)
train.head()
# test
test = pd.read_csv("/Users/dennyoh/Documents/datascience/titanic_ML_kaggle/test.csv", index_col="PassengerId")

print(test.shape)
test.head()<jupyter_output>(418, 10)<jupyter_text>Preprocessing  1.Encode Sex(숫자데이터로 바꿔줘야함)<jupyter_code># train
train["Sex_encode"] = train["Sex"].replace("male",0).replace("female",1)

print(train.shape)

sex_encode = ["Sex", "Sex_encode"]
train[sex_encode].head()
# test
test["Sex_encode"] = test["Sex"].replace("male",0).replace("female",1)

print(test.shape)

sex_encode = ["Sex", "Sex_encode"]
test[sex_encode].head()<jupyter_output>(418, 11)<jupyter_text>2.Fill in missing Fare(빈값이 있으면 안됨)<jupyter_code># train
train[train["Fare"].isnull()]
# test
test["Fare"] = test["Fare"].fillna(0)

test[test["Fare"].isnull()]<jupyter_output><empty_output><jupyter_text>3.Encode Embarked<jupyter_code># train
# C == 0, S == 1, Q == 2
# 2 + C = Q?
# 2 * Q = Q?

# 글자 데이터가 3개 이상일때는 
# one hot encodint
# True == 1, False == 0
# C == [1,0,0]
# S == [0,1,0]
# Q == [0,0,1]

train["Embarked_C"] = train["Embarked"] == "C"
train["Embarked_S"] = train["Embarked"] == "S"
train["Embarked_Q"] = train["Embarked"] == "Q"

print(train.shape)
train[["Embarked","Embarked_C","Embarked_S","Embarked_Q"]].head()
# test
test["Embarked_C"] = test["Embarked"] == "C"
test["Embarked_S"] = test["Embarked"] == "S"
test["Embarked_Q"] = test["Embarked"] == "Q"

print(test.shape)
test[["Embarked","Embarked_C","Embarked_S","Embarked_Q"]].head()<jupyter_output>(418, 14)<jupyter_text>4.AgeNaN<jupyter_code># train
train["AgeNaN"] = train["Age"].isnull() == False
train[["Age","AgeNaN"]].head()
# test
test["AgeNaN"] = test["Age"].isnull() == False
test[["Age","AgeNaN"]].head()
pd.pivot_table(train, index="AgeNaN", values="Survived")<jupyter_output><empty_output><jupyter_text>5. range Age<jupyter_code># train
train["Age(fill)"] = train["Age"]

train.loc[(train["Pclass"] == 1) & (train["Age"].isnull()), "Age(fill)"] = 38.233441
train.loc[(train["Pclass"] == 2) & (train["Age"].isnull()), "Age(fill)"] = 29.877630
train.loc[(train["Pclass"] == 3) & (train["Age"].isnull()), "Age(fill)"] = 25.140620

agefill_columns = ["Pclass","Age","Age(fill)"]

train[agefill_columns].head()
train.loc[train["Age(fill)"] < 15, "AgeType"] ="Young"
train.loc[(15 <= train["Age(fill)"]) & (train["Age(fill)"] < 30), "AgeType"] ="Medium"
train.loc[train["Age(fill)"] >= 30, "AgeType"] ="Old"

age_columns = ["Age","Age(fill)","AgeType"]

train[age_columns].head()
train["AgeType_Young"] = train["AgeType"] == "Young"
train["AgeType_Medium"] = train["AgeType"] == "Medium"
train["AgeType_Old"] = train["AgeType"] == "Old"

age_columns = ["Age","Age(fill)","AgeType","AgeType_Young","AgeType_Medium","AgeType_Old"]

train[age_columns].head()
# test
test["Age(fill)"] = test["Age"]

test.loc[(test["Pclass"] == 1) & (test["Age"].isnull()), "Age(fill)"] = 38.233441
test.loc[(test["Pclass"] == 2) & (test["Age"].isnull()), "Age(fill)"] = 29.877630
test.loc[(test["Pclass"] == 3) & (test["Age"].isnull()), "Age(fill)"] = 25.140620

agefill_columns = ["Pclass","Age","Age(fill)"]

test[agefill_columns].head()
test.loc[test["Age(fill)"] < 15, "AgeType"] ="Young"
test.loc[(15 <= test["Age(fill)"]) & (test["Age(fill)"] < 30), "AgeType"] ="Medium"
test.loc[test["Age(fill)"] >= 30, "AgeType"] ="Old"

age_columns = ["Age","Age(fill)","AgeType"]

test[age_columns].head()
test["AgeType_Young"] = test["AgeType"] == "Young"
test["AgeType_Medium"] = test["AgeType"] == "Medium"
test["AgeType_Old"] = test["AgeType"] == "Old"

age_columns = ["Age","Age(fill)","AgeType","AgeType_Young","AgeType_Medium","AgeType_Old"]

test[age_columns].head()<jupyter_output><empty_output><jupyter_text>6.range FamilySize<jupyter_code># train
train["FamilySize"] = train["SibSp"].add(train["Parch"]) + 1

family_columns = ["SibSp","Parch","FamilySize"]

train[family_columns].head()
train.loc[train["FamilySize"] == 1 , "FamilyType"] = "Single"
train.loc[(2 <= train["FamilySize"]) & (train["FamilySize"] < 5), "FamilyType"] = "Nuclear"
train.loc[train["FamilySize"] >= 5, "FamilyType"] = "Big"

familytype_columns = ["FamilySize","FamilyType"]

train[familytype_columns].head(10)
#train = train.drop(["FamilySize_Single","FamilySize_Nuclear","FamilySize_Big"], axis="columns")
#print(train.shape)
#train.head()
train["FamilyType_Single"] = train["FamilyType"] == "Single"
train["FamilyType_Nuclear"] = train["FamilyType"] == "Nuclear"
train["FamilyType_Big"] = train["FamilyType"] == "Big"

age_columns = ["FamilyType","FamilyType_Single","FamilyType_Nuclear","FamilyType_Big"]

train[age_columns].head()
# test
test["FamilySize"] = test["SibSp"].add(test["Parch"]) + 1

family_columns = ["SibSp","Parch","FamilySize"]

test[family_columns].head()
test.loc[test["FamilySize"] == 1 , "FamilyType"] = "Single"
test.loc[(2 <= test["FamilySize"]) & (test["FamilySize"] < 5), "FamilyType"] = "Nuclear"
test.loc[test["FamilySize"] >= 5, "FamilyType"] = "Big"

familytype_columns = ["FamilySize","FamilyType"]

test[familytype_columns].head(10)
test["FamilyType_Single"] = test["FamilyType"] == "Single"
test["FamilyType_Nuclear"] = test["FamilyType"] == "Nuclear"
test["FamilyType_Big"] = test["FamilyType"] == "Big"

age_columns = ["FamilyType","FamilyType_Single","FamilyType_Nuclear","FamilyType_Big"]

test[age_columns].head()<jupyter_output><empty_output><jupyter_text>Train<jupyter_code># Feature - Pclass, Sex, Fare ...
# Lable - Survived
feature_names = ["Pclass", "Sex_encode", "Fare", "Embarked_C","Embarked_S","Embarked_Q"
                 , "AgeNaN", "AgeType_Young","AgeType_Medium","AgeType_Old"
                 , "FamilyType_Single", "FamilyType_Nuclear", "FamilyType_Big"]
feature_names
x_train = train[feature_names]

print(x_train.shape)
x_train.head()
x_test = test[feature_names]

print(x_test.shape)
x_test.head()
label_name = "Survived"

y_train = train[label_name]

print(y_train.shape)
y_train.head()<jupyter_output>(891,)<jupyter_text>Use Decision Tree<jupyter_code># scikit-learn == sklearn
from sklearn.tree import DecisionTreeClassifier

model = DecisionTreeClassifier(max_depth = 9)
model
# 1) fit(train) -> 2) predict(test)
model.fit(x_train,y_train)
prediction = model.predict(x_test)

print(prediction.shape)
prediction[0:5]<jupyter_output>(418,)<jupyter_text>Submit<jupyter_code>submit = pd.read_csv("/Users/dennyoh/Documents/datascience/titanic_ML_kaggle/gender_submission.csv"
                   , index_col="PassengerId")

submit["Survived"] = prediction

print(submit.shape)
submit.head()
# 저장
submit.to_csv("decisiontree.csv")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22169671888856046, 'fraction_numerical': 0.02039609813774756, 'mean_word_length': 8.181818181818182, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '247075', 'license': 'MIT', 'n_tokens_mistral': 2839, 'n_tokens_neox': 2640, 'n_words': 354, 'path': '6-1.TML_with_py(assignmet).ipynb', 'repo_name': 'dennyoh/kaggle-Titanic-Machine-Learning-from-Disaster', 'size': '6766'}