Kaggle competition: Airbnb New User Bookings
<SOS> metric: NDCG@{K} Kaggle data source: airbnb-recruiting-new-user-bookings<choose_model_class>
%matplotlib inline
warnings.filterwarnings('ignore')
%%time
bert_layer = hub.KerasLayer('https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1', trainable=True )<count_unique_values>
sessions=pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/sessions.csv.zip')
 countries=pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/countries.csv.zip')
 age_gender=pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/age_gender_bkts.csv.zip')
 submission=pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/sample_submission_NDF.csv.zip' )
K = 3
skf = StratifiedKFold(n_splits=K, random_state=SEED, shuffle=True)
 DISASTER = df_train['target'] == 1
print('Whole Training Set Shape = {}'.format(df_train.shape))
print('Whole Training Set Unique keyword Count = {}'.format(df_train['keyword'].nunique()))
print('Whole Training Set Target Rate(Disaster){}/{}(Not Disaster)'.format(df_train[DISASTER]['target'].count() , df_train[~DISASTER]['target'].count()))
for fold,(trn_idx, val_idx)in enumerate(skf.split(df_train['text'], df_train['target']), 1):
print(f'trn_idx {trn_idx} val_idx {val_idx}')
 print('
Fold {} Training Set Shape = {} - Validation Set Shape = {}'.format(fold, df_train.loc[trn_idx, 'text'].shape, df_train.loc[val_idx, 'text'].shape))
print('Fold {} Training Set Unique keyword Count = {} - Validation Set Unique keyword Count = {}'.format(fold, df_train.loc[trn_idx, 'keyword'].nunique() , df_train.loc[val_idx, 'keyword'].nunique()))<choose_model_class>
train= pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/train_users_2.csv.zip')
 test = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/test_users.csv.zip')
class DisasterDetector:
def __init__(self, bert_layer, max_seq_length=128, lr=0.0001, epochs=15, batch_size=32):
self.bert_layer = bert_layer
self.max_seq_length = max_seq_length
vocab_file = self.bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = self.bert_layer.resolved_object.do_lower_case.numpy()
self.tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case)
 self.lr = lr
self.epochs = epochs
self.batch_size = batch_size
self.scores = {}
self.model = self.build_model()
self.model.summary()
def encode(self, texts):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = self.tokenizer.tokenize(text)
 text = text[:self.max_seq_length - 2]
input_sequence = ['[CLS]'] + text + ['[SEP]']
pad_len = self.max_seq_length - len(input_sequence)
 tokens = self.tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * self.max_seq_length
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments)
 def build_model(self):
input_word_ids = Input(shape=(self.max_seq_length,), dtype=tf.int32, name='input_word_ids')
 input_mask = Input(shape=(self.max_seq_length,), dtype=tf.int32, name='input_mask')
 segment_ids = Input(shape=(self.max_seq_length,), dtype=tf.int32, name='segment_ids')
 pooled_output, sequence_output = self.bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 optimizer = SGD(learning_rate=self.lr, momentum=0.8)
 model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 return model
def train(self, X):
for fold,(trn_idx, val_idx)in enumerate(skf.split(X['text'], X['target'])) :
print('
Fold {}
'.format(fold))
X_trn_encoded = self.encode(X.loc[trn_idx, 'text'].str.lower())
 y_trn = X.loc[trn_idx, 'target']
X_val_encoded = self.encode(X.loc[val_idx, 'text'].str.lower())
 y_val = X.loc[val_idx, 'target']
metrics = ClassificationReport(train_data=(X_trn_encoded, y_trn), validation_data=(X_val_encoded, y_val))
self.model.fit(X_trn_encoded, y_trn, validation_data=(X_val_encoded, y_val), callbacks=[metrics], epochs=self.epochs, batch_size=self.batch_size)
 self.scores[fold] = {
'train': {
'precision': metrics.train_precision_scores,
'recall': metrics.train_recall_scores,
'f1': metrics.train_f1_scores
},
'validation': {
'precision': metrics.val_precision_scores,
'recall': metrics.val_recall_scores,
'f1': metrics.val_f1_scores
}
}
def predict(self, X):
X_test_encoded = self.encode(X['text'].str.lower())
 y_pred = self.model.predict(X_test_encoded)
 return y_pred<train_model>
countries.sort_values(by='distance_km' )
clf = DisasterDetector(bert_layer, max_seq_length=128, lr=0.0001, epochs=10, batch_size=32)
 clf.train(df_train )<save_to_csv>
a=train['country_destination'].value_counts()
a=a.drop(['NDF','other'],axis=0)
 df_value_counts = pd.DataFrame(a)
 df_value_counts = a.reset_index()
df_value_counts.columns = ['country_destination', 'value_counts']
mapdata= pd.merge(df_value_counts, countries,how= 'inner' , on='country_destination')
 mapdata['poppercent']=mapdata['value_counts']/mapdata['value_counts'].sum()
mapdata['text']=['United States', 'France', 'Italy','United Kingdom', 'Spanish','Canada' ,'German', 'Dutch','Australia', 'Brazil' ]
y_pred = clf.predict(df_test)
 model_submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
 model_submission['target'] = np.round(y_pred ).astype('int')
 model_submission.to_csv('model_submission.csv', index=False)
 model_submission.describe()<train_model>
a=train.groupby('country_destination' ).count().sort_values(by='id',ascending=True)
 a.id
output_dir = 'cp.ckpt'
print("Saving model to %s" % output_dir)
 model_to_save = clf.model
model_to_save.save_weights(output_dir)
<import_modules>
age_gender.drop(age_gender.index[0],inplace=True)
 age_gender
import os
import pandas as pd
import shutil
from eda import *
from csv2json import *<load_from_csv>
agebtw0n19= age_gender[(age_gender.age_bucket=='0-4')|(age_gender.age_bucket=='5-9')|(age_gender.age_bucket=='10-14')|(age_gender.age_bucket=='15-19')]
agebtw49n34= age_gender[(age_gender.age_bucket=='45-49')|(age_gender.age_bucket=='40-44')|(age_gender.age_bucket=='35-39')|(age_gender.age_bucket=='30-34')]
agebtw99n50= age_gender[(age_gender.age_bucket=='95-99')|(age_gender.age_bucket=='90-94')|(age_gender.age_bucket=='85-89')|(age_gender.age_bucket=='80-84')|(age_gender.age_bucket=='75-79')|(age_gender.age_bucket=='70-74')|(age_gender.age_bucket=='70-74')|(age_gender.age_bucket=='65-69')|(age_gender.age_bucket=='60-64')|(age_gender.age_bucket=='55-59')|(age_gender.age_bucket=='50-54')]
agebtw20n29= age_gender[(age_gender.age_bucket=='25-29')|(age_gender.age_bucket=='20-24')]
sample_submission = pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
 train_data = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test_data = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv' )<categorify>
actionanditsdetail=pd.crosstab(age_gender.age_bucket, age_gender.country_destination,margins=False)
 stat, p, dof, expected = chi2_contingency(actionanditsdetail)
 alpha = 0.05
print("p value is " + str(p))
if p <= alpha:
	print('Dependent(reject H0)')
 else:
	print('Independent(H0 holds true)' )
train_data = prepare_data(train_data)
 test_data = prepare_data(test_data )<define_variables>
actionanditsdetail=pd.crosstab(age_gender.country_destination, age_gender.gender,margins=False)
 stat, p, dof, expected = chi2_contingency(actionanditsdetail)
 alpha = 0.05
print("p value is " + str(p))
if p <= alpha:
	print('Dependent(reject H0)')
 else:
	print('Independent(H0 holds true)')
create_sample_no_lambdas('/kaggle/working', train_data, 'train')
 create_sample_no_lambdas('/kaggle/working', test_data, 'test')
 create_sample_no_lambdas('/kaggle/working', test_data, 'dev' )<load_pretrained>
sessions=sessions.sort_values('user_id')
 sessions.rename(columns={"user_id": "id"},inplace=True)
 sessions.sort_values(by='id',inplace=True)
 sessions = sessions[(sessions['secs_elapsed'].notnull())&(sessions['secs_elapsed'] > 0.0)]
sessions[(sessions['action_detail']=='booking')]
if os.path.exists('/kaggle/working/results'):
shutil.rmtree('/kaggle/working/results')
 !python run_classifier_TABSA.py \
--task_name rambler2011_json_NLI_M \
--data_dir /kaggle/working/ \
--vocab_file '/kaggle/input/my-practices-for-nlp-real-or-not/NLPRealOrNot/conversational_cased_L-12_H-768_A-12_v1/vocab.txt' \
--bert_config_file '/kaggle/input/my-practices-for-nlp-real-or-not/NLPRealOrNot/conversational_cased_L-12_H-768_A-12_v1/bert_config.json' \
--init_checkpoint '/kaggle/input/my-practices-for-nlp-real-or-not/NLPRealOrNot/conversational_cased_L-12_H-768_A-12_v1/pytorch_model.bin' \
--eval_test \
--do_lower_case \
--max_seq_length 512 \
--train_batch_size 12 \
--learning_rate 2e-5 \
--num_train_epochs 1.0 \
--output_dir /kaggle/working/results/ \
--seed 42<load_from_csv>
groupedid=pd.DataFrame(sessions.groupby(['id'])['secs_elapsed'].sum())
 groupedid['hour_elapsed']=groupedid['secs_elapsed'].div(3600 ).round(decimals=0)
 groupedid.drop('secs_elapsed',axis=1,inplace=True)
 groupedid.reset_index(inplace=True)
 groupedid[(groupedid['id'] == '6udv3scuxe')|(groupedid['id'] == 'yxf0sm9sbw')|(groupedid['id'] == 'nttj7g9av6')]
data = pd.read_csv('/kaggle/working/test_NLI_M.tsv', sep='\t')
 results = []
with open('/kaggle/working/results/test_ep_1.txt')as f:
for line in f:
results.append(int(line.split() [0]))<feature_engineering>
actionndevicetype=pd.crosstab(sessions.action, sessions.device_type,margins=False)
 stat, p, dof, expected = chi2_contingency(actionndevicetype)
 alpha = 0.05
print("p value is " + str(p))
if p <= alpha:
	print('Dependent(reject H0)')
 else:
	print('Independent(H0 holds true)' )
data['answer'] = results<merge>
print(train.country_destination.value_counts())
 sns.countplot(train.country_destination )
sample_submission = sample_submission.set_index('id' ).join(data.set_index('tonal_word')) ['answer']<rename_columns>
train= pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/train_users_2.csv.zip')
 test = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/test_users.csv.zip')
sample_submission = sample_submission.rename(columns={"id": "id", "answer": "target"} )<categorify>
total = train.isnull().sum().sort_values(ascending=False)
 percent =(train.isnull().sum() /train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )
encoder = {1:0, 0:1}
sample_submission['target'] = sample_submission['target'].map(encoder )<save_to_csv>
train['first_affiliate_tracked'] = train['first_affiliate_tracked'].fillna('Unknown')
 test['first_affiliate_tracked'] = test['first_affiliate_tracked'].fillna('Unknown' )
sample_submission.to_csv('/kaggle/working/submission.csv', index=False )<load_from_url>
train[(train.age < 14)&(train.age > 110)]=np.nan
test[(test.age < 14)&(test.age > 110)]=np.nan
train['age'].fillna(train['age'].mean() , inplace=True)
 test['age'].fillna(train['age'].mean() , inplace=True )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<set_options>
categorical_features = [
'affiliate_channel',
'affiliate_provider',
'first_affiliate_tracked',
'first_browser',
'first_device_type',
'gender',
'language',
'signup_app',
'signup_method',
'signup_flow'
]
for categorical_feature in categorical_features:
train[categorical_feature] = train[categorical_feature].astype('category')
 for categorical_feature in categorical_features:
test[categorical_feature] = test[categorical_feature].astype('category' )
pd.set_option("display.max_colwidth", 200)
 warnings.filterwarnings("ignore", category=DeprecationWarning)
 %matplotlib inline<load_from_csv>
train['date_account_created'] = pd.to_datetime(train['date_account_created'])
 train['date_first_booking'] = pd.to_datetime(train['date_first_booking'])
 train['timestamp_first_active'] = pd.to_datetime(train['timestamp_first_active'], format='%Y%m%d%H%M%S')
 train['timestamp_first_active'] = pd.to_datetime(train['timestamp_first_active'] ).dt.date
test['date_account_created'] = pd.to_datetime(test['date_account_created'])
 test['timestamp_first_active'] = pd.to_datetime(test['timestamp_first_active'], format='%Y%m%d%H%M%S')
 test['timestamp_first_active'] = pd.to_datetime(test['timestamp_first_active'] ).dt.date
train = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test = pd.read_csv('.. /input/nlp-getting-started/test.csv' )<count_values>
train.drop(['date_first_booking'], axis=1,inplace=True)
 test.drop(['date_first_booking'], axis=1,inplace=True )
train['target'].value_counts()<feature_engineering>
dac = np.vstack(train.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 train['dac_year'] = dac[:,0]
train['dac_month'] = dac[:,1]
train['dac_day'] = dac[:,2]
train.drop(['date_account_created'], axis=1,inplace=True)
 tfa = np.vstack(train.timestamp_first_active.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 train['tfa_year'] = tfa[:,0]
train['tfa_month'] = tfa[:,1]
train['tfa_day'] = tfa[:,2]
train.drop(['timestamp_first_active'], axis=1,inplace=True)
 dac = np.vstack(test.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 test['dac_year'] = dac[:,0]
test['dac_month'] = dac[:,1]
test['dac_day'] = dac[:,2]
test.drop(['date_account_created'], axis=1,inplace=True)
 tfa = np.vstack(test.timestamp_first_active.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 test['tfa_year'] = tfa[:,0]
test['tfa_month'] = tfa[:,1]
test['tfa_day'] = tfa[:,2]
test.drop(['timestamp_first_active'], axis=1,inplace=True )
def remove_pattern(input_txt, pattern):
r = re.findall(pattern, input_txt)
 for i in r:
input_txt = re.sub(i, '', input_txt)
 return input_txt
train['tweet'] = np.vectorize(remove_pattern )(train['text'], "
test['tweet'] = np.vectorize(remove_pattern )(test['text'], "
train.head()<feature_engineering>
le = LabelEncoder()
train['gender']= le.fit_transform(train['gender'])
 train['signup_method']= le.fit_transform(train['signup_method'])
 train['first_affiliate_tracked']= le.fit_transform(train['first_affiliate_tracked'])
 train['signup_method']= le.fit_transform(train['signup_method'])
 train['language']= le.fit_transform(train['language'])
 train['affiliate_channel']= le.fit_transform(train['affiliate_channel'])
 train['affiliate_provider']= le.fit_transform(train['affiliate_provider'])
 train['signup_app']= le.fit_transform(train['signup_app'])
 train['first_device_type']= le.fit_transform(train['first_device_type'])
 train['first_browser']= le.fit_transform(train['first_browser'])
 train['signup_flow']= le.fit_transform(train['signup_flow'] )
train['tweet'] = train['tweet'].str.replace("[^a-zA-Z
test['tweet'] = test['tweet'].str.replace("[^a-zA-Z
train.head()<feature_engineering>
le = LabelEncoder()
test['gender']= le.fit_transform(test['gender'])
 test['signup_method']= le.fit_transform(test['signup_method'])
 test['first_affiliate_tracked']= le.fit_transform(test['first_affiliate_tracked'])
 test['signup_method']= le.fit_transform(test['signup_method'])
 test['language']= le.fit_transform(test['language'])
 test['affiliate_channel']= le.fit_transform(test['affiliate_channel'])
 test['affiliate_provider']= le.fit_transform(test['affiliate_provider'])
 test['signup_app']= le.fit_transform(test['signup_app'])
 test['first_device_type']= le.fit_transform(test['first_device_type'])
 test['first_browser']= le.fit_transform(test['first_browser'])
 test['signup_flow']= le.fit_transform(test['signup_flow'] )
train['tweet'] = train['tweet'].apply(lambda x: ' '.join([w for w in x.split() if len(w)>3]))
test['tweet'] = test['tweet'].apply(lambda x: ' '.join([w for w in x.split() if len(w)>3]))
train.head()<data_type_conversions>
train.country_destination.replace('NDF',0,inplace=True)
 train.country_destination.replace('US',1,inplace=True)
 train.country_destination.replace('other',2,inplace=True)
 train.country_destination.replace('FR',3,inplace=True)
 train.country_destination.replace('CA',4,inplace=True)
 train.country_destination.replace('GB',5,inplace=True)
 train.country_destination.replace('ES',6,inplace=True)
 train.country_destination.replace('IT',7,inplace=True)
 train.country_destination.replace('PT',8,inplace=True)
 train.country_destination.replace('NL',9,inplace=True)
 train.country_destination.replace('DE',10,inplace=True)
 train.country_destination.replace('AU',11,inplace=True )
train['tweet'] = train['tweet'].str.lower()
test['tweet'] = test['tweet'].str.lower()<feature_engineering>
y=train['country_destination']
X=train.drop(['country_destination','id'],axis=1)
 smotetomek = SMOTETomek(sampling_strategy='auto')
 X_train1, X_test1, y_train1, y_test1 = train_test_split(X, y, test_size=0.30, random_state=1,shuffle=True,stratify=y)
 X_train, y_train = smotetomek.fit_resample(X_train1, y_train1 )
set(stopwords.words('english'))
stops = set(stopwords.words('english'))
train['tokenized_sents'] = train.apply(lambda row: nltk.word_tokenize(row['tweet']), axis=1)
 test['tokenized_sents'] = test.apply(lambda row: nltk.word_tokenize(row['tweet']), axis=1)
 def remove_stops(row):
my_list = row['tokenized_sents']
meaningful_words = [w for w in my_list if not w in stops]
return(meaningful_words)
 train['clean_tweet'] = train.apply(remove_stops, axis=1)
 test['clean_tweet'] = test.apply(remove_stops, axis=1)
 train.drop(["tweet","tokenized_sents"], axis = 1, inplace = True)
 test.drop(["tweet","tokenized_sents"], axis = 1, inplace = True )<string_transform>
x=pd.DataFrame(X_train)
 Y=pd.DataFrame(y_train)
 result = pd.concat([x, Y], axis=1, join='inner')
 sns.countplot(result.country_destination )
def rejoin_words(row):
my_list = row['clean_tweet']
joined_words =(" ".join(my_list))
return joined_words
train['clean_tweet'] = train.apply(rejoin_words, axis=1)
 test['clean_tweet'] = test.apply(rejoin_words, axis=1)
 train.head()<drop_column>
target_names = ['NDF', 'US', 'other', 'FR', 'CA', 'GB', 'ES', 'IT', 'PT', 'NL','DE', 'AU']
rf=RandomForestClassifier()
rf.fit(X_train,y_train)
 y_predrf=rf.predict(X_test1)
 print(classification_report(y_test1, y_predrf, target_names=target_names))
train.drop(["keyword","location","text"], axis = 1, inplace = True)
 test.drop(["keyword","location","text"], axis = 1, inplace = True)
 train.head()<load_pretrained>
feature_names=list(X_train1.columns.values.tolist() )
tokenizer_QA = BertTokenizer.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')
<categorify>
pred_country={0:"NDF", 1:"US", 2:"other", 3:"FR", 4:"CA", 5:"GB", 6:"ES", 7:"IT", 8:"PT", 9:"DE", 10:"NL", 11:"AU"}
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer_QA.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer_QA.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments)
 def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
xgb = XGBClassifier()
xgb.fit(X_train,y_train)
 y_predxgb=xgb.predict(X_test1)
 print(classification_report(y_test1, y_predxgb, target_names=target_names))
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<data_type_conversions>
predictionsxgb=xgb.predict(test.drop(['id'],axis=1))
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy() 
<categorify>
resultsxgb=[]
for i in predictionsxgb:
resultsxgb.append(pred_country[i] )
train_input = bert_encode(train.clean_tweet.values, tokenizer_QA, max_len=160)
 test_input = bert_encode(test.clean_tweet.values, tokenizer_QA, max_len=160)
 train_labels = train.target.values<train_model>
mlp = MLPClassifier(activation='identity', solver='sgd',learning_rate='adaptive', alpha=0.0001, batch_size='auto')
 mlp.fit(X_train,y_train)
 predsmlp = mlp.predict(X_test1)
 print(classification_report(y_test1, predsmlp, target_names=target_names))
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=3,
callbacks=[checkpoint],
batch_size=16
)<predict_on_test>
parameters = {'solver': ['lbfgs','sgd'], 'max_iter': [1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000 ], 'alpha': 10.0 ** -np.arange(1, 10), 'hidden_layer_sizes':np.arange(10, 15), 'random_state':[0,1,2,3,4,5,6,7,8,9]}
mlpgridsearch = GridSearchCV(MLPClassifier() , parameters, n_jobs=-1)
 mlpgridsearch.fit(X_train,y_train)
 predsgridmlp = mlpgridsearch.predict(X_test1)
 print(classification_report(y_test1, predsgridmlp, target_names=target_names))
model.load_weights('model.h5')
 test_pred = model.predict(test_input )<save_to_csv>
print(classification_report(y_test1, y_predcnb, target_names=target_names))
submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 submission['target'] = test_pred.round().astype(int)
 submission.to_csv('new_submission.csv', index=False )<load_from_url>
dac = np.vstack(train.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)train['dac_year'] = dac[:,0] train['dac_month'] = dac[:,1] train['dac_day'] = dac[:,2] train.drop(['date_account_created'], axis=1,inplace=True)
 dac = np.vstack(test.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)test['dac_year'] = dac[:,0] test['dac_month'] = dac[:,1] test['dac_day'] = dac[:,2] test.drop(['date_account_created'], axis=1,inplace=True)
 test[(test.age < 14)&(test.age > 110)]=np.nan test['age'].fillna(train['age'].mean() , inplace=True)
 for categorical_feature in categorical_features: train[categorical_feature] = train[categorical_feature].astype('category')for categorical_feature in categorical_features: test[categorical_feature] = test[categorical_feature].astype('category')
 transform the dataset
smotetomek = SMOTETomek(sampling_strategy='auto')
 split the dataset into train and test sets
X_train1, X_test1, y_train1, y_test1 = train_test_split(X, y, test_size=0.30, random_state=1,shuffle=True)X_train, y_train = smotetomek.fit_resample(X_train1, y_train1)
 xgb = XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bynode=1, colsample_bytree=0.5, gamma=0, gpu_id=-1, importance_type='gain', interaction_constraints=None, learning_rate=0.3, max_delta_step=0, max_depth=6, min_child_weight=1, monotone_constraints=None, n_estimators=25, n_jobs=0, num_parallel_tree=1, objective='multi:softprob', random_state=0, reg_alpha=0, reg_lambda=1, scale_pos_weight=None, seed=0, subsample=0.5, tree_method=None, validate_parameters=False, verbosity=None)xgb.fit(X_train, y_train)target_names = ['NDF', 'US', 'other', 'FR', 'CA', 'GB', 'ES', 'IT', 'PT', 'NL','DE', 'AU'] y_predxgb=xgb.predict(X_test1)