<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 1. Load train and test data
Step1:  2. Tokenize name into (surname, title, first name and maiden name)
Step2:  2.1 Extract features from Title variable
Step3:  It seems we can extract some info from title
1. Whether a woman is married Mme/Mrs vs Miss/Mlle  vs Ms(Undetermined or single 
Step4:  3 Examine marriages / sibling relationships
Step5:  Initialize data structures for algorithm
Step6:  1. Extract marriages in greedy fashion. Assume is_married has no fp ( might have actually 
<ASSISTANT_TASK:>
Python Code:
train = pd.read_csv("data/train.csv")
train["dataset"] = "train"
train.head()
test = pd.read_csv("data/test.csv")
test["dataset"] = "test"
test.head()
#Combine both datasets to predict families
train = train.append(test)
train.set_index(train["PassengerId"],inplace=True)
Explanation: 1. Load train and test data
End of explanation
name_tokenizer = re.compile(r"^(?P<surname>[^,]+), (?P<title>[A-Z a-z]+?)\. (?P<f_name>[A-Z a-z.]+)?(?P<maiden_name>\([A-Za-z .]+\))?")
name_tokens = ["surname","title","f_name","maiden_name"]
for name_tk in name_tokens:
    train[name_tk] = train.Name.apply(lambda x: name_tokenizer.match(x).group(name_tk))
    test[name_tk] = test.Name.apply(lambda x: name_tokenizer.match(x).group(name_tk))
train.head(n=5)
Explanation: 2. Tokenize name into (surname, title, first name and maiden name)
End of explanation
print train.groupby(["title","Sex"]).size()
Explanation: 2.1 Extract features from Title variable
End of explanation
#Encode special title following this logic
train.has_special_title = train.title.apply(lambda x: x not in ["Mr","Mrs","Miss","Mme","Mlle","Master"])
Explanation: It seems we can extract some info from title
1. Whether a woman is married Mme/Mrs vs Miss/Mlle  vs Ms(Undetermined or single :/? )
2. Master title apparently given to male kids
2. Nobility vs laypeople : (Dr, Col, Capt,  ...) vs (Mr,Master,Mrs,Miss). Ambiguous cases (Mlle,Mme,Ms,Don/Dona?)
End of explanation
def is_married(couple_rows):
    are_married=False
    if couple_rows.irow(0).Sex != couple_rows.irow(1).Sex:
        #Get who is the husband and whose the wife
        man = couple_rows.irow(0) if couple_rows.irow(0).Sex == "male" else couple_rows.irow(1)
        woman = couple_rows.irow(0) if couple_rows.irow(0).Sex == "female" else couple_rows.irow(1)
        #Marriage tests
        marriage_tests = {}
        marriage_tests["same_f_name"] = woman.f_name is not None and woman.f_name in man.f_name
        marriage_tests["consistent_title"] =  woman.title not in ("Miss","Mlle") and man.title != "Master"
        marriage_tests["same_ticket"] = woman.Ticket == man.Ticket
        marriage_tests["same_pclass"] = woman.Pclass == man.Pclass
        marriage_tests["legal_age"] = (woman.title in ("Mme","Mrs") or woman.Age >= 10) and man.Age > 10
        marriage_tests["consistent_SibSp"] = (woman.SibSp > 0 and man.SibSp > 0) or (woman.SibSp == man.SibSp)
        are_married = marriage_tests["same_f_name"] and marriage_tests["legal_age"] or ( )
        
        consistency_checks = ( marriage_tests["consistent_title"] and 
                               marriage_tests["legal_age"] and 
                              marriage_tests["same_pclass"] and 
                              marriage_tests["same_ticket"] and
                              marriage_tests["consistent_SibSp"])
        if are_married and not consistency_checks:
            failed_tests = ", ".join("{}:{}".format(x,marriage_tests[x]) for x in marriage_tests if not marriage_tests[x])
            print "WARNING: Sketchy marriage: {}".format(failed_tests)
            print couple_rows
            
            print
    return are_married
Explanation: 3 Examine marriages / sibling relationships
End of explanation
#Data structures - sets to keep track which ones have already been assigned
married_people = set()
people_with_parents = set()
links_to_assign = train[["SibSp","Parch"]]
#Matches a couple with the Max amount of kids they can have 
#Which is the min(husband.Parch, wife.Parch)
marriages_table = {}
Explanation: Initialize data structures for algorithm
End of explanation
#Subset only people who have spouses/siblings on the boat
train_sibsp = train.ix[ train.SibSp > 0]
#People grouped by surname
surname_groups = train_sibsp.groupby("surname").groups
for surname in surname_groups:
    surname_rows = surname_groups[surname]
    couples = itertools.combinations(surname_rows,2)
    for cpl in couples:
        cpl_rows = train_sibsp.ix[list(cpl)]
        if is_married(cpl_rows):
            #Make sure we're not marrying somebody twice :p
            assert cpl[0] not in married_people,"{} is already married :/".format(cpl[0])
            assert cpl[1] not in married_people,"{} is already married :/".format(cpl[1])
            
            #add couples to married set
            married_people.add(cpl[0])
            married_people.add(cpl[1])
            
            marriages_table[cpl] = min(links_to_assign.ix[cpl[0]]["Parch"], links_to_assign.ix[cpl[1]]["Parch"] )
            
            
            #print
#    break
marriages_table
train.ix[list((26,1066))]
train.ix[ (train.SibSp > 0) | (train.Parch > 0) ].shape
train
Explanation: 1. Extract marriages in greedy fashion. Assume is_married has no fp ( might have actually :/ )
End of explanation
<END_TASK>