Kaggle competition: Titanic - Machine Learning from Disaster
le_res=le.fit_transform(df['Year'])
 Year=pd.DataFrame(le_res)
 Year.columns=['Year']
df=df.drop(labels=['Year'],axis=1)
 df=pd.concat([Year,df],axis=1)
 le_res_test=le.fit_transform(df_test['Year'])
 Year_test=pd.DataFrame(le_res_test)
 Year_test.columns=['Year']
df_test=df_test.drop(labels=['Year'],axis=1)
 df_test=pd.concat([Year_test,df_test],axis=1)
<categorify>
df = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
le_res=le.fit_transform(df['Month'])
 Month=pd.DataFrame(le_res)
 Month.columns=['Month']
df=df.drop(labels=['Month'],axis=1)
 df=pd.concat([Month,df],axis=1)
 le_res_test=le.fit_transform(df_test['Month'])
 Month_test=pd.DataFrame(le_res_test)
 Month_test.columns=['Month']
df_test=df_test.drop(labels=['Month'],axis=1)
 df_test=pd.concat([Month_test,df_test],axis=1)
<categorify>
style.use('ggplot')
le_res=le.fit_transform(df['Day'])
 Day=pd.DataFrame(le_res)
 Day.columns=['Day']
df=df.drop(labels=['Day'],axis=1)
 df=pd.concat([Day,df],axis=1)
 le_res_test=le.fit_transform(df_test['Day'])
 Day_test=pd.DataFrame(le_res_test)
 Day_test.columns=['Day']
df_test=df_test.drop(labels=['Day'],axis=1)
 df_test=pd.concat([Day_test,df_test],axis=1)
<categorify>
df.drop(['Name'], axis=1, inplace = True)
 df.drop(['PassengerId'], axis=1, inplace= True)
 test.drop(['Name'], axis=1, inplace = True)
le_res=le.fit_transform(df['Hour'])
 Hour=pd.DataFrame(le_res)
 Hour.columns=['Hour']
df=df.drop(labels=['Hour'],axis=1)
 df=pd.concat([Hour,df],axis=1)
 le_res_test=le.fit_transform(df_test['Hour'])
 Hour_test=pd.DataFrame(le_res_test)
 Hour_test.columns=['Hour']
df_test=df_test.drop(labels=['Hour'],axis=1)
 df_test=pd.concat([Hour_test,df_test],axis=1)
<categorify>
df.fillna(0, inplace = True)
 test.fillna(0, inplace = True)
le_res=le.fit_transform(df['Minute'])
 Minute=pd.DataFrame(le_res)
 Minute.columns=['Minute']
df=df.drop(labels=['Minute'],axis=1)
 df=pd.concat([Minute,df],axis=1)
 le_res_test=le.fit_transform(df_test['Minute'])
 Minute_test=pd.DataFrame(le_res_test)
 Minute_test.columns=['Minute']
df_test=df_test.drop(labels=['Minute'],axis=1)
 df_test=pd.concat([Minute_test,df_test],axis=1)
<feature_engineering>
bins = [0,1, 5, 10, 25, 50, 100]
labels = [1,2,3,4,5,6]
df["raw_radial"]=np.sqrt(np.power(df['X'],2)+ np.power(df['Y'],2))
df_test["raw_radial"]=np.sqrt(np.power(df_test['X'],2)+ np.power(df_test['Y'],2))<drop_column>
df['Age'] = pd.cut(df['Age'], bins = bins, labels = labels)
 test['Age'] = pd.cut(test['Age'], bins = bins, labels = labels)
le_res=le.fit_transform(df['Category'])
 cat=pd.DataFrame(le_res)
 cat.columns=['Category']
df=df.drop(labels=['Category'],axis=1)
 df=pd.concat([cat,df],axis=1)
 df.columns<normalization>
df = pd.get_dummies(df, columns = ['Sex'],drop_first = True)
 test = pd.get_dummies(test, columns = ['Sex'],drop_first = True )
xy_scaler = StandardScaler()
xy_scaler.fit(df.loc[:,['X','Y']])
 xy_scaled = xy_scaler.transform(df.loc[:,['X','Y']])
 kmeans = KMeans(n_clusters=26, init='k-means++')
 kmeans.fit(xy_scaled);
xy_scaler_test = StandardScaler()
xy_scaler_test.fit(df_test.loc[:,['X','Y']])
 xy_scaled_test = xy_scaler_test.transform(df_test.loc[:,['X','Y']])
 kmeans = KMeans(n_clusters=26, init='k-means++')
 kmeans.fit(xy_scaled_test);<predict_on_test>
bins = [0,10,20,30,50, 100, 200 , 250, 300, 350, 400, 450, 500, 550]
labels = [1,2,3,4,5,6,7,8,9,10,11,12,13]
df['Fare'] = pd.cut(df['Fare'], bins = bins, labels = labels)
 test['Fare'] = pd.cut(test['Fare'], bins = bins, labels = labels)
 df['Fare'] = df['Fare'].astype('int32')
 test['Fare'] = test['Fare'].astype('int32' )
geoData = df.loc[:,['X','Y']]
df['closest_centers_f'] = kmeans.predict(geoData)
 id_label=kmeans.labels_
df.loc[:,'label'] = pd.Series(kmeans.labels_)
 geoData_test = df_test.loc[:,['X','Y']]
df_test['closest_centers_f'] = kmeans.predict(geoData_test)
 id_label_test=kmeans.labels_
df_test.loc[:, 'label'] = pd.Series(kmeans.labels_ )<create_dataframe>
fare_scale = preprocessing.MinMaxScaler()
df_fares = df['Fare'].values
scaled_fares = df_fares.reshape(-1,1)
 scaled_fares = fare_scale.fit_transform(scaled_fares)
 scaled_fares = scaled_fares.flatten()
df['Fare'] = pd.Series(scaled_fares)
 test_fares = test['Fare'].values
scaled_fares = test_fares.reshape(-1,1)
 scaled_fares = fare_scale.transform(scaled_fares)
 scaled_fares = scaled_fares.flatten()
test['Fare'] = pd.Series(scaled_fares )
le_res=le.fit_transform(df['Address'])
 Address=pd.DataFrame(le_res)
 Address.columns=['Address']
df=df.drop(labels=['Address'],axis=1)
 df=pd.concat([Address,df],axis=1)
 le_res=le.fit_transform(df_test['Address'])
 Address_test=pd.DataFrame(le_res)
 Address_test.columns=['Address']
df_test=df_test.drop(labels=['Address'],axis=1)
 df_test=pd.concat([Address_test,df_test],axis=1 )<drop_column>
label_enc = preprocessing.LabelEncoder()
df['Cabin'] = df['Cabin'].astype('str')
 test['Cabin'] = test['Cabin'].astype('str')
 enc_list = []
for i in df['Cabin'].values:
enc_list.append(i)
 for i in test['Cabin'].values:
enc_list.append(i)
 label_enc.fit(enc_list)
 df['Cabin'] = label_enc.transform(df['Cabin'])
 test['Cabin'] = label_enc.transform(test['Cabin'] )
df=df[['Address', 'Minute', 'Hour', 'Day', 'Month', 'Year',
'District', 'DayOfWeek', 'X', 'Y', 'rot45_X', 'rot45_Y', 'rot30_X',
'rot30_Y', 'radial60', 'Intersection', 'Block', 'raw_radial',
'closest_centers_f', 'label']]
df_test=df_test[['Address', 'Minute', 'Hour', 'Day', 'Month', 'Year', 'District',
'DayOfWeek', 'X', 'Y', 'rot45_X', 'rot45_Y', 'rot30_X', 'rot30_Y',
'radial60', 'Intersection', 'Block', 'raw_radial', 'closest_centers_f',
'label']]<split>
label_enc = preprocessing.LabelEncoder()
df['Embarked'] = df['Embarked'].astype('str')
 df['Embarked'] = label_enc.fit_transform(df['Embarked'])
 test['Embarked'] = test['Embarked'].astype('str')
 test['Embarked'] = label_enc.transform(test['Embarked'] )
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.20,shuffle=False )<train_model>
label_enc = preprocessing.LabelEncoder()
df['Ticket'] = df['Ticket'].astype('str')
 test['Ticket'] = test['Ticket'].astype('str')
 enc_list = []
for i in df['Ticket'].values:
enc_list.append(i)
 for i in test['Ticket'].values:
enc_list.append(i)
 label_enc.fit(enc_list)
 df['Ticket'] = label_enc.transform(df['Ticket'])
 test['Ticket'] = label_enc.transform(test['Ticket'] )
HYPER_PARAMS = {
'learning_rate': 0.02,
'n_estimators':800,
'max_depth': 6,
'subsample': 0.8,
'colsample_bytree': 0.8,
'max_delta_step': 1,
'objective': 'multi:softmax',
'nthread': 4,
'seed': 1747
}
model = xgb.XGBClassifier(**HYPER_PARAMS)
 model.fit(X,y)
<predict_on_test>
df = df.astype('float')
 df.fillna(0, inplace = True)
 test = test.astype('float')
 test.fillna(0, inplace = True )
y_pred=model.predict_proba(df_test )<categorify>
X = np.array(df.drop(['Survived'], axis = 1))
X = preprocessing.scale(X)
 Y = np.array(df['Survived'] )
temp = data['Category']
le.fit_transform(temp)
 le.classes_<create_dataframe>
X_train, X_test, Y_train, Y_test = train_test_split(X,Y,stratify = Y, test_size = 0.1, random_state = 31 )
y_pred= pd.DataFrame(y_pred, index=Id_test,columns = le.classes_ )<save_to_csv>
model = svm.SVC(kernel = 'poly',degree=3, random_state = 31, gamma = "auto", C = 1)
 model.fit(X_train, Y_train)
 predictions = model.predict(X_train)
 accuracy = accuracy_score(predictions,Y_train)
 print("Training accuracy = %0.2f" %(accuracy * 100))
y_pred.to_csv("submit.csv", float_format = '%.5F' )<choose_model_class>
predictions = model.predict(X_test)
 accuracy = accuracy_score(predictions,Y_test)
 print("Testing accuracy = %0.2f" %(accuracy * 100))
<train_model>
clf = DecisionTreeClassifier(max_depth=4, random_state = 31)
 clf.fit(X_train, Y_train)
 predictions = model.predict(X_train)
 accuracy_clf = accuracy_score(predictions, Y_train)
 print("Training accuracy = %0.2f" %(accuracy_clf * 100))
<set_options>
predictions = clf.predict(X_test)
 accuracy_clf = accuracy_score(predictions, Y_test)
 print("Testing accuracy = %0.2f" %(accuracy_clf * 100))
tf.enable_eager_execution()
tf.executing_eagerly()<count_values>
Pid = test['PassengerId']
X_final_test = test.drop(['PassengerId'], axis = 1)
 X_final_test = preprocessing.scale(X_final_test)
for label in data.columns[1:]:
print("Distribution of", label)
 print(data[label].value_counts() )<count_values>
Y_pred = clf.predict(X_final_test )
LABELS = data.columns[1:]
def build_label(row):
return ",".join([LABELS[idx] for idx, val in enumerate(row[1:])if val == 1])
 data.apply(lambda x: build_label(x), axis=1 ).value_counts()<split>
test['Survived'] = Y_pred
train_data, val_data = train_test_split(data, test_size=0.2, random_state=2019 )<init_hyperparams>
test = test.astype('int32' )
IMAGE_SIZE = 224
IMAGENET_MEAN = [0.485, 0.456, 0.406]
IMAGENET_STD = [0.229, 0.224, 0.225]
BATCH_SIZE = 64
LEARNING_RATE = 0.001
LEARNING_RATE_SCHEDULE_FACTOR = 0.1
LEARNING_RATE_SCHEDULE_PATIENCE = 5
MAX_EPOCHS = 100<categorify>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": test["Survived"]
} )
def preprocessing_image(image):

 return image<normalization>
submission.to_csv('submission.csv', index=False )
<categorify><EOS> 
submission.to_csv('submission.csv', index=False )