Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<split>
pd.plotting.register_matplotlib_converters()
%matplotlib inline
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train_data, dev_data = train_test_split(data, test_size=0.1, random_state=0 )<prepare_x_and_y>
train_path = ".. /input/titanic/train.csv"
train_data = pd.read_csv(train_path)
 train_data = train_data.set_index("PassengerId")
 train_data[0:20]
X_train, y_train = train_data.drop(columns=['G1', 'G2', 'G3']), train_data['G3']
X_dev, y_dev = dev_data.drop(columns=['G1', 'G2', 'G3']), dev_data['G3']<define_variables>
test_path = ".. /input/titanic/test.csv"
test_data = pd.read_csv(test_path)
 test_data = test_data.set_index("PassengerId")
 X_test = test_data
X_test.head()
num_features = ['age', 'absences', 'failures', 'studytime', 'Medu', 'Fedu', 'goout', 'absences', 'freetime']
cat_features = ['sex']<train_model>
y = train_data.Survived
X = train_data.drop(['Survived','Name','Ticket'], axis = 1)
 X_test = X_test.drop(['Name', 'Ticket'], axis = 1)
 X.head()
class FeatureSelector(BaseEstimator, TransformerMixin):
def __init__(self, columns):
self.columns = columns
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
return X[self.columns]
class OutlierQuantileRemover(BaseEstimator, TransformerMixin):
def __init__(self, column, q=0.99):
self.column = column
self.q = q
self.quantile = None
def fit(self, X, y=None):
self.quantile = X[self.column].quantile()
return self
def transform(self, X, y=None):
X.loc[X[self.column] > self.quantile, self.column] = self.quantile
return X
class OutlierThresholdRemover(BaseEstimator, TransformerMixin):
def __init__(self, column, t):
self.column = column
self.t = t
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X.loc[X[self.column] > self.t, self.column] = self.t
return X
pipeline = Pipeline([ 
('selector', FeatureSelector(columns=num_features+cat_features)) , 
('out', OutlierThresholdRemover(column='failures', t=3)) , 
('out2', OutlierQuantileRemover(column='freetime')) , 
('pre', make_column_transformer(( OneHotEncoder(handle_unknown='ignore'), cat_features), remainder='passthrough')) , 
('clf', LinearRegression(normalize=True))
])
 pipeline.fit(X_train, y_train )<predict_on_test>
print("Column, dtype, null, unique")
 for column in X:
print(column,X[column].dtype,X[column].isnull().sum() ,X[column].nunique())
 print("Percentage of null Cabin values: " + str(X.Cabin.isnull().sum() /X.Cabin.isnull().count()))
y_pred_train = pipeline.predict(X_train)
 y_pred_dev = pipeline.predict(X_dev)
 print('train: ', mean_absolute_error(y_train, y_pred_train))
print('dev: ', mean_absolute_error(y_dev, y_pred_dev))<load_from_csv>
print("Column, dtype, null")
 for column in X_test:
print(column,X_test[column].dtype,X_test[column].isnull().sum())
 print("Percentage of null Cabin values in test data: " + str(X_test.Cabin.isnull().sum() /X_test.Cabin.isnull().count()))
X_test = pd.read_csv("/kaggle/input/machine-learning-lab-cas-data-science-hs-20/test-data.csv", index_col=0 )<predict_on_test>
X = X.drop(['Cabin'], axis = 1 )
y_test_pred = pipeline.predict(X_test )<save_to_csv>
X_test = X_test.drop(['Cabin'], axis = 1 )
X_test_submission = pd.DataFrame(index=X_test.index)
 X_test_submission['G3'] = y_test_pred
X_test_submission.to_csv('submission_outlier_pipeline.csv', header=True, index_label='id' )<set_options>
enco = LabelEncoder()
X_label = train_data.copy()
enco.fit_transform(X.Sex.unique())
 X_label['Sex'] = enco.transform(train_data['Sex'])
 X_label.Embarked = X_label.Embarked.fillna(value = 'A')
 X_labeled = X_label.copy()
enco2 = LabelEncoder()
enco2.fit_transform(X_label.Embarked.unique())
 X_labeled['Embarked'] = enco2.transform(X_label['Embarked'] )
warnings.simplefilter(action='ignore', category=FutureWarning )<load_from_csv>
imp = SimpleImputer(strategy = 'most_frequent')
 X_imp = pd.DataFrame(imp.fit_transform(X))
X_test_imp = pd.DataFrame(imp.transform(X_test))
X_imp.columns = X.columns
X_test_imp.columns = X_test.columns
cols = ['Sex','Embarked']
enco = OneHotEncoder(handle_unknown = 'ignore', sparse = False)
 X_label = pd.DataFrame(enco.fit_transform(X_imp[cols]))
X_test_label = pd.DataFrame(enco.transform(X_test_imp[cols]))
X_label.index = X.index
X_test_label.index = X_test.index
X = X.drop(cols, axis=1)
 X_test = X_test.drop(cols, axis=1)
 X = pd.concat([X, X_label], axis=1)
 X_test = pd.concat([X_test, X_test_label], axis=1)
 X = X.rename(columns={0: "female", 1: "male", 2: "C", 3: "Q", 4: "S"})
 X_test = X_test.rename(columns={0: "female", 1: "male", 2: "C", 3: "Q", 4: "S"} )
train = pd.read_csv('.. /input/insta_train.csv')
 test = pd.read_csv('.. /input/insta_test.csv' )<categorify>
X_new = X.drop(['Parch','SibSp'], axis = 1)
 X_test = X_test.drop(['Parch','SibSp'], axis = 1)
 X_train, X_val, y_train, y_val = train_test_split(X_new, y, train_size=0.8, test_size=0.2, random_state=0)
 X_train.head()
v = TfidfVectorizer(max_features=None, max_df=0.1, stop_words='english', min_df=0.0003)
 x = v.fit(train['caption'].fillna('' ).values)
 a = v.transform(train['caption'].fillna('' ).values)
 b = v.transform(test['caption'].fillna('' ).values )<categorify>
def testing(bs):
model = keras.Sequential([
layers.BatchNormalization(input_shape = [8]),
layers.Dense(units = 8, activation = 'relu'),
layers.Dense(units = 8, activation = 'relu'),
layers.Dense(units = 1, activation = 'sigmoid')
 ])
 model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['binary_accuracy'])
 early_stopping = keras.callbacks.EarlyStopping(patience = 25, min_delta = 0.001, restore_best_weights = True)
 history = model.fit(X_train, y_train, validation_data =(X_val, y_val), batch_size = bs, epochs = 500, callbacks = [early_stopping], verbose = 0)
 history = pd.DataFrame(history.history)
 print(bs)
 print(( "Best Validation Loss: {:0.4f}" +\
"
Best Validation Accuracy: {:0.4f}")\ 
.format(history['val_loss'].min() ,
history['val_binary_accuracy'].max()))
<concatenate><EOS> 
pred = model.predict(X_test)
 preds = []
for i in range(0,418):
if np.isnan(pred[i][0]):
pred[i][0] = 1
preds.append(int(round(pred[i][0])))
 output = pd.DataFrame({'PassengerId': X_test.index,'Survived': preds})
 output.to_csv('submission.csv', index=False)
 preds