Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<set_options>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train["n"] = 0
test["n"] = 1
global tot
tot = pd.concat([train,test],sort = False )
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
SEED = 520
seed_everything(SEED )<feature_engineering>
def check_survive(label):
global tot
return tot[["Survived",label]].groupby(label ).Survived.mean()
def process_Sex() :
global tot
tot.Sex = tot.Sex.replace({"male": 0, "female": 1})
 def process_Name() :
global tot
tot["Title"] = tot.Name.str.extract("([A-Za-z]+)\.")
 tot["Title"].replace(['Lady', 'Countess','Sir', 'Jonkheer', 'Dona'], 'Upper',inplace=True)
 tot["Title"].replace(['Don', 'Rev','Capt','Col','Major','Dr','Rev'], 'Officer',inplace=True)
 tot.replace({"Mlle": "Miss", "Ms": "Miss", "Mme":"Mrs"},inplace=True)
 temp = check_survive("Title" ).sort_values()
temp.iloc[:] = np.arange(len(temp))
dic = temp.to_dict()
tot.Title.replace(dic, inplace= True)
 def process_Family() :
global tot
tot["FamilySize"] = tot.SibSp + tot.Parch + 1
tot.FamilySize = tot.FamilySize.map(lambda x: 2 if 1< x < 5 else 1 if x == 1 else 0)
 def process_Embarked() :
global tot
tot.Embarked.fillna("S",inplace =True)
 tot.Embarked = tot.Embarked.replace({"S": 0, "Q":1, "C":2})
 def fareof(x):
if x < 7.5: return 0
if x < 14 : return 1
if x < 50 : return 2
if x < 80 : return 3
return 4
def process_Fare() :
global tot
tot.Fare.fillna(0, inplace=True)
 tot.Fare[tot.Fare.notna() ] = tot.Fare[tot.Fare.notna() ].map(lambda x: fareof(x))
def ageof(x):
if x < 10: return 0
if x < 25 : return 1
if x < 35 : return 2
return 3
def process_Age() :
global tot
tot.Age[tot.Age.notna() ] = tot.Age[tot.Age.notna() ].map(lambda x: ageof(x))
dic = tot[tot.Age.notna() ][["Age","Title"]].groupby("Title" ).Age.mean().to_dict()
tot.Age[tot.Age.isna() ] = tot.Title[tot.Age.isna() ].replace(dic ).map(lambda x: int(x))
def process_Ticket() :
global tot
tot["RT"] = tot.Ticket.map(lambda x: x.split(" ")[0] if len(x.split(" ")) ==1 else x.split(" ")[1])
 tot["LT"] = tot.Ticket.map(lambda x: str(x.split(" ")[0])[0] if len(x.split(" ")) >1 else np.nan)
 tot["Tlen"] = tot.RT.map(lambda x: len(x))
tot["RT"] = tot.RT.map(lambda x: str(x)[0])
 def process_Cabin() :
global tot
def process_all() :
global tot
process_Name()
process_Embarked()
process_Family()
process_Fare()
process_Sex()
process_Age()
process_Ticket()
process_Cabin()
N_JOBS = cpu_count()
os.environ['MKL_NUM_THREADS'] = str(N_JOBS)
 os.environ['OMP_NUM_THREADS'] = str(N_JOBS)
 DataLoader = partial(DataLoader, num_workers=N_JOBS )<define_variables>
lnames = tot.Name.map(lambda x: x.split(",")[0])
 tot.Name = lnames
tnum = tot.Ticket.map(lambda x: x.split(" ")[-1])
 tot.Ticket = tnum
tot["FamSize"] = tot.SibSp + tot.Parch
nlist = tot.Name.value_counts().index
def _one_sample_positive_class_precisions(scores, truth):

 num_classes = scores.shape[0]
pos_class_indices = np.flatnonzero(truth > 0)
 if not len(pos_class_indices):
return pos_class_indices, np.zeros(0)
 retrieved_classes = np.argsort(scores)[::-1]
class_rankings = np.zeros(num_classes, dtype=np.int)
 class_rankings[retrieved_classes] = range(num_classes)
 retrieved_class_true = np.zeros(num_classes, dtype=np.bool)
 retrieved_class_true[class_rankings[pos_class_indices]] = True
retrieved_cumulative_hits = np.cumsum(retrieved_class_true)
 precision_at_hits =(
 retrieved_cumulative_hits[class_rankings[pos_class_indices]] / 
(1 + class_rankings[pos_class_indices].astype(np.float)))
 return pos_class_indices, precision_at_hits
def calculate_per_class_lwlrap(truth, scores):

 assert truth.shape == scores.shape
num_samples, num_classes = scores.shape
precisions_for_samples_by_classes = np.zeros(( num_samples, num_classes))
for sample_num in range(num_samples):
pos_class_indices, precision_at_hits =(
 _one_sample_positive_class_precisions(scores[sample_num, :],
truth[sample_num, :]))
precisions_for_samples_by_classes[sample_num, pos_class_indices] =(
 precision_at_hits)
 labels_per_class = np.sum(truth > 0, axis=0)
 weight_per_class = labels_per_class / float(np.sum(labels_per_class))
per_class_lwlrap =(np.sum(precisions_for_samples_by_classes, axis=0)/
np.maximum(1, labels_per_class))
return per_class_lwlrap, weight_per_class<define_variables>
tot["FamDeath"] = np.nan
for i in range(len(tot)) :
if tot.iloc[i,:].FamSize > 0:
hisname = tot.iloc[i,:].Name
hisfam = tot.iloc[i,:].FamSize
temp = pd.concat([tot.iloc[:i,:], tot.iloc[i+1:,:]])
 family = temp[(temp.Name == hisname)*(temp.FamSize == hisfam)]
if len(family)== 0:
continue
tot.FamDeath.iloc[i] = family.Survived.mean()
tot.FamDeath.fillna(0.5,inplace=True)
dataset_dir = Path('.. /input/freesound-audio-tagging-2019')
 preprocessed_dir = Path('.. /input/fat2019_prep_mels1' )<define_variables>
del tot["Ticket"], tot["Cabin"], tot["RT"], tot["LT"]
del tot["FamSize"], tot["Name"]
csvs = {
'train_curated': dataset_dir / 'train_curated.csv',
'train_noisy': preprocessed_dir / 'trn_noisy_best50s.csv',
'sample_submission': dataset_dir / 'sample_submission.csv',
}
dataset = {
'train_curated': dataset_dir / 'train_curated',
'train_noisy': dataset_dir / 'train_noisy',
'test': dataset_dir / 'test',
}
mels = {
'train_curated': preprocessed_dir / 'mels_train_curated.pkl',
'train_noisy': preprocessed_dir / 'mels_trn_noisy_best50s.pkl',
'test': preprocessed_dir / 'mels_test.pkl',
}<load_from_csv>
dropped = ["Survived","n","PassengerId","Embarked","Parch","Age","SibSp","Tlen"]
parameters = {
'n_estimators' : [100],
'random_state' : [1],
'n_jobs' : [3],
'min_samples_split': np.arange(8,12),
'max_depth' : np.arange(2,6)
 }
clf = grid_search.GridSearchCV(RandomForestClassifier() , parameters)
 clf.fit(tot[tot.n == 0].drop(dropped,axis = 1), tot[tot.n==0].Survived)
 print(clf.best_estimator_)
train_curated = pd.read_csv(csvs['train_curated'])
 train_noisy = pd.read_csv(csvs['train_noisy'])
 train_df = pd.concat([train_curated, train_noisy], sort=True, ignore_index=True)
 train_df.head()<load_from_csv>
data = []
clf = clf.best_estimator_
num_trial = 10
for i in range(num_trial):
X_train, X_test, y_train, y_test = train_test_split(tot[tot.n == 0].drop(dropped,axis = 1), tot[tot.n==0].Survived, random_state = i)
 clf.fit(X_train, y_train)
 data.append(clf.score(X_test, y_test))
plt.scatter(np.arange(num_trial),data )
test_df = pd.read_csv(csvs['sample_submission'])
 test_df.head()<data_type_conversions>
clf.fit(tot[tot.n == 0].drop(dropped,axis = 1), tot[tot.n == 0].Survived)
 subm = tot[tot.n == 1].drop(["Survived"], axis = 1 ).join(pd.Series(clf.predict(tot[tot.n == 1].drop(dropped,axis = 1)) ,name="Survived"))
<define_variables><EOS> 
subm = subm[["PassengerId","Survived"]].set_index("PassengerId")
 subm.Survived = subm.Survived.map(lambda x: int(x))
subm.to_csv("Submission.csv" )