<jupyter_start><jupyter_text>Load dataset<jupyter_code>test_df =  pd.read_csv("./test.csv")
train_df = pd.read_csv("./train.csv")
train_df.head(1)
test_df.head()
train_df.set_index('PassengerId', inplace=True)
test_df.set_index('PassengerId', inplace=True)

train_df.head()
test_df.head()
train_index = train_df.index
test_index = test_df.index
y_train_df = train_df.pop("Survived")
y_train_df.head()<jupyter_output><empty_output><jupyter_text>Data preproecessing<jupyter_code>pd.set_option('display.float_format', lambda x: '%.2f' % x)
test_df.head()
test_df.isnull().sum() / len(test_df)<jupyter_output><empty_output><jupyter_text>Decion 1 - Drop cabin<jupyter_code>del test_df["Cabin"]
del train_df["Cabin"]
all_df = train_df.append(test_df)
all_df.head()
(all_df.isnull().sum() / len(all_df)).plot(kind='bar')
plt.show()
len(all_df)
del all_df["Name"]
del all_df["Ticket"]
all_df.head()
all_df["Sex"] = all_df["Sex"].replace({"male": 0, "female": 1})
all_df.head()
all_df["Embarked"].unique()
all_df["Embarked"] = all_df["Embarked"].replace({"S":0, "C":1, "Q":2, np.nan:99})
all_df["Embarked"].unique()
all_df.head()
pd.get_dummies(all_df["Embarked"], prefix="embarked")
matrix_df = pd.merge(all_df, pd.get_dummies(all_df["Embarked"], prefix="embarked"), left_index=True, right_index=True)
matrix_df.head()
matrix_df.corr()
all_df.groupby("Pclass")["Age"].mean()
#all_df.loc[]
all_df.loc[
    (all_df["Pclass"]==1 & (all_df["Age"].isnull()), "Age")
]
# 실측값에 대해서 억지로 넣어줌
all_df.loc[(all_df["Pclass"] == 1) & (all_df["Age"].isnull()), "Age"] = 39.16
all_df.loc[(all_df["Pclass"] == 2) & all_df["Age"].isnull(), "Age"] = 29.51
all_df.loc[(all_df["Pclass"] == 3) & all_df["Age"].isnull(), "Age"] = 24.82
all_df.isnull().sum()
all_df.groupby("Pclass")["Fare"].mean()
all_df[all_df["Fare"].isnull()]
all_df.loc[all_df["Fare"].isnull(), "Fare"] = 13.30
del all_df["Embarked"]
all_df["Pclass"] = all_df["Pclass"].replace({1:"A", 2:"B", 3:"C"})
all_df = pd.get_dummies(all_df)
all_df.head()
all_df = pd.merge(
    all_df, matrix_df[["embarked_0", "embarked_1", "embarked_2", "embarked_99"]],
    left_index= True, right_index=True)
train_df = all_df[all_df.index.isin(train_index)]
test_df = all_df[all_df.index.isin(test_index)]
train_df.head(3)
test_df.head(3)<jupyter_output><empty_output><jupyter_text>Build Model<jupyter_code>x_data = train_df.values

y_data = y_train_df.values
y_data
from sklearn.linear_model import LogisticRegression
cls = LogisticRegression()
cls.fit(x_data, y_data)
cls.intercept_
cls.coef_
cls.predict(test_df.values)
test_df.index
x_test = test_df.as_matrix()
y_test =cls.predict(x_test)
y_test 
result = np.concatenate((test_index.values.reshape(-1,1), cls.predict(x_test).reshape(-1,1)), axis=1)
result[:5]
df_submssion = pd.DataFrame(result, columns=["PassengerId", "Servived"])
df_submssion
df_submssion.to_csv("submission_result.csv",index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2002752924982794, 'fraction_numerical': 0.015829318651066758, 'mean_word_length': 8.347266881028938, 'pattern_counts': {'":': 5, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '154821', 'license': 'MIT', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1273, 'n_words': 159, 'path': '88.kaggle/titanic/titanic.ipynb', 'repo_name': 'LenKIM/Show-me-the-data-science', 'size': '2906'}