<jupyter_start><jupyter_text>Pre-inspection<jupyter_code>data_train.head()
data_train.describe()
data_train.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 1047 entries, 287 to 641
Data columns (total 14 columns):
 #   Column     Non-Null Count  Dtype   
---  ------     --------------  -----   
 0   pclass     1047 non-null   float64 
 1   survived   1047 non-null   category
 2   name       1047 non-null   object  
 3   sex        1047 non-null   category
 4   age        831 non-null    float64 
 5   sibsp      1047 non-null   float64 
 6   parch      1047 non-null   float64 
 7   ticket     1047 non-null   object  
 8   fare       1047 non-null   float64 
 9   cabin      241 non-null    object  
 10  embarked   1045 non-null   category
 11  boat       387 non-null    object  
 12  body       90 non-null     float64 
 13  home.dest  582 non-null    object  
dtypes: category(3), float64(6), object(5)
memory usage: 101.6+ KB<jupyter_text>Feature selction- Which features are chosen for modeling and why?  Feature preparation: Categorical Sex<jupyter_code># Plot
sns.displot(data=data_train, x="survived", hue="sex", multiple="dodge")
plt.show()
sex_imp = SimpleImputer(missing_values="", strategy='most_frequent')  # Replace missing values. Todo: Replace evering that is neither "female" nor "male" with a custom imputer
sex_enc = OrdinalEncoder()  # Numerical encoding / Ordinal encoding
sex_std = StandardScaler()  # Standardize

sex_preprocessor = Pipeline(steps=[
    ("sex_imp", sex_imp),
    ("sex_enc", sex_enc),
    ("sex_std", sex_std),
])
# Plot
sns.displot(data=data_train, x="boat", hue="survived", multiple="dodge")
# sns.displot(data=data_train[data_train['parch']>0], x="parch", hue="survived", multiple="dodge")
plt.show()<jupyter_output><empty_output><jupyter_text>Parch<jupyter_code># Plot
sns.displot(data=data_train, x="parch", hue="survived", multiple="dodge")
sns.displot(data=data_train[data_train['parch']>0], x="parch", hue="survived", multiple="dodge")
plt.show()
parch_imp = SimpleImputer(missing_values=np.nan, strategy='most_frequent')  # Replace missing values
parch_enc = OneHotEncoder()  # One hot encoding

parch_preprocessor = Pipeline(steps=[
    ("parch_imp", parch_imp),
    ("parch_enc", parch_enc),
])<jupyter_output><empty_output><jupyter_text>Sibsb<jupyter_code># Plot
sns.displot(data=data_train, x="sibsp", hue="survived", multiple="dodge")
sns.displot(data=data_train[data_train['sibsp']>0], x="sibsp", hue="survived", multiple="dodge")
plt.show()
sibsp_imp = SimpleImputer(missing_values=np.nan, strategy='most_frequent')  # Replace missing values
sibsp_enc = OneHotEncoder()  # One hot encoding

sibsp_preprocessor = Pipeline(steps=[
    ("sibsp_imp", sibsp_imp),
    ("sibsp_enc", sibsp_enc),
])<jupyter_output><empty_output><jupyter_text>Pclass<jupyter_code># Plot
sns.displot(data=data_train, x="survived", hue="pclass", multiple="dodge")
plt.show()
pclass_imp = SimpleImputer(missing_values=np.NaN, strategy='constant', fill_value=3)  # Replace missing values. Todo: Better use most frequent value?
pclass_std = StandardScaler()  # Standardize

pclass_preprocessor = Pipeline(steps=[
    ("pclass_imp", pclass_imp),
    ("pclass_std", pclass_std),
])<jupyter_output><empty_output><jupyter_text>ticket<jupyter_code>data_train['ticket'].value_counts()<jupyter_output><empty_output><jupyter_text>Feature preparation: Numerical Embarked<jupyter_code>sns.displot(data=data_train, x="survived", hue="embarked", multiple="dodge")
plt.show()
embarked_imp = SimpleImputer(missing_values=np.NaN, strategy='constant', fill_value='C')  # Replace missing values
embarked_enc = OrdinalEncoder()  # Numerical encoding
embarked_std = StandardScaler()  # Standardize

embarked_preprocessor = Pipeline(steps=[
    ("embarked_imp", embarked_imp),
    ("embarked_enc", embarked_enc),
    ("embarked_std", embarked_std),
])<jupyter_output><empty_output><jupyter_text>Age<jupyter_code>sns.displot(data=data_train, x="age", hue="survived", kind="kde")
plt.show()
age_imp = SimpleImputer(missing_values=np.nan, strategy='mean')  # Impute missing values. Todo: Clculate a mean per personal title and see how it improves the result.
age_std = StandardScaler()  # Standardize

age_preprocessor = Pipeline(steps=[
    ("age_imp", age_imp),
    ("age_std", age_std),
])<jupyter_output><empty_output><jupyter_text>Fare<jupyter_code>sns.displot(data=data_train, x="fare", hue="survived", kind="kde")
plt.show()
fare_imp = SimpleImputer(missing_values=np.nan, strategy='mean')  # todo: Use multivariate imputer
fare_std = StandardScaler()  # Standardize

fare_preprocessor = Pipeline(steps=[
    ("fare_imp", fare_imp),
    ("fare_std", fare_std),
])<jupyter_output><empty_output><jupyter_text>Feature preparation: Preprocessor pipeline<jupyter_code>preprocessor = ColumnTransformer(
    [
        ('sex_preprocessor', sex_preprocessor, ['sex',]),
        ('pclass_preprocessor', pclass_preprocessor, ['pclass',]),
        ('parch_preprocessor', parch_preprocessor, ['parch',]),
        ('sibsp_preprocessor', sibsp_preprocessor, ['sibsp',]),
        ('embarked_preprocessor', embarked_preprocessor, ['embarked',]),
        ('age_preprocessor', age_preprocessor, ['age',]),
        ('fare_preprocessor', fare_preprocessor, ['fare',]),
    ],
    remainder='drop'
)<jupyter_output><empty_output><jupyter_text>Modelling<jupyter_code>classifiers = {
        "Nearest Neighbors": KNeighborsClassifier(3),
        "Linear SVC": SVC(kernel="linear", C=0.025),
        "RBF SVC": SVC(gamma=2, C=1),
        "Gaussian Process": GaussianProcessClassifier(1.0 * RBF(1.0)),
        "Decision Tree": DecisionTreeClassifier(max_depth=5),
        "Random Forest": RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
        "Neural Net": MLPClassifier(alpha=1, max_iter=1000),
        "AdaBoost": AdaBoostClassifier(),
        "Naive Bayes": GaussianNB(),
        "QDA": QuadraticDiscriminantAnalysis(),
}
# Training and scoring
scores = {}
for clf in classifiers.keys():
    full_pipeline = Pipeline(steps=[
        ('preprocessor', preprocessor),
        ('model', classifiers[clf]),
    ])
    full_pipeline.fit(X_train, y_train)
    score = full_pipeline.score(X_test, y_test)
    scores[clf] = score

sorted_scores = sorted([(value, key) for (key,value) in scores.items()], reverse=True)
for s in sorted_scores:
    print(f"{s[1]}: {s[0]:.3f}")<jupyter_output>Gaussian Process: 0.824
Random Forest: 0.821
Neural Net: 0.821
Decision Tree: 0.813
Linear SVC: 0.798
AdaBoost: 0.794
Nearest Neighbors: 0.782
RBF SVC: 0.779
Perceptron: 0.698
QDA: 0.584
Naive Bayes: 0.408
{'fraction_non_alphanumeric': 0.14212432106216052, 'fraction_numerical': 0.0264031382015691, 'mean_word_length': 5.172253258845438, 'pattern_counts': {'":': 10, '<': 44, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '394264', 'license': 'MIT', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2348, 'n_words': 457, 'path': 'data_analytics/sample_datasets/titanic.ipynb', 'repo_name': 'croidzen/playground', 'size': '6628'}