<jupyter_start><jupyter_text>Exploring Embarked, Pclass and Fare<jupyter_code>for city in df["Embarked"].unique():
    v = df[df["Embarked"] == city][["Pclass", "Fare"]]
    for pclass in v["Pclass"].unique():
        p = v[v["Pclass"] == pclass]
        fmedian = p["Fare"].median()
        print(city, pclass, fmedian)
# import libraries and load in dataset
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.datasets import make_moons
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score


Xm, ym = make_moons(n_samples=10000, noise=0.4, random_state=42)

Xm
X_train, X_test, y_train, y_test = train_test_split(Xm, ym, test_size=0.2, shuffle=True)

# parameters to be tested - Iterate several times to find better ranges
parameters = {
    'min_samples_split': [20, 25, 30],
    'min_samples_leaf': [5, 8, 10],
    'max_leaf_nodes': [18, 19],
    'max_features': [2]
}

# instantiate model
model = DecisionTreeClassifier(random_state=42)

# grid search using cv
clf = GridSearchCV(model, parameters)
clf.fit(X_train, y_train)

# Output [17]: GridSearchCV(estimator=DecisionTreeClassifier(random_state=42),
             # param_grid={'max_features': [2], 'max_leaf_nodes': [18, 19],
                         # 'min_samples_leaf': [5, 8, 10],
                         # 'min_samples_split': [20, 25, 30]})
            
clf.best_estimator_
# Output [24]: DecisionTreeClassifier(max_features=2, max_leaf_nodes=18, min_samples_leaf=8, 
                                    # min_samples_split=20, random_state=42)
rs = ShuffleSplit(n_splits=1000, train_size=0.0125, random_state=42)
rs.get_n_splits(X_train)
print(rs)

X_subsets = dict()
y_subsets = dict()
for i, indexes in enumerate(rs.split(X_train)):
    X_subsets[i] = X_train[indexes[0], :]
    y_subsets[i] = y_train[indexes[0]]
    
# some checks
assert X_subsets[0].shape == (100, 2), ("Dimensional Error.")
assert y_subsets[0].shape == (100,), ("Dimensional Error.")
assert y_subsets[100].shape[0] == X_subsets[100].shape[0], ("Dimensional mismatch")
assert len(X_subsets.keys()) == 1000, ("The number of subsets is not 1000.")
assert len(y_subsets.keys()) == 1000, ("The number of subsets is not 1000.")
clf = DecisionTreeClassifier(max_features=2, max_leaf_nodes=19, min_samples_leaf=5,
                             min_samples_split=20, random_state=42)
scores = []
for subset in range(0, 1000):
    #fit model
    clf.fit(X_subsets[subset], y_subsets[subset])
    # make predictions
    y_pred = clf.predict(X_test)
    # evaluate accuracy and store
    scores.append(accuracy_score(y_test, y_pred))


print("Max accuracy: {:.2f}%.".format(max(scores) * 100))
print("Min accuracy: {:.2f}%.".format(min(scores) * 100))
print("Average accuracy: {:.2f}%.".format(np.mean(scores) * 100))
print("Std of accuracy: {:.2f}.".format(np.std(scores) * 100))
for tree in scores:
    print(clf.score(X_test, y_test))
y_pred = []
for instance in range(0, X_test.shape[0]):
    predictions = []
    # loop through all the subsets
    for subset in range(0, 1000):
        # build a decision tree
        clf.fit(X_subsets[subset], y_subsets[subset])
        # make prediction for the instance in the test set and append to list
        predictions.append(clf.predict(X_test[instance].reshape(-1, 2)))
    assert len(predictions) == 1000, "The number of predictions is not 1,000."
    assert len(predictions[0]) == 1, "Dimensional Error."
    # choose most voted class
    y_pred.append(stats.mode(np.array(predictions), axis=None)[0][0])

assert len(y_pred) == X_test.shape[0], "Dimensional mismatch."

# evaluate performance using accuracy
print("Accuracy on test set: {:.2f}%".format(accuracy_score(y_test, y_pred) * 100))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1369967080273487, 'fraction_numerical': 0.040010129146619396, 'mean_word_length': 4.266666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '440517', 'license': 'MIT', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1350, 'n_words': 327, 'path': 'em_pc_fa.ipynb', 'repo_name': 'MaelkoM/surviving-the-titanic', 'size': '3949'}