Kaggle competition: Don't Get Kicked!
train_df['Age_Bin'] =(train_df['Age']//15)*15
train_df[['Age_Bin', 'Survived']].groupby(['Age_Bin'] ).mean()<count_values>
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()
print('Number of passengers in each class:')
 train_df['Pclass'].value_counts()<groupby>
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()
train_df['Fare_Bin'] = pd.qcut(train_df['Fare'], 5)
 train_df[['Fare_Bin', 'Survived']].groupby(['Fare_Bin'] ).mean()<filter>
train_data.isnull().sum()
alone = train_df[(train_df['SibSp'] == 0)&(train_df['Parch'] == 0)]
not_alone = train_df[(train_df['SibSp'] != 0)|(train_df['Parch'] != 0)]<count_values>
test_data.isnull().sum()
train_df['Title'] = train_df['Name'].apply(lambda x: x.split(',')[1].split('.')[0].strip())
 train_df['Title'].replace({'Mlle': 'Miss', 'Mme': 'Mrs', 'Ms': 'Miss'}, inplace = True)
 train_df['Title'].replace(['Don', 'Rev', 'Dr', 'Major', 'Lady', 'Sir', 'Col', 'Capt', 'the Countess', 'Jonkheer'],
'Rare Title', inplace = True)
 train_df['Title'].value_counts()<count_unique_values>
train_data['IsBadBuy'].value_counts()
print('Cabin:
Number of existing values: ', train_df['Cabin'].notnull().sum())
 print(' Number of unique values: ', train_df['Cabin'].nunique() )<feature_engineering>
train_data['Model'].value_counts()
all_data['Age'] = all_data['Age'].fillna(train_df['Age'].median())
 all_data['Fare'] = all_data['Fare'].fillna(train_df['Fare'].median())
 print('Done!' )<feature_engineering>
train_data.drop('Model',axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True )
all_data['Last_Name'] = all_data['Name'].apply(lambda x: str.split(x, ',')[0])
 all_data['Fare'].fillna(all_data['Fare'].mean() , inplace = True)
 default_sr_value = 0.5
all_data['Family_Survival'] = default_sr_value
for grp, grp_df in all_data[['Survived','Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId', 'SibSp', 'Parch', 'Age', 'Cabin']].groupby(['Last_Name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
all_data.loc[all_data['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
all_data.loc[all_data['PassengerId'] == passID, 'Family_Survival'] = 0
for _, grp_df in all_data.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
all_data.loc[all_data['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
all_data.loc[all_data['PassengerId'] == passID, 'Family_Survival'] = 0
all_data['Age_Bin'] =(all_data['Age']//15)*15
all_data['Fare_Bin'] = pd.qcut(all_data['Fare'], 5)
 all_data['Relatives'] = all_data['SibSp'] + all_data['Parch']
all_data['Title'] = all_data['Name'].apply(lambda x: x.split(',')[1].split('.')[0].strip())
 all_data['Title'].replace({'Mlle':'Miss', 'Mme':'Mrs', 'Ms':'Miss'}, inplace = True)
 all_data['Title'].replace(['Don', 'Rev', 'Dr', 'Major', 'Lady', 'Sir', 'Col', 'Capt', 'the Countess', 'Jonkheer', 'Dona'],
'Rare Title', inplace = True)
 print('Done!' )<categorify>
train_data['Trim'].value_counts()
all_data['Fare_Bin'] = LabelEncoder().fit_transform(all_data['Fare_Bin'])
 all_data['Age_Bin'] = LabelEncoder().fit_transform(all_data['Age_Bin'])
 all_data['Title_Bin'] = LabelEncoder().fit_transform(all_data['Title'])
 all_data['Sex'] = LabelEncoder().fit_transform(all_data['Sex'])
 print('Done!' )<drop_column>
train_data.drop('Trim',inplace=True,axis=1)
 test_data.drop('Trim',inplace=True,axis=1)
all_data.drop(['PassengerId', 'Age', 'Fare', 'Name', 'SibSp', 'Parch', 'Ticket', 'Cabin', 'Title', 'Last_Name', 'Embarked'], axis = 1, inplace = True)
 print('Done!')
 print('Modified dataset: ')
 all_data.head()<prepare_x_and_y>
train_data['SubModel'].value_counts()
train_df = all_data[:891]
X_train = train_df.drop('Survived', 1)
 y_train = train_df['Survived']
test_df = all_data[891:]
X_test = test_df.copy()
X_test.drop('Survived', axis = 1, inplace = True)
 print('Splitting: Done!' )<normalization>
train_data.drop('SubModel',inplace=True,axis=1)
 test_data.drop('SubModel',inplace=True,axis=1 )
std_scaler = StandardScaler()
X_train_scaled = std_scaler.fit_transform(X_train)
 X_test_scaled = std_scaler.transform(X_test)
 print('Scaling: Done!' )<choose_model_class>
train_data['Color'].value_counts()
random_state = 1
clf_list = [GaussianNB() ,
LogisticRegression(random_state = random_state),
KNeighborsClassifier() ,
SVC(random_state = random_state, probability = True),
DecisionTreeClassifier(random_state = random_state),
RandomForestClassifier(random_state = random_state),
XGBClassifier(random_state = random_state),
AdaBoostClassifier(base_estimator = DecisionTreeClassifier(random_state = random_state), random_state = random_state)]
cv_base_mean, cv_std = [], []
for clf in clf_list:
cv = cross_val_score(clf, X_train_scaled, y = y_train, scoring = 'accuracy', cv = 5, n_jobs = -1)
 cv_base_mean.append(cv.mean())
 cv_std.append(cv.std())
 cv_total = pd.DataFrame({'Algorithm': ['Gaussian Naive Bayes', 'Logistic Regression', 'k-Nearest Neighboors', 'SVC', 'Decision Tree', 'Random Forest', 'XGB Classifier', 'AdaBoost Classifier'],
'CV-Means': cv_base_mean,
'CV-Errors': cv_std})
 sns.barplot('CV-Means', 'Algorithm', data = cv_total, palette = 'Paired', orient = 'h', **{'xerr': cv_std})
 plt.xlabel('Mean Accuracy')
 plt.title('Cross Validation Scores')
 plt.xlim([0.725, 0.88])
 plt.axvline(x = 0.80, color = 'firebrick', linestyle = '--');<compute_train_metric>
test_data['Color'].value_counts()
estimators = [('gnb', clf_list[0]),('lr', clf_list[1]), 
('knn', clf_list[2]),('svc', clf_list[3]), 
('dt', clf_list[4]),('rf', clf_list[5]), 
('xgb', clf_list[6]),('ada', clf_list[7])]
base_voting_hard = VotingClassifier(estimators = estimators , voting = 'hard')
 base_voting_soft = VotingClassifier(estimators = estimators , voting = 'soft')
 cv_hard = cross_val_score(base_voting_hard, X_train_scaled, y_train, cv = 5)
 cv_soft = cross_val_score(base_voting_soft, X_train_scaled, y_train, cv = 5)
 print('Baseline Models - Ensemble
--------------------------')
 print('Hard Voting: {}%'.format(np.round(cv_hard.mean() *100, 1)))
 print('Soft Voting: {}%'.format(np.round(cv_soft.mean() *100, 1)) )<predict_on_test>
train_data['Color'].fillna(value='Color_Unknown',inplace=True)
 test_data['Color'].fillna(value='Color_Unknown',inplace=True )
base_voting_hard.fit(X_train_scaled, y_train)
 base_voting_soft.fit(X_train_scaled, y_train)
 y_pred_base_hard = base_voting_hard.predict(X_test_scaled)
 y_pred_base_soft = base_voting_hard.predict(X_test_scaled )<find_best_params>
print("Number of null values in Color column "+str(train_data['Color'].isnull().sum()))
print("Number of null values in Color column "+str(test_data['Color'].isnull().sum()))
cv_means_tuned = [np.nan]
def clf_performance(classifier, model_name):
print(model_name)
 print('-------------------------------')
 print(' Best Score: ' + str(classifier.best_score_))
print(' Best Parameters: ' + str(classifier.best_params_))
cv_means_tuned.append(classifier.best_score_ )<train_on_grid>
train_data['Transmission'].value_counts()
lr = LogisticRegression()
param_grid = {'max_iter' : [100],
'penalty' : ['l1', 'l2'],
'C' : np.logspace(-2, 2, 20),
'solver' : ['lbfgs', 'liblinear']}
clf_lr = GridSearchCV(lr, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_lr = clf_lr.fit(X_train_scaled, y_train)
 clf_performance(best_clf_lr, 'Logistic Regression' )<train_on_grid>
test_data['Transmission'].value_counts()
knn = KNeighborsClassifier()
param_grid = {'n_neighbors' : np.arange(3, 30, 2),
'weights': ['uniform', 'distance'],
'algorithm': ['auto'],
'p': [1, 2]}
clf_knn = GridSearchCV(knn, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_knn = clf_knn.fit(X_train_scaled, y_train)
 clf_performance(best_clf_knn, 'KNN' )<define_search_space>
train_data[train_data['Transmission']=='Manual']
svc = SVC(probability = True)
 param_grid = tuned_parameters = [{'kernel': ['rbf'],
'gamma': [0.01, 0.1, 0.5, 1, 2, 5],
'C': [.1, 1, 2, 5]},
{'kernel': ['linear'],
'C': [.1, 1, 2, 10]},
{'kernel': ['poly'],
'degree' : [2, 3, 4, 5],
'C': [.1, 1, 10]}]
clf_svc = GridSearchCV(svc, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_svc = clf_svc.fit(X_train_scaled, y_train)
 clf_performance(best_clf_svc, 'SVC' )<train_on_grid>
train_data['Transmission'].replace("Manual","MANUAL",inplace=True )
dt = DecisionTreeClassifier(random_state = 1)
 param_grid = {'max_depth': [3, 5, 10, 20, 50],
'criterion': ['entropy', 'gini'],
'min_samples_split': [5, 10, 15, 30],
'max_features': [None, 'auto', 'sqrt', 'log2']}
clf_dt = GridSearchCV(dt, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_dt = clf_dt.fit(X_train_scaled, y_train)
 clf_performance(best_clf_dt, 'Decision Tree' )<choose_model_class>
train_data['Transmission'].value_counts()
rf = RandomForestClassifier(random_state = 42)
 param_grid = {'n_estimators': [50, 150, 300, 450],
'criterion': ['entropy'],
'bootstrap': [True],
'max_depth': [3, 5, 10],
'max_features': ['auto','sqrt'],
'min_samples_leaf': [2, 3],
'min_samples_split': [2, 3]}
clf_rf = GridSearchCV(rf, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_rf = clf_rf.fit(X_train_scaled, y_train)
 clf_performance(best_clf_rf, 'Random Forest' )<choose_model_class>
train_data['Transmission'].fillna(value="Transmission_unk",inplace=True)
 test_data['Transmission'].fillna(value="Transmission_unk",inplace=True )
xgb = XGBClassifier(random_state = 42)
 param_grid = {'n_estimators': [15, 25, 50, 100],
'colsample_bytree': [0.65, 0.75, 0.80],
'max_depth': [None],
'reg_alpha': [1],
'reg_lambda': [1, 2, 5],
'subsample': [0.50, 0.75, 1.00],
'learning_rate': [0.01, 0.1, 0.5],
'gamma': [0.5, 1, 2, 5],
'min_child_weight': [0.01],
'sampling_method': ['uniform']}
clf_xgb = GridSearchCV(xgb, param_grid = param_grid, cv = 3, verbose = True, n_jobs = -1)
 best_clf_xgb = clf_xgb.fit(X_train_scaled, y_train)
 clf_performance(best_clf_xgb, 'XGB' )<train_on_grid>
train_data['WheelTypeID'].value_counts()
adaDTC = AdaBoostClassifier(base_estimator = DecisionTreeClassifier(random_state = random_state), random_state=random_state)
 param_grid = {'algorithm': ['SAMME', 'SAMME.R'],
'base_estimator__criterion' : ['gini', 'entropy'],
'base_estimator__splitter' : ['best', 'random'],
'n_estimators': [2, 5, 10, 50],
'learning_rate': [0.01, 0.1, 0.2, 0.3, 1, 2]}
clf_ada = GridSearchCV(adaDTC, param_grid = param_grid, cv = 5, scoring = 'accuracy', n_jobs = -1, verbose = 1)
 best_clf_ada = clf_ada.fit(X_train_scaled, y_train)
 clf_performance(best_clf_ada, 'AdaBost' )<create_dataframe>
train_data.drop('WheelTypeID',inplace=True,axis=1)
 test_data.drop('WheelTypeID',inplace=True,axis=1 )
cv_total = pd.DataFrame({'Algorithm': ['Gaussian Naive Bayes', 'Logistic Regression', 'k-Nearest Neighboors', 'SVC', 'Decision Tree', 'Random Forest', 'XGB Classifier', 'AdaBoost Classifier'],
'Baseline': cv_base_mean,
'Tuned Performance': cv_means_tuned})
 cv_total<find_best_params>
train_data['WheelType'].value_counts()
best_lr = best_clf_lr.best_estimator_
best_knn = best_clf_knn.best_estimator_
best_svc = best_clf_svc.best_estimator_
best_dt = best_clf_dt.best_estimator_
best_rf = best_clf_rf.best_estimator_
best_xgb = best_clf_xgb.best_estimator_
estimators = [('lr', best_lr),('knn', best_knn),('svc', best_svc), 
('rf', best_rf),('xgb', best_xgb),('dt', best_dt)]
tuned_voting_hard = VotingClassifier(estimators = estimators, voting = 'hard', n_jobs = -1)
 tuned_voting_soft = VotingClassifier(estimators = estimators, voting = 'soft', n_jobs = -1)
 tuned_voting_hard.fit(X_train_scaled, y_train)
 tuned_voting_soft.fit(X_train_scaled, y_train)
 cv_hard = cross_val_score(tuned_voting_hard, X_train_scaled, y_train, cv = 5)
 cv_soft = cross_val_score(tuned_voting_soft, X_train_scaled, y_train, cv = 5)
 print('Tuned Models - Ensemble
-----------------------')
 print('Hard Voting: {}%'.format(np.round(cv_hard.mean() *100, 2)))
 print('Soft Voting: {}%'.format(np.round(cv_soft.mean() *100, 2)))
 y_pred_tuned_hd = tuned_voting_hard.predict(X_test_scaled ).astype(int)
 y_pred_tuned_sf = tuned_voting_soft.predict(X_test_scaled ).astype(int )<save_to_csv>
test_data['WheelType'].value_counts()
test_df = pd.DataFrame(pd.read_csv('.. /input/titanic/test.csv')['PassengerId'])
 pd.DataFrame(data = {'PassengerId': test_df.PassengerId,
'Survived': y_pred_base_hard.astype(int)} ).to_csv('01-Baseline_Hard_voting.csv', index = False)
 pd.DataFrame(data = {'PassengerId': test_df.PassengerId,
'Survived': y_pred_base_soft.astype(int)} ).to_csv('02-Baseline_Soft_voting.csv', index = False)
 pd.DataFrame(data = {'PassengerId': test_df.PassengerId,
'Survived': y_pred_tuned_hd.astype(int)} ).to_csv('03-Tuned_Hard_Voting.csv', index = False)
 pd.DataFrame(data = {'PassengerId': test_df.PassengerId,
'Survived': y_pred_tuned_sf.astype(int)} ).to_csv('04-Tuned_Soft_Voting.csv', index = False )<set_options>
train_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value='WheelType_unk',inplace=True )
%matplotlib inline
py.init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
 GradientBoostingClassifier, ExtraTreesClassifier)
<load_from_csv>
train_data['WheelType'].value_counts()
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 PassengerId = test['PassengerId']
train.head(3 )<data_type_conversions>
train_data['Nationality'].value_counts()
train['Ticket_type'] = train['Ticket'].apply(lambda x: x[0:3])
 train['Ticket_type'] = train['Ticket_type'].astype('category')
 train['Ticket_type'] = train['Ticket_type'].cat.codes
test['Ticket_type'] = test['Ticket'].apply(lambda x: x[0:3])
 test['Ticket_type'] = test['Ticket_type'].astype('category')
 test['Ticket_type'] = test['Ticket_type'].cat.codes
train.head(3 )<feature_engineering>
test_data['Nationality'].value_counts()
full_data = [train, test]
train['Words_Count'] = train['Name'].apply(lambda x: len(x.split()))
test['Words_Count'] = test['Name'].apply(lambda x: len(x.split()))
train['Has_Cabin'] = train["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 test['Has_Cabin'] = test["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 for dataset in full_data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
for dataset in full_data:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
for dataset in full_data:
dataset['Embarked'] = dataset['Embarked'].fillna('S')
 for dataset in full_data:
dataset['Fare'] = dataset['Fare'].fillna(train['Fare'].median())
 train['CategoricalFare'] = pd.qcut(train['Fare'], 4)
 for dataset in full_data:
age_avg = dataset['Age'].mean()
age_std = dataset['Age'].std()
age_null_count = dataset['Age'].isnull().sum()
age_null_random_list = np.random.randint(age_avg - age_std, age_avg + age_std, size=age_null_count)
 dataset['Age'][np.isnan(dataset['Age'])] = age_null_random_list
dataset['Age'] = dataset['Age'].astype(int)
 train['CategoricalAge'] = pd.cut(train['Age'], 5)
 def get_title(name):
title_search = re.search('([A-Za-z]+)\.', name)
 if title_search:
return title_search.group(1)
 return ""
for dataset in full_data:
dataset['Title'] = dataset['Name'].apply(get_title)
 for dataset in full_data:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 for dataset in full_data:
dataset['Sex'] = dataset['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] 						 = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] 							 = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 dataset.loc[ dataset['Age'] <= 16, 'Age'] 					 = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4 ;<drop_column>
train_data['Nationality'].fillna(value='Nationality_unk',inplace=True)
 test_data['Nationality'].fillna(value='Nationality_unk',inplace=True )
drop_elements = ['PassengerId', 'Name', 'Ticket', 'Cabin', 'SibSp']
train = train.drop(drop_elements, axis = 1)
 train = train.drop(['CategoricalAge', 'CategoricalFare'], axis = 1)
 test = test.drop(drop_elements, axis = 1 )<prepare_x_and_y>
train_data['Size'].value_counts()
y_train = train['Survived'].ravel()
train = train.drop(['Survived'], axis=1)
 x_train = train.values
x_test = test.values<predict_on_test>
test_data['Size'].value_counts()
gbm = xgb.XGBClassifier(
 n_estimators= 2000,
max_depth= 4,
min_child_weight= 2,
gamma=0.9,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
nthread= -1,
scale_pos_weight=1 ).fit(x_train, y_train)
 xgb_predictions = gbm.predict(x_test )<create_dataframe>
train_data['Size'].fillna(value='Size_unk',inplace=True)
 test_data['Size'].fillna(value="Size_unk",inplace=True )
StackingSubmission = pd.DataFrame({ 'PassengerId': PassengerId,
'Survived': xgb_predictions } )<load_from_csv>
train_data['TopThreeAmericanName'].value_counts()
df1 = pd.read_csv('.. /input/91-genetic-algorithms-explained-using-geap/submission.csv')
 df2 = pd.read_csv('.. /input/titanic-eda-fe-3-model-decision-tree-viz/submission_GA.csv')
 df3 = pd.read_csv('.. /input/91-genetic-algorithms-explained-using-geap/submission.csv')
 x1=0.4; x2=0.3; df1.head()<save_to_csv>
test_data['TopThreeAmericanName'].value_counts()
df_ensemble = df1.copy()
k = 'Survived'
df_ensemble[k] = x1*df1[k] + x2*df2[k] +(1.0-x1-x2)*df3[k]
df_ensemble[k] = df_ensemble[k].apply(lambda f: 1 if f>=0.5 else 0)
 df_ensemble.to_csv('df_ensemble_pre.csv', index=False)
 df_ensemble.head()<save_to_csv>
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True )
df_ensemble = df1.copy()
k = 'Survived'
df_ensemble[k] = x1*df1[k] + x2*df2[k] +(1.0-x1-x2)*StackingSubmission[k]
df_ensemble[k] = df_ensemble[k].apply(lambda f: 1 if f>=0.5 else 0)
 df_ensemble.to_csv('df_ensemble.csv', index=False)
 df_ensemble.head()<load_from_csv>
train_data['PRIMEUNIT'].value_counts()
url="https://github.com/thisisjasonjafari/my-datascientise-handcode/raw/master/005-datavisualization/titanic.csv"
s=requests.get(url ).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
 test_data_with_labels = c
test_data = pd.read_csv('.. /input/titanic/test.csv')
 warnings.filterwarnings('ignore')
 for i, name in enumerate(test_data_with_labels['name']):
if '"' in name:
test_data_with_labels['name'][i] = re.sub('"', '', name)
 for i, name in enumerate(test_data['Name']):
if '"' in name:
test_data['Name'][i] = re.sub('"', '', name)
 survived = []
for name in test_data['Name']:
survived.append(int(test_data_with_labels.loc[test_data_with_labels['name'] == name]['survived'].values[-1]))
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission['Survived'] = survived
submission.to_csv('ground_truth.csv', index=False )<save_to_csv>
test_data['PRIMEUNIT'].value_counts()
StackingSubmission.to_csv("XGB.csv", index=False)
 StackingSubmission.head()<import_modules>
train_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 test_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True )
import numpy as np
import pandas as pd
from tensorflow import keras
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, BatchNormalization, MaxPool2D, Dropout
from tensorflow.python.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint<load_from_csv>
train_data['AUCGUART'].replace("AGREEN","GREEN",inplace=True)
 test_data['AUCGUART'].replace("ARED","RED",inplace=True )
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv")
<prepare_x_and_y>
train_data['AUCGUART'].fillna(value="AUC_unk",inplace=True)
 test_data['AUCGUART'].fillna(value="AUC_unk",inplace=True )
img_rows = 28
img_cols = 28
num_img = train.shape[0]
X_train = train.values[:,1:]
y_train = keras.utils.to_categorical(train.label, 10)
 X_train = X_train.reshape(num_img, img_rows, img_cols, 1)/255.0
X_test = test.values[:]
test_num_img = test.shape[0]
X_test = X_test.reshape(test_num_img, img_rows, img_cols, 1)/255.0<choose_model_class>
train_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1 )
model = Sequential()
model.add(Conv2D(256, kernel_size=(4, 4),
activation='elu',
input_shape=(img_rows, img_cols, 1)))
 model.add(MaxPool2D(2, 2))
model.add(Conv2D(128, kernel_size=(3,3), activation='elu'))
model.add(MaxPool2D(2, 2))
model.add(Conv2D(128, kernel_size=(3,3), activation='elu'))
model.add(Dropout(0.2))
model.add(Conv2D(128, kernel_size=(3,3), activation='elu'))
model.add(Flatten())
 model.add(Dense(128,activation='elu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(128,activation='tanh'))
model.add(BatchNormalization())
 model.add(Dense(10, activation='softmax'))<train_model>
train_data.drop('PurchDate',axis=1,inplace=True)
 test_data.drop("PurchDate",axis=1,inplace=True )
optimizer = keras.optimizers.Adam(learning_rate = 0.01)
 reduce_lr = ReduceLROnPlateau(monitor='val_accuracy',factor = 0.5, patience = 2, min_lr = 1e-6, verbose=1)
 earlystop = EarlyStopping(monitor='val_accuracy', patience=5, verbose=1)
 checkpoint = ModelCheckpoint("",monitor='val_accuracy', verbose=1, save_best_only=True)
 model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=optimizer,
metrics=['accuracy'])
 history = model.fit(X_train, y_train,
batch_size=128,
epochs=50,
validation_split = 0.1,
shuffle = True,
callbacks=[reduce_lr, earlystop, checkpoint])
<predict_on_test>
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'
model = keras.models.load_model("")
 predictions = model.predict_classes(X_test )<save_to_csv>
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
out = pd.DataFrame({"ImageId": i+1 , "Label": predictions[i]} for i in range(0, test_num_img))
out.to_csv('submission.csv', index=False )<train_model>
for i in not_categorical:
maximum=np.max(train_data[i])
 train_data[i]=train_data[i]/maximum
maximum_test=np.max(test_data[i])
 test_data[i]=test_data[i]/maximum_test
class TaskDataset(Dataset):
def __init__(self, path, name, mode='train'):
self.tokenizer = BertTokenizer('vocab.txt')
 self.items = self.__load(path, name, mode)
 def __load(self, path, name, mode):
items = []
task = Task(path, name)
 self.task_width, self.task_height, _ = task.max_size()
max_length = 2 +(self.task_width + 1)* self.task_height
if mode == 'train':
for inp, out in task.get_train() :
inp = self.__tokenize(inp, max_length)
 out = self.__tokenize(out, max_length)
 items.append(( name, inp, out))
if mode == 'valid':
for inp, out in task.get_valid() :
inp = self.__tokenize(inp, max_length)
 out = self.__tokenize(out, max_length)
 items.append(( name, inp, out))
if mode == 'tests':
for inp in task.get_tests() :
inp = self.__tokenize(inp, max_length)
 items.append(( name, inp, inp))
return items
def __tokenize(self, sequ, max_length):
sequ_dict = self.tokenizer.encode_plus(sequ, add_special_tokens=False, max_length=max_length, pad_to_max_length=True)
 return np.array(sequ_dict['input_ids'], dtype=np.int64), np.array(sequ_dict['attention_mask'], dtype=np.int64), np.array(sequ_dict['token_type_ids'], dtype=np.int64),
def __len__(self):
return len(self.items)
 def __getitem__(self, idx):
return self.items[idx] 
<init_hyperparams>
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
INTERMEDIATE_SIZE = 1024
HIDDEN_SIZE = 1024
VOCAB_SIZE = 16
def create_model_config(w, h):
return {
"architectures": [
"BertForMaskedLM"
],
"attention_probs_dropout_prob": 0.5,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.5,
"hidden_size": HIDDEN_SIZE,
"initializer_range": 0.02,
"intermediate_size": INTERMEDIATE_SIZE,
"max_position_embeddings": 2 +(w + 1)* h,
"num_attention_heads": 8,
"num_hidden_layers": 8,
"type_vocab_size": 1,
"vocab_size": VOCAB_SIZE,
"num_labels": 1
}
class Network(BertPreTrainedModel):
def __init__(self, w, h):
config = BertConfig.from_dict(create_model_config(w, h))
super(Network, self ).__init__(config)
 self.bert = BertModel(config)
 self.fc = nn.Linear(HIDDEN_SIZE, VOCAB_SIZE)
 self.init_weights()
def forward(self, X):
ids = X[0]
msk = X[1]
typ = X[2]
output = self.bert(ids, attention_mask=msk, token_type_ids=typ)
 x = output[0]
x = F.relu(self.fc(x))
x = x.transpose(1, 2)
 return x 
<init_hyperparams>
train_data[categorical[0]]
class Summary() :
def __init__(self):
self.epoch = -1
self.loss = None
self.accuracy = None
self.values = None
self.history = []
def register(self, epoch, loss, accuracy, values={}):
values['epoch'] = epoch
values['loss'] = loss
values['accuracy'] = accuracy
self.history.append(values)
 if self.epoch < 0 or accuracy > self.accuracy:
self.epoch = epoch
self.loss = loss
self.accuracy = accuracy
self.values = values
return True
return False
def hash(self):
def r4(v): return "{:6.4f}".format(v)
 return F'{self.epoch:02}-{r4(self.accy ).strip() }'
def __str__(self):
def r4(v): return "{:6.4f}".format(v)
 return F'{self.epoch}\t{r4(self.values["loss"])}\t{r4(self.values["accuracy"])}'
class BaseMetrics() :
def __init__(self):
self.loss = None
self.preds = None
self.targs = None
self.values = None
def begin(self):
self.loss = []
self.preds = []
self.targs = []
self.values = None
def update(self, loss, dz, dy):
self.loss.append(self._get_loss(loss))
self.preds.append(self._get_preds(dz))
self.targs.append(self._get_targets(dy))
def _get_loss(self, loss):
return loss.item()
def _get_preds(self, dz):
return dz.detach().cpu().numpy()
def _get_targets(self, dy):
return dy.detach().cpu().numpy()
def commit(self, epoch):
loss = np.mean(self.loss)
 accuracy = 0.0
self.values = {
'loss': 0.0,
'accuracy': 0.0
}
return accuracy
def __str__(self):
def r4(v): return "{:6.4f}".format(v)
 return F'{r4(self.values["loss"])}\t{r4(self.values["accuracy"])}'
class CrossEntropyMetrics(BaseMetrics):
def _get_preds(self, dz):
dz = dz[:,:,1:]
return dz.detach().cpu().numpy()
def _get_targets(self, dy):
dy = dy[:,1:]
return dy.detach().cpu().numpy()
def commit(self, epoch):
loss = np.mean(self.loss)
 preds = np.concatenate(self.preds, axis=0)
 targs = np.concatenate(self.targs, axis=0)
 preds = preds.argmax(axis=1)
 targs = targs
preds = preds.reshape(preds.shape[0], -1)
 targs = targs.reshape(targs.shape[0], -1)
 accuracy =(sum(preds == targs)/ len(preds)).mean()
match = 0
for pred, targ in zip(preds, targs):
if sum(pred != targ)== 0:
match += 1
accuracy += match
self.values = {
'loss': loss,
'accuracy': accuracy
}
return accuracy 
<define_variables>
pd.get_dummies(train_data[categorical[0]] )
def flattener(pred):
str_pred = str([row for row in pred])
 str_pred = str_pred.replace(', ', '')
 str_pred = str_pred.replace('[[', '|')
 str_pred = str_pred.replace('][', '|')
 str_pred = str_pred.replace(']]', '|')
 return str_pred
def random_seed(seed):
random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True 
<set_options>
for i in categorical:
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,inplace=True,axis=1 )
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 PATH = '.. /input/abstraction-and-reasoning-challenge'
PATH_TESTS = PATH + '/test'
EPOCHS = 100
BATCH = 4
LR = 0.0001
SEED = 1234
random_seed(SEED)
 TOKENS = ['[PAD]', '[UNK]', '[CLS]', '[SEP]', '[MASK]', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '[
with open('vocab.txt', 'w')as fp:
for t in TOKENS:
fp.write(t + '
')
 def train(name, epochs=EPOCHS):
ds_train = TaskDataset(PATH_TESTS, name, mode='train')
 ld_train = torch.utils.data.DataLoader(ds_train, batch_size=BATCH, num_workers=4, shuffle=True)
 w = ds_train.task_width
h = ds_train.task_height
model = Network(w, h)
 model = model.to(DEVICE)
 criterion = nn.CrossEntropyLoss()
optimizer = op.Adam(model.parameters() , lr=LR)
 metrics = CrossEntropyMetrics()
summary_train = Summary()
for epoch in range(epochs):
model.train()
metrics.begin()
for step,(id, x, y)in enumerate(ld_train):
optimizer.zero_grad()
x0 = x[0].to(DEVICE)
 x1 = x[1].to(DEVICE)
 x2 = x[2].to(DEVICE)
 dy = y[0].to(DEVICE)
 dz = model(( x0, x1, x2))
loss = criterion(dz[:,:,1:], dy[:,1:])
 metrics.update(loss, dz, dy)
 loss.backward()
optimizer.step()
accuracy = metrics.commit(epoch)
 summary_train.register(epoch, loss.item() , accuracy, metrics.values)
 print(summary_train)
 return model
def predict(model, name):
ds_tests = TaskDataset(PATH_TESTS, name, mode='tests')
 ld_tests = torch.utils.data.DataLoader(ds_tests, batch_size=1, shuffle=False)
 w = ds_tests.task_width
h = ds_tests.task_height
preds = []
model.eval()
with torch.no_grad() :
for step,(id, x, y)in enumerate(ld_tests):
x0 = x[0].to(DEVICE)
 x1 = x[1].to(DEVICE)
 x2 = x[2].to(DEVICE)
 dz = model(( x0, x1, x2))
z = dz.detach().cpu().numpy()
z = z[0]
az = z.argmax(0)
 w, h = Task.pred_size(az)
 w = max(1, w)
 h = max(1, h)
 pred = Task.as_array(az, w, h)
 pred = Task.as_list(pred)
 flat = flattener(pred)
 preds.append(flat)
 return preds
submission = pd.read_csv(PATH + '/sample_submission.csv', index_col='output_id')
 STEPS = -1
step = 0
hash = set()
for output_id in submission.index:
task_id = output_id.split('_')[0]
if task_id in hash:
continue
hash.add(task_id)
 name = str(task_id + '.json')
 model = train(name, EPOCHS)
 preds = predict(model, name)
 for n in range(len(preds)) :
pred = preds[n] + ' ' + preds[n] + ' ' + preds[n]
submission.loc[F'{task_id}_{n}', 'output'] = pred
step += 1
if step == STEPS: break
submission.to_csv('submission.csv')
<categorify>
for i in train_data.drop('IsBadBuy',axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))
df = pd.read_csv('submission.csv')
 df.head()
tokenizer = BertTokenizer('vocab.txt')
 def tokenize(sequ, max_length):
sequ_dict = tokenizer.encode_plus(sequ, add_special_tokens=False, max_length=max_length, pad_to_max_length=True)
 return np.array(sequ_dict['input_ids'], dtype=np.int64)
 for values in df.values:
name = values[0]
name = name[:-2] + '.json'
print('TRAIN', name)
 task = Task(PATH_TESTS, name)
 for inp, out in task.get_train() :
task_width, task_height, _ = task.max_size()
max_length = 2 +(task_width + 1)* task_height
x = tokenize(inp, max_length)
 y = tokenize(out, max_length)
 wx, hx = Task.pred_size(x)
 arr_x = Task.as_array(x, wx, hx)
 wy, hy = Task.pred_size(y)
 arr_y = Task.as_array(y, wy, hy)
 plot_task_array([arr_x, arr_y])
 ix = 0
print('PREDS', name)
 for inp in task.get_tests() :
try:
x = tokenize(inp, max_length)
 wx, hx = Task.pred_size(x)
 arr_x = Task.as_array(x, wx, hx)
 parts = values[1].split(' ')[0].split('|')
 parts = parts[1:-1]
h = len(parts)
 w = len(parts[0])
 a = np.zeros(( h, w), dtype='int')
 for y in range(h):
for x in range(w):
a[y][x] = parts[y][x]
plot_task_array([arr_x, a])
 except Exception as ex:
print(ex)
 print()<set_options>
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
%matplotlib inline<load_from_csv>
test_data=test_data[train_data.drop("IsBadBuy",axis=1 ).columns]
train_csv_path = '.. /input/train.csv'
test_csv_path = '.. /input/test.csv'
train_df = pd.read_csv(train_csv_path)
 test_df = pd.read_csv(test_csv_path)
 n_train = len(train_df)
 n_pixels = len(train_df.columns)- 1
n_class = len(set(train_df['label']))
print('Number of training samples: {0}'.format(n_train))
print('Number of training pixels: {0}'.format(n_pixels))
print('Number of classes: {0}'.format(n_class))
print(train_df.head())
 n_test = len(test_df)
 n_pixels = len(test_df.columns)
 print('Number of test samples: {0}'.format(n_test))
print('Number of test pixels: {0}'.format(n_pixels))
print(test_df.head() )<data_type_conversions>
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']
class MNISTDataset(Dataset):

 def __init__(self, dataframe,
transform = transforms.Compose([transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
):
df = dataframe
self.n_pixels = 784
if len(df.columns)== self.n_pixels:
self.X = df.values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = None
else:
self.X = df.iloc[:,1:].values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = torch.from_numpy(df.iloc[:,0].values)
 self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
if self.y is not None:
return self.transform(self.X[idx]), self.y[idx]
else:
return self.transform(self.X[idx] )<choose_model_class>
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )
RandAffine = transforms.RandomAffine(degrees=45, translate=(0.1, 0.1), scale=(0.8, 1.2))<data_type_conversions>
from sklearn.neighbors import KNeighborsClassifier
batch_size = 64
train_transforms = transforms.Compose(
 [transforms.ToPILImage() ,
RandAffine,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
 val_test_transforms = transforms.Compose(
 [transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
 def get_dataset(dataframe, dataset=MNISTDataset,
transform=transforms.Compose([transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])) :
return dataset(dataframe, transform=transform )<choose_model_class>
from sklearn.neighbors import KNeighborsClassifier
<choose_model_class>
from sklearn.neighbors import KNeighborsClassifier
class MNISTResNet(ResNet):
def __init__(self):
super(MNISTResNet, self ).__init__(BasicBlock, [2, 2, 2, 2], num_classes=10)
 self.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=1, padding=3,bias=False)
 model = MNISTResNet()
print(model )<train_on_grid>
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )
def train(train_loader, model, criterion, optimizer, epoch):
model.train()
for batch_idx,(data, target)in enumerate(train_loader):
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss = criterion(output, target)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if(batch_idx + 1)% 100 == 0:
print('Train Epoch: {} [{}/{}({:.0f}%)]\tLoss: {:.6f}'.format(
 epoch,(batch_idx + 1)* len(data), len(train_loader.dataset),
100.*(batch_idx + 1)/ len(train_loader), loss.data.item()))<find_best_params>
KNN.score(X_test,y_test )
def validate(val_loader, model, criterion):
model.eval()
loss = 0
correct = 0
for _,(data, target)in enumerate(val_loader):
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss += criterion(output, target ).data.item()
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
loss /= len(val_loader.dataset)
 print('
On Val set Average loss: {:.4f}, Accuracy: {}/{}({:.3f}%)
 '.format(
 loss, correct, len(val_loader.dataset),
100.0 * float(correct)/ len(val_loader.dataset)) )<choose_model_class>
predict=KNN.predict(test_data.drop('RefId',axis=1))
total_epoches = 20
step_size = 5
base_lr = 0.01
optimizer = optim.Adam(model.parameters() , lr=base_lr)
 criterion = nn.CrossEntropyLoss()
exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=step_size, gamma=0.1)
 if torch.cuda.is_available() :
model = model.cuda()
criterion = criterion.cuda()<split>
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()
def split_dataframe(dataframe=None, fraction=0.9, rand_seed=1):
df_1 = dataframe.sample(frac=fraction, random_state=rand_seed)
 df_2 = dataframe.drop(df_1.index)
 return df_1, df_2
for epoch in range(total_epoches):
print("
Train Epoch {}: lr = {}".format(epoch, exp_lr_scheduler.get_lr() [0]))
train_df_new, val_df = split_dataframe(dataframe=train_df, fraction=0.9, rand_seed=epoch)
 train_dataset = get_dataset(train_df_new, transform=train_transforms)
 val_dataset = get_dataset(val_df, transform=val_test_transforms)
 train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size, shuffle=True)
 val_loader = torch.utils.data.DataLoader(dataset=val_dataset,
batch_size=batch_size, shuffle=False)
 train(train_loader=train_loader, model=model, criterion=criterion, optimizer=optimizer, epoch=epoch)
 validate(val_loader=val_loader, model=model, criterion=criterion)
 exp_lr_scheduler.step()<predict_on_test>
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True )
def prediciton(test_loader, model):
model.eval()
test_pred = torch.LongTensor()
for i, data in enumerate(test_loader):
if torch.cuda.is_available() :
data = data.cuda()
output = model(data)
 pred = output.cpu().data.max(1, keepdim=True)[1]
test_pred = torch.cat(( test_pred, pred), dim=0)
 return test_pred<create_dataframe>
Submission.head()
Submission.to_csv('Submission.csv' )