Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
%matplotlib inline
def run_classifier(data):
with tf.Graph().as_default() :
data_size = FLAGS.num_variables
num_classes = FLAGS.num_classes
data_placeholder = tf.placeholder("float", shape=(None, data_size))
logits = inference(data_placeholder, data_size, 1.0)
 init_op = tf.group(tf.global_variables_initializer() , tf.local_variables_initializer())
 sess = tf.Session()
sess.run(init_op)
 saver = tf.train.Saver()
saver.restore(sess, './models.ckpt')
 prediction_list = []
total = len(data)
 with sess.as_default() :
for i in range(total):
v = logits.eval(feed_dict={data_placeholder: [data[i]]})
 sm = tf.nn.softmax(v)
 smv = sm.eval()
prediction_list.append(smv[0])
 cls = np.argmax(smv)
 return prediction_list<save_to_csv>
train = pd.read_csv('.. /input/titanic/train.csv', index_col=0)
 test = pd.read_csv('.. /input/titanic/test.csv', index_col=0)
 train
def make_output(class_list):
result_df = pd.DataFrame(class_list)
 train_df =(pd.read_csv('.. /input/train.csv'))
test_df =(pd.read_csv('.. /input/test.csv'))
cat_label_list = train_df['species'].astype('category' ).cat.categories
new_columns = ['id']
new_columns.extend(list(cat_label_list))
result_df.insert(0, 'id', test_df['id'])
 result_df.columns = new_columns
result_df.to_csv(path_or_buf='predicted.csv', index=False, encoding='utf-8' )<load_from_csv>
freq = train['Embarked'].value_counts().index[0]
train['Embarked'].fillna(freq, inplace=True )
test_data = load_data_and_labels('.. /input/test.csv', is_labels_exist=False)
 result_list = run_classifier(test_data)
 make_output(result_list )<set_options>
train.drop(['Ticket', 'Cabin'], axis=1, inplace=True)
 train
%matplotlib inline 
<set_options>
train = pd.get_dummies(train, columns=['Embarked', 'Sex'], prefix=['Embarked', 'Sex'], prefix_sep=' - ')
 train
rcParams['figure.figsize'] = 10,10<load_from_csv>
list_handling = train['Name'].apply(lambda x: x.split(',')[1].split('.')[0] ).value_counts()
list_handling
data = pd.read_csv('.. /input/train.csv')
 parent_data = data.copy()
ID = data.pop('id' )<categorify>
for elem in list_handling.index:
cond1 = train['Name'].str.contains(elem + '.', regex=False)
 cond2 = train['Age'].isnull()
train.loc[cond1 & cond2, 'Age'] = train.loc[cond1, 'Age'].mean()
y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 print(y.shape )<normalization>
nans = train.isnull().sum()
nans[nans != 0]
X = StandardScaler().fit(data ).transform(data)
 print(X.shape )<categorify>
test.isnull().sum()
y_cat = to_categorical(y)
 print(y_cat.shape )<choose_model_class>
test.isnull().sum()
model = Sequential()
model.add(Dense(1024,input_dim=192))
model.add(Dropout(0.2))
model.add(Activation('sigmoid'))
model.add(Dense(512))
model.add(Dropout(0.3))
model.add(Activation('sigmoid'))
model.add(Dense(99))
model.add(Activation('softmax'))<choose_model_class>
test = pd.get_dummies(test, columns=['Embarked', 'Sex'], prefix=['Embarked', 'Sex'], prefix_sep=' - ')
 test.drop(['Cabin', 'Ticket'], axis=1, inplace=True)
 test
model.compile(loss='categorical_crossentropy', optimizer='rmsprop' )<train_model>
for elem in list_handling.index:
cond1 = test['Name'].str.contains(elem + '.', regex=False)
 cond2 = test['Age'].isnull()
test.loc[cond1 & cond2, 'Age'] = train.loc[train['Name'].str.contains(elem + '.', regex=False), 'Age'].mean()
cond3 = test['Fare'].isnull()
test.loc[cond3, 'Fare'] = train.loc[train['Name'].str.contains(elem + '.', regex=False), 'Fare'].mean()
history = model.fit(X, y_cat, batch_size=128, nb_epoch=100, verbose=1 )<load_from_csv>
train.drop(['Name'], axis=1, inplace=True)
 test.drop(['Name'], axis=1, inplace=True )
test = pd.read_csv('.. /input/test.csv' )<drop_column>
Y_train = train['Survived'].values
X_train = train.drop(['Survived'], axis=1 ).values
X_test = test.values
index = test.pop('id' )<normalization>
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
test = StandardScaler().fit_transform(test )<predict_on_test>
mean = X_train.mean(axis=0)
 std = X_train.std(axis=0)
 X_train -= mean
X_train /= std
X_test -= mean
X_test /= std
yPred = model.predict_proba(test )<save_to_csv>
model = Sequential()
model.add(Dense(40, kernel_initializer='uniform', activation='relu', input_shape=(X_train.shape[1],)))
 model.add(Dropout(0.5))
model.add(Dense(100, kernel_initializer='uniform', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(40, kernel_initializer='uniform', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
model.compile(optimizer=optimizers.Adam(lr=1e-5), loss='binary_crossentropy', metrics=['acc'])
 model.summary()
yPred = pd.DataFrame(yPred,index=index,columns=sorted(parent_data.species.unique()))
yPred.to_csv('predictions.csv' )<drop_column>
checkpoint_path = 'bestmodel4.hdf5'
checkpoint = ModelCheckpoint(checkpoint_path, monitor='val_acc', verbose=0, save_best_only=True, mode='max')
 callbacks_list = [checkpoint]
history = model.fit(X_train, Y_train, batch_size=30, epochs=3000,
callbacks=callbacks_list, verbose=0, validation_split=0.2 )
yPredTest = yPredTest.set_value(1190, 'Acer_Rubrum', 1 )<save_to_csv>
model.load_weights(checkpoint_path)
 pred = model.predict(X_test)
 Y_pred =(pred > 0.5 ).astype(int )
yPredTest.to_csv('predictions.csv' )<define_variables>
Y_pred = Y_pred[:, 0]
Y_pred
( -np.log10(10**-15)/594 )<compute_test_metric>
submission = pd.DataFrame({"Survived": Y_pred}, index=test.index)
 submission.to_csv("submission.csv" )
0.407/(-np.log10(10**-15)/594)
<load_from_csv>
pd.concat([submission, pd.read_csv('.. /input/titanic/gender_submission.csv', index_col=0)], axis=1 )
df_train=pd.read_csv(".. /input/leaf-classification/train.csv.zip",index_col='id')
 df_train.shape<count_missing_values>
print(confusion_matrix(pd.read_csv('.. /input/titanic/gender_submission.csv', index_col=0 ).values, Y_pred))
accuracy_score(pd.read_csv('.. /input/titanic/gender_submission.csv', index_col=0 ).values, Y_pred )
df_train.isna().sum()<prepare_x_and_y>
precision_recall_fscore_support(pd.read_csv('.. /input/titanic/gender_submission.csv', index_col=0 ).values,
Y_pred, average='binary' )
y_train=df_train.species
X_train=df_train.drop(columns=['species'],axis=1 )<count_unique_values>
param_grid = [
{'penalty' : ['l1', 'l2', 'elasticnet', 'none'],
'C' : np.logspace(-4, 4, 500),
'solver' : ['lbfgs','newton-cg','liblinear','sag','saga'],
'max_iter' : np.arange(100, 150, 10)
 }
]
scoring = {'Accuracy': 'accuracy'}
gs = GridSearchCV(LogisticRegression() , return_train_score=True,
param_grid=param_grid, scoring=scoring, cv=10, refit='Accuracy', n_jobs=-1 )
object_cols = [cname for cname in X_train.columns if X_train[cname].nunique() < 10 and X_train[cname].dtype == "object"]
object_cols<count_values>
gs.fit(X_train, Y_train)
 print("best params: " + str(gs.best_estimator_))
print("best params: " + str(gs.best_params_))
print('best score:', gs.best_score_ )
len(y_train.value_counts() )<categorify>
Y_pred2 = gs.predict(X_test )
encoder = LabelEncoder()
y_fit = encoder.fit(y_train)
 y_train = y_fit.transform(y_train)
 classes = list(y_fit.classes_ )<normalization>
submission2 = pd.DataFrame({"Survived": Y_pred2}, index=test.index)
 submission2.to_csv("submission2.csv" )
quantile_transformer = QuantileTransformer(random_state=0)
 scaler = quantile_transformer.fit(X_train)
 X_train= quantile_transformer.transform(X_train )<define_search_space>
pd.concat([submission2, pd.read_csv('.. /input/titanic/gender_submission.csv', index_col=0)], axis=1 )
parameters = {
'tol':[0.001, 0.009],
'C':list(range(100, 1000,100)) ,
'max_iter': list(range(10, 100, 10)) ,
"solver":("newton-cg", "lbfgs", "liblinear"),
"penalty":("l1", "l2")
 }
parameters<define_variables>
print(confusion_matrix(pd.read_csv('.. /input/titanic/gender_submission.csv', index_col=0 ).values, Y_pred2))
accuracy_score(pd.read_csv('.. /input/titanic/gender_submission.csv', index_col=0 ).values, Y_pred2 )
my_randome_state=500<choose_model_class>
precision_recall_fscore_support(pd.read_csv('.. /input/titanic/gender_submission.csv', index_col=0 ).values,
Y_pred2, average='binary' )