Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
%matplotlib inline
sns.set()
plt.style.use('seaborn')
 warnings.filterwarnings("ignore", category = DeprecationWarning)
 warnings.filterwarnings("ignore", category = FutureWarning)
max_dict = dict(( feat, np.max(data[feat])+ 1)for feat in keep_cate )<set_options>
train_raw = pd.read_csv('.. /input/titanic/train.csv')
 test_raw = pd.read_csv('.. /input/titanic/test.csv')
 passengerid = test_raw['PassengerId']
class Attention(Layer):
def __init__(self, step_dim,
W_regularizer=None, b_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('glorot_uniform')
 self.W_regularizer = regularizers.get(W_regularizer)
 self.b_regularizer = regularizers.get(b_regularizer)
 self.W_constraint = constraints.get(W_constraint)
 self.b_constraint = constraints.get(b_constraint)
 self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
super(Attention, self ).__init__(**kwargs)
 def build(self, input_shape):
assert len(input_shape)== 3
self.W = self.add_weight(( input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight(( input_shape[1],),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer,
constraint=self.b_constraint)
 else:
self.b = None
self.built = True
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(K.dot(K.reshape(x,(-1, features_dim)) ,
K.reshape(self.W,(features_dim, 1))),(-1, step_dim))
if self.bias:
eij += self.b
eij = K.tanh(eij)
 a = K.exp(eij)
 if mask is not None:
a *= K.cast(mask, K.floatx())
 a /= K.cast(K.sum(a, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 a = K.expand_dims(a)
 weighted_input = x * a
return K.sum(weighted_input, axis=1)
 def compute_output_shape(self, input_shape):
return input_shape[0], self.features_dim 
<categorify>
train_copy = train_raw.copy(deep = True)
 test_copy = test_raw.copy(deep = True )
def get_dnn_model2(n_train, batch_size, epochs):
conts_size = 12
inputs = [Input(shape=[1], name=feat)for feat in keep_cate + keep_float]
embeds = []
for i, feat in enumerate(keep_cate):
embeds.append(Flatten()(Embedding(max_dict[feat], conts_size )(inputs[i])))
 y_dot = []
for field1, field2 in itertools.combinations([i for i in range(len(keep_cate)) ], 2):
embed1 = embeds[field1]
embed2 = embeds[field2]
y_dot.append(
 Dot(1, normalize=False )([embed1, embed2])
)
 y_dot_cat = concatenate(y_dot)
 for i, feat in enumerate(keep_float):
embeds.append(inputs[i + len(keep_cate)])
 title = Input(shape=[pad_title], name="Description")
 embed_title = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1],
weights=[embedding_matrix], trainable= False )(title)
 embed_title = SpatialDropout1D(0.2 )(embed_title)
 embed_title = Bidirectional(CuDNNGRU(96, return_sequences=True))(embed_title)
 att_pool = Attention(pad_title )(embed_title)
 max_pool2 = GlobalMaxPooling1D()(embed_title)
 embed_title = concatenate([att_pool, max_pool2])
 description = Input(shape=[pad_desc], name="metadata_annots_top_desc")
 embed_description = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1],
weights=[embedding_matrix], trainable= False )(description)
 embed_description = SpatialDropout1D(0.2 )(embed_description)
 embed_description = Bidirectional(CuDNNGRU(128, return_sequences=True))(embed_description)
 att_pool = Attention(pad_desc )(embed_description)
 max_pool2 = GlobalMaxPooling1D()(embed_description)
 embed_description = concatenate([att_pool,max_pool2])
 sentiment_entities = Input(shape=[pad_sent], name="sentiment_entities")
 embed_sentiment_entities = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1],
weights=[embedding_matrix], trainable= False )(sentiment_entities)
 embed_sentiment_entities = SpatialDropout1D(0.2 )(embed_sentiment_entities)
 embed_sentiment_entities = Bidirectional(CuDNNGRU(128, return_sequences=True))(embed_sentiment_entities)
 att_pool = Attention(pad_sent )(embed_sentiment_entities)
 max_pool2 = GlobalMaxPooling1D()(embed_sentiment_entities)
 embed_sentiment_entities = concatenate([att_pool,max_pool2])
 inputs += [title, description,sentiment_entities]
embeds += [embed_title, embed_description, embed_sentiment_entities]
out = concatenate(embeds)
 out = concatenate([out, y_dot_cat])
 out = Dense(300 )(out)
 out = Activation('relu' )(out)
 out = Dropout(0.2 )(out)
 out = BatchNormalization()(out)
 out = Dense(1, activation='linear', name='output' )(out)
 model = Model(inputs, out)
 exp_decay = lambda init, fin, steps:(init / fin)**(1 /(steps - 1)) - 1
steps = int(n_train / batch_size)* epochs
lr_init, lr_fin = 0.001, 0.0005
lr_decay = exp_decay(lr_init, lr_fin, steps)
 optimizer = Adam(lr=lr_init, decay=lr_decay)
 model.compile(optimizer=optimizer, loss="mse")
 return model
def get_dnn_model(n_train, batch_size, epochs):
conts_size = 10
inputs = [Input(shape=[1], name=feat)for feat in keep_cate + keep_float]
embeds = []
for i, feat in enumerate(keep_cate):
if feat in data.columns:
embeds.append(Flatten()(Embedding(max_dict[feat], conts_size )(inputs[i])))
 for i, feat in enumerate(keep_float):
if feat in data.columns:
embeds.append(inputs[i + len(keep_cate)])

out = concatenate(embeds)
 out = Dense(512 )(out)
 out = BatchNormalization()(out)
 out = Activation('relu' )(out)
 out = Dense(1, activation='linear', name='output' )(out)
 model = Model(inputs, out)
 exp_decay = lambda init, fin, steps:(init / fin)**(1 /(steps - 1)) - 1
steps = int(n_train / batch_size)* epochs
lr_init, lr_fin = 0.001, 0.0005
lr_decay = exp_decay(lr_init, lr_fin, steps)
 optimizer = Adam(lr=lr_init, decay=lr_decay)
 model.compile(optimizer=optimizer, loss="mse")
 return model
def rmse(y_true, y_pred):
return mean_squared_error(y_true, y_pred)
 def get_oof(model, X, y, X_test,
k_fold, metric=rmse, save=False,
name=None, is_df=False):
ntrain, ntest = X.shape[0], X_test.shape[0]
oof_train = np.zeros(( ntrain,))
oof_test = np.zeros(( ntest,))
kf = StratifiedKFold(n_splits=k_fold, shuffle=True, random_state=1337)
 oof_test_skf = np.empty(( k_fold, ntest))
errors = []
for i,(train_index, val_index)in enumerate(kf.split(X,y)) :
print(f'Fold {i}.......... ')
 if is_df:
X_train, y_train, X_val, y_val = \
X.loc[train_index], y.loc[train_index], X.loc[val_index], y.loc[val_index]
else:
X_train, y_train, X_val, y_val = \
X[train_index], y[train_index], X[val_index],y[val_index]
val_pred = model.fit_predict(X_train, y_train, X_val, y_val)
 error = metric(val_pred, y_val)
 print(f"{i}th error is {error}")
 errors.append(error)
 oof_train[val_index] = val_pred
oof_test_skf[i, :] = model.predict(X_test)
 print(f"mean error is {np.mean(errors)}")
 oof_test[:] = oof_test_skf.mean(axis=0)
 return oof_train.reshape(-1, 1), oof_test.reshape(-1, 1 )<prepare_x_and_y>
train_copy.loc[(train_copy['Sex']=='female')&(train_copy['Embarked']=='S')&(train_copy['Pclass']==1)&(train_copy['Survived']==0)]
X = data[:len_train]
X_test = data[len_train:]
X = pd.concat([X,X_TXT[:len_train]], axis = 1)
 X_test = pd.concat([X_test,X_TXT[len_train:]], axis = 1)
 print(len(X), type(X))
print(len(X_test))
y = X_train['AdoptionSpeed']
k_fold = 10
batch_size = 300
epochs = 10
dnn_wrapper = DNNModelWrapper(get_dnn_model2, batch_size, epochs, get_keras_data)
 dnn_oof_train, dnn_oof_test = get_oof(dnn_wrapper, X, y,
X_test, k_fold,
save=True, name="dnn_v1",
is_df=True)
<compute_train_metric>
common_titles = ['Mr.','Miss.','Mrs.','Master.']
for df in [train_copy, test_copy]:
df['FamilySize'] = df['Parch']+df['SibSp']+1
df['IsAlone']=0
df.loc[(df.FamilySize==1),'IsAlone'] = 1
df['NameLen'] = df['Name'].apply(lambda x : len(x))
df['Title'] = df['Name'].apply(lambda x : x.split(',')[1].strip().split() [0])
 df['Title'] = df['Title'].apply(lambda x : x if x in common_titles else 'Misc.' )
def get_fea_importance(clf, feature_name):
gain = clf.feature_importance('gain')
 importance_df = pd.DataFrame(
 {'feature':clf.feature_name() ,
'split':clf.feature_importance('split'),
'gain':100 * gain / gain.sum() ,
} ).sort_values('gain',ascending=False)
 return importance_df
def run_lgb(params, X_train, X_test):
n_splits = 10
verbose_eval = 1000
num_rounds = 60000
early_stop = 500
kf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=1337)
 oof_train = np.zeros(( X_train.shape[0]))
oof_test = np.zeros(( X_test.shape[0], n_splits))
i = 0
feature_importance_df = pd.DataFrame()
for train_idx, valid_idx in kf.split(X_train, X_train['AdoptionSpeed'].values):
X_tr = X_train.iloc[train_idx, :]
X_val = X_train.iloc[valid_idx, :]
y_tr = X_tr['AdoptionSpeed'].values
X_tr = X_tr.drop(['AdoptionSpeed'], axis=1)
 y_val = X_val['AdoptionSpeed'].values
X_val = X_val.drop(['AdoptionSpeed'], axis=1)
 lgb_train = lgb.Dataset(X_tr, y_tr)
 lgb_val = lgb.Dataset(X_val, y_val)
 clf = lgb.train(params, lgb_train , valid_sets = lgb_val,feature_name = list(X_tr.columns),
verbose_eval=verbose_eval, early_stopping_rounds=early_stop, num_boost_round=num_rounds)
 valid_pred = clf.predict(X_val, num_iteration=clf.best_iteration)
 test_pred = clf.predict(X_test, num_iteration=clf.best_iteration)
 fold_importance_df_fold = get_fea_importance(clf, list(X_tr.columns))
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df_fold], axis=0)
 oof_train[valid_idx] = valid_pred
oof_test[:, i] = test_pred
i += 1
feature_importance_df = feature_importance_df[
["feature", "split","gain"]
].groupby("feature",as_index=False ).mean().sort_values(by="gain", ascending=False)
 oof_test = oof_test.mean(axis=1)
 return oof_train, oof_test, feature_importance_df<init_hyperparams>
train_copy.loc[train_copy['NameLen']==54]
lgb_params = {
'learning_rate': 0.005,
'eval_metric': 'l2',
'seed': 1337,
'bagging_fraction': 0.8,
'feature_fraction': 0.75,
'boosting': 'gbdt',
'max_depth':-1,
'num_leaves': 32,
"objective": "regression",
}<split>
train_copy['NameLenBin'],train_namelen_bins = pd.cut(train_copy['NameLen'].astype(int),20,retbins = True )
oof_train_lgb, oof_test_lgb,feature_importance_df = run_lgb(lgb_params, X_train_non_null, X_test_non_null )<define_variables>
titlewise_grouping = train_copy.groupby('Title')
 titlewise_agemean = titlewise_grouping.mean()
titlewise_agemean['Age'].to_frame()
drop_f = list(feature_importance_df[feature_importance_df.gain == 0].feature )<drop_column>
train_copy['Age'] = titlewise_grouping['Age'].apply(lambda x: x.fillna(x.mean()))
X_train_non_null.drop(drop_f, inplace = True, axis = 1)
 X_test_non_null.drop(drop_f, inplace = True, axis = 1 )<train_model>
titlewise_grouping = test_copy.groupby('Title')
 titlewise_agemean = titlewise_grouping.mean()
test_copy['Age'] = titlewise_grouping['Age'].apply(lambda x: x.fillna(x.mean()))
oof_train, oof_test = run_xgb(xgb_params, X_train_non_null, X_test_non_null )<feature_engineering>
for dataset in [train_copy, test_copy]:
dataset['Fare'] = dataset['Fare'].fillna(dataset['Fare'].mean())
 dataset['Embarked'] = dataset['Embarked'].fillna(dataset['Embarked'].mode() [0] )
X_train_non_null['nn_pred'] = dnn_oof_train
X_test_non_null['nn_pred'] = dnn_oof_test
X_train_non_null['lgb_pred'] = oof_train_lgb
X_test_non_null['lgb_pred'] = oof_test_lgb
X_train_non_null['xgb_pred'] = oof_train
X_test_non_null['xgb_pred'] = oof_test.mean(axis=1)
 dnn_oof_train = X_train_non_null['nn_pred'].values
dnn_oof_test = X_test_non_null['nn_pred'].values
lgb_oof_train = X_train_non_null['lgb_pred'].values
lgb_oof_test = X_test_non_null['lgb_pred'].values<define_variables>
test_age_bins = np.concatenate(( [-np.inf], train_age_bins[1:-1], [np.inf]))
test_namelen_bins = np.concatenate(( [-np.inf], train_namelen_bins[1:-1], [np.inf]))
test_copy['AgeBin'] = pd.cut(test_copy['Age'], test_age_bins)
 test_copy['NameLenBin'] = pd.cut(test_copy['NameLen'], test_namelen_bins)
 test_fare_bins = np.concatenate(( [-np.inf], train_fare_bins[1:-1], [np.inf]))
test_copy['FareBin'] = pd.cut(test_copy['Fare'], test_fare_bins )
oof_train_ensemble = oof_train * 0.45 + dnn_oof_train * 0.1 + lgb_oof_train * 0.45
oof_test_ensemble = oof_test.mean(axis=1)* 0.45 + dnn_oof_test * 0.1 + lgb_oof_test * 0.45<save_to_csv>
print("Done.Final Check")
 print(train_copy.isnull().sum())
 print("-"*25)
 print(test_copy.isnull().sum() )
optR = OptimizedRounder()
optR.fit(oof_train_ensemble, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
valid_pred = optR.predict(oof_train_ensemble, coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, valid_pred)
 print("QWK = ", qwk)
 test_predictions = optR.predict(oof_test_ensemble, coefficients)
 Counter(test_predictions)
 plot_pred(test_predictions)
 submission = pd.DataFrame({'PetID': test['PetID'].values, 'AdoptionSpeed': test_predictions})
 submission.to_csv('submission.csv', index=False )<compute_train_metric>
for dataset in [train_copy,test_copy]:
dataset = dataset.drop(['Cabin','PassengerId','Ticket','Name','FareBin','SibSp','Parch','NameLenBin','Age'],axis = 'columns',inplace = True )
optR = OptimizedRounder()
optR.fit(oof_train_lgb, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
valid_pred = optR.predict(oof_train_lgb, coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, valid_pred)
 print("QWK LGB = ", qwk )<compute_train_metric>
train_copy = pd.get_dummies(train_copy,columns = ['Embarked'],drop_first = False)
 test_copy = pd.get_dummies(test_copy, columns = ['Embarked'],drop_first = False )
optR = OptimizedRounder()
optR.fit(dnn_oof_train, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
valid_pred = optR.predict(dnn_oof_train, coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, valid_pred)
 print("QWK DNN= ", qwk )<statistical_test>
for dataframe in [train_copy,test_copy]:
dataframe['Sex'] = dataframe['Sex'].map({'male':0, 'female': 1 })
 dataframe['Title'] = dataframe['Title'].map({'Mr.':1, 'Misc.': 0, 'Master.':2, 'Miss.': 3, 'Mrs.': 4 })
 dataframe['Pclass'] = dataframe['Pclass'].map({1:3,2:2,3:1} )
optR = OptimizedRounder()
optR.fit(oof_train, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
valid_pred = optR.predict(oof_train, coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, valid_pred)
 print("QWK XGB= ", qwk )<predict_on_test>
labeler = LabelEncoder()
train_copy['AgeBin'] = labeler.fit_transform(train_copy['AgeBin'])
 test_copy['AgeBin'] = labeler.fit_transform(test_copy['AgeBin'] )
coefficients_ = coefficients.copy()
train_predictions = optR.predict(oof_train_ensemble, coefficients_ ).astype(np.int8)
 print(f'train pred distribution: {Counter(train_predictions)}')
 test_predictions = optR.predict(oof_test_ensemble, coefficients_ ).astype(np.int8)
 print(f'test pred distribution: {Counter(test_predictions)}' )<count_values>
scaler = StandardScaler()
scaler.fit(train_copy.drop(['Survived'],axis=1))
scaled_features = scaler.transform(train_copy.drop(['Survived'],axis=1))
train_copy_scaled = pd.DataFrame(scaled_features)
 test_copy.fillna(test_copy.mean() , inplace=True)
 scaled_features = scaler.transform(test_copy)
 test_copy_scaled = pd.DataFrame(scaled_features )
Counter(train_predictions )<count_values>
X_train_scaled = train_copy_scaled
y_train_scaled = train_copy['Survived']
X_test_scaled = test_copy_scaled
Counter(test_predictions )<statistical_test>
X_train = train_copy.drop('Survived',axis = 1)
 y_train = train_copy['Survived']
X_test = test_copy.copy(deep = True )
oof_train_lgb_stacking, oof_test_lgb_stacking, feature_importance_df = run_lgb(lgb_params, X_train_non_null, X_test_non_null)
 print(feature_importance_df)
 optR = OptimizedRounder()
optR.fit(oof_train_lgb_stacking, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
valid_pred = optR.predict(oof_train_lgb_stacking, coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, valid_pred)
 print("QWK XGB= ", qwk )<set_options>
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
pd.options.display.max_columns = 128
torch.multiprocessing.set_start_method("spawn" )<load_pretrained>
svc = SVC(C=100, gamma=0.01)
 scores_rfc = cross_val_score(svc, X_train, y_train, cv=10, scoring='accuracy')
 print("Mean score for 10 folds is ",scores_rfc.mean() *100 )
def jopen(path):
with open(path, "r", encoding="utf-8")as f:
json_file = json.load(f)
 return json_file
def parse_sentiment_file(path):
file = jopen(path)
 language: str = file["language"]
sentiment: list = file["documentSentiment"]
entities: list = [x["name"] for x in file["entities"]]
entity = " ".join(entities)
 sentence_sentiment: list = [x["sentiment"] for x in file["sentences"]]
magnitude: np.ndarray = np.array(
 [x["magnitude"] for x in sentence_sentiment])
 score: np.ndarray = np.array([x["score"] for x in sentence_sentiment])
 return_js = {
"magnitude_sum": magnitude.sum() ,
"magnitude_mean": magnitude.mean() ,
"magnitude_var": magnitude.var() ,
"score_sum": score.sum() ,
"score_mean": score.mean() ,
"score_var": score.var() ,
"language": language,
"entity": entity,
"document_magnitude": sentiment["magnitude"],
"document_score": sentiment["score"]
}
return return_js
def parse_metadata(path):
file: dict = jopen(path)
 file_keys = list(file.keys())
 if "labelAnnotations" in file_keys:
file_annots = file["labelAnnotations"]
file_mean_score = np.asarray([x["score"] for x in file_annots] ).mean()
file_desc = " ".join([x["description"] for x in file_annots])
 else:
file_mean_score = np.nan
file_desc = ""
file_colors: list = file["imagePropertiesAnnotation"]["dominantColors"][
"colors"]
file_crops: list = file["cropHintsAnnotation"]["cropHints"]
color_score = np.asarray([x["score"] for x in file_colors] ).mean()
pixel_frac = np.asarray([x["pixelFraction"] for x in file_colors] ).mean()
crop_conf = np.asarray([x["confidence"] for x in file_crops] ).mean()
if "importanceFraction" in file_crops[0].keys() :
crop_importance = np.asarray(
 [x["importanceFraction"] for x in file_crops] ).mean()
else:
crop_importance = np.nan
metadata = {
"annot_score": file_mean_score,
"color_score": color_score,
"pixel_frac": pixel_frac,
"crop_conf": crop_conf,
"crop_importance": crop_importance,
"desc": file_desc
}
return metadata
def additinal_features_per_id(pet_id, sentiment_path: Path, meta_path: Path):
sentiment_path = sentiment_path / f"{pet_id}.json"
try:
sentiment = parse_sentiment_file(sentiment_path)
 sentiment["pet_id"] = pet_id
except FileNotFoundError:
sentiment = {}
meta_files = sorted(meta_path.glob(f"{pet_id}*.json"))
metadata_list = []
if len(meta_files)> 0:
for f in meta_files:
metadata = parse_metadata(f)
 metadata["pet_id"] = pet_id
metadata_list.append(metadata)
 return sentiment, metadata_list
def load_additional_features(ped_ids: list, sentiment_path: Path,
meta_path: Path):
features = Parallel(
 n_jobs=-1, verbose=1 )(
 delayed(additinal_features_per_id )(i, sentiment_path, meta_path)
 for i in ped_ids)
 sentiments = [x[0] for x in features if len(x[0])> 0]
metadatas = [x[1] for x in features if len(x[1])> 0]
sentiment_keys = sentiments[0].keys()
metadata_keys = metadatas[0][0].keys()
sentiment_dict = {}
metadata_dict = {}
for key in sentiment_keys:
sentiment_dict[key] = [x[key] for x in sentiments]
for key in metadata_keys:
meta_list = []
for meta_per_pid in metadatas:
meta_list += [meta[key] for meta in meta_per_pid]
metadata_dict[key] = meta_list
sentiment_df = pd.DataFrame(sentiment_dict)
 metadata_df = pd.DataFrame(metadata_dict)
 return sentiment_df, metadata_df
def aggregate_metadata(metadata_df: pd.DataFrame,
aggregates=["sum", "mean", "var"]):
meta_desc: pd.DataFrame = metadata_df.groupby(["pet_id"])["desc"].unique()
meta_desc = meta_desc.reset_index()
meta_desc["desc"] = meta_desc["desc"].apply(lambda x: " ".join(x))
meta_gr: pd.DataFrame = metadata_df.drop(["desc"], axis=1)
 for i in meta_gr.columns:
if "pet_id" not in i:
meta_gr[i] = meta_gr[i].astype(float)
 meta_gr = meta_gr.groupby(["pet_id"] ).agg(aggregates)
 meta_gr.columns = pd.Index(
 [f"{c[0]}_{c[1].upper() }" for c in meta_gr.columns.tolist() ])
 meta_gr = meta_gr.reset_index()
return meta_gr, meta_desc
def aggregate_sentiment(sentiment_df: pd.DataFrame, aggregates=["sum"]):
sentiment_desc: pd.DataFrame = sentiment_df.groupby(
 ["pet_id"])["entity"].unique()
sentiment_desc = sentiment_desc.reset_index()
sentiment_desc["entity"] = sentiment_desc["entity"].apply(
 lambda x: " ".join(x))
sentiment_lang = sentiment_df.groupby(
 ["pet_id"])["language"].unique()
sentiment_lang = sentiment_lang.reset_index()
sentiment_lang["language"] = sentiment_lang["language"].apply(
 lambda x: " ".join(x))
sentiment_desc = sentiment_desc.merge(
 sentiment_lang, how="left", on="pet_id")
 sentiment_gr: pd.DataFrame = sentiment_df.drop(["entity", "language"],
axis=1)
 for i in sentiment_gr.columns:
if "pet_id" not in i:
sentiment_gr[i] = sentiment_gr[i].astype(float)
 sentiment_gr = sentiment_gr.groupby(["pet_id"] ).agg(aggregates)
 sentiment_gr.columns = pd.Index(
 [f"{c[0]}" for c in sentiment_gr.columns.tolist() ])
 sentiment_gr = sentiment_gr.reset_index()
return sentiment_gr, sentiment_desc<load_from_csv>
svc = SVC(C=100, gamma=0.01)
 scores_rfc = cross_val_score(svc, X_train_scaled, y_train_scaled, cv=10, scoring='accuracy')
 print("Mean score for 10 folds is ",scores_rfc.mean() *100 )
input_dir = Path(".. /input/petfinder-adoption-prediction/")
 train = pd.read_csv(input_dir / "train/train.csv")
 test = pd.read_csv(input_dir / "test/test.csv")
 sample_submission = pd.read_csv(input_dir / "test/sample_submission.csv" )<define_variables>
def get_best_score(model):
print(model.best_score_)
 print(model.best_params_)
 print(model.best_estimator_)
 return model.best_score_
sp_train = input_dir / Path("train_sentiment/")
 mp_train = input_dir / Path("train_metadata/")
 sp_test = input_dir / Path("test_sentiment/")
 mp_test = input_dir / Path("test_metadata/" )<feature_engineering>
param_grid = {'C': [0.1,10, 100, 1000,5000], 'gamma': [1,0.1,0.01,0.001,0.0001], 'kernel': ['rbf']}
best_svc = GridSearchCV(SVC() , param_grid, cv=10, refit=True, verbose=0)
 best_svc.fit(X_train_scaled,y_train_scaled )
<categorify><EOS> 
pred_all_svc = best_svc.predict(X_test_scaled)
 sub_svc = pd.DataFrame()
sub_svc['PassengerId'] = passengerid
sub_svc['Survived'] = pred_all_svc
sub_svc.to_csv('bear_grylls.csv',index=False )