Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<categorify>
!pip install transformers==3.0.2
def decay_4_first_10_then_1_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
if j<10:
arr[j]=1.+(max(1,array[j])-1.) /4.
 else :
arr[j]=1.
 return arr
	
def decay_16_first_10_then_1_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
if j<10:
arr[j]=1.+(max(1,array[j])-1.) /16.
 else :
arr[j]=1.
 return arr	
def decay_2_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
arr[j]=1.+(max(1,array[j])-1.) /2.
 return arr
def decay_4_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
arr[j]=1.+(max(1,array[j])-1.) /4.
 return arr 	
	
def acceleratorx2_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
arr[j]=1.+(max(1,array[j])-1.) *2.
 return arr
def decay_1_5_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
arr[j]=1.+(max(1,array[j])-1.) /1.5
return arr
def stay_same_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
arr[j]=1.
 return arr
def decay_2_last_12_linear_inter_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
arr[j]=1.+(max(1,array[j])-1.) /2.
 arr12=(max(1,arr[-12])-1.) /12.
 for j in range(0, 12):
arr[len(arr)-12 +j]= max(1, 1 +(( arr12*12)-(j+1)*arr12))
return arr
def decay_4_last_12_linear_inter_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
arr[j]=1.+(max(1,array[j])-1.) /4.
 arr12=(max(1,arr[-12])-1.) /12.
 for j in range(0, 12):
arr[len(arr)-12 +j]= max(1, 1 +(( arr12*12)-(j+1)*arr12))
return arr
def linear_last_12_f(array):
arr=[1.0 for k in range(len(array)) ]
for j in range(len(array)) :
arr[j]=max(1,array[j])
 arr12=(max(1,arr[-12])-1.) /12.
 for j in range(0, 12):
arr[len(arr)-12 +j]= max(1, 1 +(( arr12*12)-(j+1)*arr12))
return arr
decay_4_first_10_then_1 =[ "Heilongjiang_China","Liaoning_China","Shanghai_China"]
decay_4_first_10_then_1_fatality=[]
decay_16_first_10_then_1 =["Beijing_China","Fujian_China","Guangdong_China","Shandong_China","Sichuan_China","Zhejiang_China"]
decay_16_first_10_then_1_fatality=[]
decay_4=["nan_Bhutan","nan_Burundi","nan_Cabo Verde","Prince Edward Island_Canada",
"nan_Central African Republic","Inner Mongolia_China","nan_Maldives"]
decay_4_fatality=["nan_Congo(Kinshasa)"]
decay_2 =["nan_Congo(Kinshasa)","Faroe Islands_Denmark","nan_Eritrea","French Guiana_France","nan_Korea, South","nan_MS Zaandam"]
decay_2_fatality=[]
stay_same=["nan_Diamond Princess","nan_Timor-Leste"]
		
stay_same_fatality=["Beijing_China","Fujian_China","Guangdong_China","Shandong_China",
"Sichuan_China","Zhejiang_China", "Heilongjiang_China","Liaoning_China","Shanghai_China"]
normal=[]
normal_fatality=["nan_Korea, South","New York_US"]
decay_4_last_12_linear_inter =[ "Greenland_Denmark","nan_Dominica","nan_Equatorial Guinea","nan_Eswatini","New Caledonia_France",
"Saint Barthelemy_France","St Martin_France","nan_Gambia","nan_Grenada","nan_Holy See","nan_Mauritania","nan_Namibia","nan_Nicaragua"
,"nan_Papua New Guinea","nan_Saint Lucia","nan_Saint Vincent and the Grenadines","nan_Seychelles","nan_Sierra Leone","nan_Somalia","nan_Suriname",
"Anguilla_United Kingdom","British Virgin Islands_United Kingdom","Montserrat_United Kingdom","Turks and Caicos Islands_United Kingdom","nan_Zimbabwe"
, "Hong Kong_China","Curacao_Netherlands"]
decay_4_last_12_linear_inter_fatality=[]
decay_2_last_12_linear_inter =[ "nan_Chad",
"nan_Congo(Brazzaville)","nan_Fiji","French Polynesia_France","nan_Gabon",
"nan_Guyana","nan_Laos","nan_Nepal","Sint Maarten_Netherlands",
"nan_Saint Kitts and Nevis","nan_Sudan","nan_Syria","nan_Tanzania",
"Bermuda_United Kingdom","Cayman Islands_United Kingdom","nan_Zambia","Northwest Territories_Canada","Yukon_Canada"
,"nan_Mongolia","nan_Uganda"]
decay_2_last_12_linear_inter_fatality=[]
acceleratorx2=[]
acceleratorx2_fatality=[]
warm_st=['nan_Angola','nan_Antigua and Barbuda','Northern Territory_Australia','nan_Bahamas',
'nan_Bangladesh','nan_Belize','nan_Benin','nan_Botswana','nan_Burundi','nan_Cabo Verde','nan_Cameroon',
'nan_Central African Republic','nan_Chad','Hong Kong_China',"nan_Cote d'Ivoire",'nan_Cuba','Greenland_Denmark',
'nan_Dominica','nan_Equatorial Guinea','nan_Eritrea','nan_Eswatini','nan_Fiji','French Polynesia_France','New Caledonia_France',
'Saint Barthelemy_France','St Martin_France','nan_Gabon','nan_Gambia','nan_Grenada','nan_Guyana','nan_Haiti','nan_Holy See',
'nan_Honduras','nan_Ireland','nan_Korea, South','nan_Laos','nan_Liberia','nan_Libya','nan_Maldives','nan_Mali',
'nan_Mauritania','nan_Mauritius','nan_Mongolia','nan_Mozambique','nan_Namibia','nan_Nepal','Aruba_Netherlands',
'nan_Nicaragua','nan_Niger','nan_Papua New Guinea','nan_Saint Kitts and Nevis','nan_Saint Lucia',
'nan_Saint Vincent and the Grenadines','nan_Seychelles','nan_Sierra Leone','nan_Somalia',
'nan_Spain','nan_Sudan','nan_Suriname','nan_Syria','nan_Tanzania','nan_Togo','nan_Uganda','Anguilla_United Kingdom',
'Bermuda_United Kingdom','British Virgin Islands_United Kingdom','Channel Islands_United Kingdom',
'Gibraltar_United Kingdom','Isle of Man_United Kingdom','Montserrat_United Kingdom','nan_United Kingdom',
'Turks and Caicos Islands_United Kingdom','nan_Uzbekistan','nan_Zimbabwe',
]
decay_1_5 =["nan_Angola" ,"nan_Antigua and Barbuda","Montana_US","Nebraska_US","nan_Bangladesh","Illinois_US"
,"Northern Territory_Australia","nan_Bahamas","nan_Bahrain","nan_Barbados" ,"nan_Belize","nan_Benin",
	"nan_Botswana","nan_Brunei","Manitoba_Canada","New Brunswick_Canada","Saskatchewan_Canada",
	"nan_Cote d'Ivoire","nan_France","nan_Guinea-Bissau","nan_Haiti","nan_Italy","nan_Libya","nan_Malta","nan_Mauritius",
	"Aruba_Netherlands","nan_Niger","nan_Spain","nan_Togo","Guam_US","Iowa_US","Idaho_US","Connecticut_US","California_US","New York_US","Virgin Islands_US",
	"Channel Islands_United Kingdom","Gibraltar_United Kingdom","Isle of Man_United Kingdom","nan_United Kingdom",'nan_Burma']
decay_1_5_fatality=["nan_Cameroon","nan_Mali","nan_Cuba","Delaware_US","District of Columbia_US",
"Kansas_US","Louisiana_US","Michigan_US","New Mexico_US","Ohio_US","Oklahoma_US","Pennsylvania_US","Puerto Rico_US","Rhode Island_US",
"South Dakota_US" ,"Tennessee_US","Texas_US","Vermont_US","Virginia_US","West Virginia_US","nan_Uzbekistan"]
linear_last_12=["nan_Honduras","nan_Ireland","Colorado_US","nan_Liberia","nan_Mozambique"]
linear_last_12_fatality=[]
tr_frame=train_frame
features_train=tr_frame[names].values
standard_confirmed_train=tr_frame["ConfirmedCases"].values
standard_fatalities_train=tr_frame["Fatalities"].values
current_confirmed_train=tr_frame["ConfirmedCases"].values
features_cv=[]
name_cv=[]
standard_confirmed_cv=[]
standard_fatalities_cv=[]
names_=tr_frame["key"].values
training_horizon=int(features_train.shape[0]/len(unique_keys))
print("training horizon = ",training_horizon)
 for dd in range(training_horizon-1,features_train.shape[0],training_horizon):
features_cv.append(features_train[dd])
 name_cv.append(names_[dd])
 standard_confirmed_cv.append(standard_confirmed_train[dd])
 standard_fatalities_cv.append(standard_fatalities_train[dd])
 print(name_cv[-1], standard_confirmed_cv[-1], standard_fatalities_cv[-1])
 features_cv=np.array(features_cv)
 preds_confirmed_cv=np.zeros(( features_cv.shape[0],horizon))
preds_confirmed_standard_cv=np.zeros(( features_cv.shape[0],horizon))
preds_fatalities_cv=np.zeros(( features_cv.shape[0],horizon))
preds_fatalities_standard_cv=np.zeros(( features_cv.shape[0],horizon))
overal_rmsle_metric_confirmed=0.0
for j in range(preds_confirmed_cv.shape[1]):
this_features_cv=features_cv
preds=predict(features_cv, input_name=model_directory +"confirmed"+ str(j))
preds_confirmed_cv[:,j]=preds
print(" modelling confirmed, case %d, , original cv %d and after %d "%(j,this_features_cv.shape[0],this_features_cv.shape[0]))
predictions=[]
for ii in range(preds_confirmed_cv.shape[0]):
current_prediction=standard_confirmed_cv[ii]
if current_prediction==0 :
current_prediction=0.1
this_preds=preds_confirmed_cv[ii].tolist()
name=name_cv[ii]
reserve=this_preds[0]
	
if name in normal:
this_preds=this_preds	
	
elif name in decay_4_first_10_then_1:
this_preds=decay_4_first_10_then_1_f(this_preds)
 		
elif name in decay_16_first_10_then_1:
this_preds=decay_16_first_10_then_1_f(this_preds)		
		
elif name in decay_4_last_12_linear_inter:
this_preds=decay_4_last_12_linear_inter_f(this_preds)		
elif name in decay_4:
this_preds=decay_4_f(this_preds)
 elif name in decay_2:
this_preds=decay_2_f(this_preds)
 elif name in decay_2_last_12_linear_inter:
this_preds=decay_2_last_12_linear_inter_f(this_preds)
 elif name in decay_1_5:
this_preds=decay_1_5_f(this_preds)
 elif name in linear_last_12:
this_preds=linear_last_12_f(this_preds)
 elif name in acceleratorx2:
this_preds=acceleratorx2_f(this_preds)
 elif name in stay_same or "China" in name:
this_preds=stay_same_f(this_preds)
 if name in warm_st:
this_preds[0]=reserve
for j in range(preds_confirmed_cv.shape[1]):
current_prediction*=max(1,this_preds[j])
 preds_confirmed_standard_cv[ii][j]=current_prediction
for j in range(preds_confirmed_cv.shape[1]):
this_features_cv=features_cv
preds=predict(features_cv, input_name=model_directory +"fatal"+ str(j))
preds_fatalities_cv[:,j]=preds
print(" modelling fatalities, case %d, original cv %d and after %d "%(j,this_features_cv.shape[0],this_features_cv.shape[0]))
predictions=[]
for ii in range(preds_fatalities_cv.shape[0]):
current_prediction=standard_fatalities_cv[ii]
if current_prediction==0 and standard_confirmed_cv[ii]>400:
current_prediction=0.1
this_preds=preds_fatalities_cv[ii].tolist()
name=name_cv[ii]
reserve=this_preds[0]
	
if name in normal_fatality:
this_preds=this_preds	 	
	
elif name in decay_4_first_10_then_1_fatality:
this_preds=decay_4_first_10_then_1_f(this_preds)
 		
elif name in decay_16_first_10_then_1_fatality:
this_preds=decay_16_first_10_then_1_f(this_preds)
 elif name in decay_4_last_12_linear_inter_fatality:
this_preds=decay_4_last_12_linear_inter_f(this_preds)		
elif name in decay_4_fatality:
this_preds=decay_4_f(this_preds)		
		
elif name in decay_2_fatality:
this_preds=decay_2_f(this_preds)
 elif name in decay_2_last_12_linear_inter_fatality:
this_preds=decay_2_last_12_linear_inter_f(this_preds)
 elif name in decay_1_5_fatality:
this_preds=decay_1_5_f(this_preds)	
elif name in linear_last_12_fatality:
this_preds=linear_last_12_f(this_preds)
 elif name in acceleratorx2_fatality:
this_preds=acceleratorx2_f(this_preds)
 elif name in stay_same_fatality:
this_preds=stay_same_f(this_preds)
 elif name in normal:
this_preds=this_preds	
	
elif name in decay_4_first_10_then_1:
this_preds=decay_4_first_10_then_1_f(this_preds)
 		
elif name in decay_16_first_10_then_1:
this_preds=decay_16_first_10_then_1_f(this_preds)		
elif name in decay_4_last_12_linear_inter:
this_preds=decay_4_last_12_linear_inter_f(this_preds)		
elif name in decay_4:
this_preds=decay_4_f(this_preds)
 		
elif name in decay_2:
this_preds=decay_2_f(this_preds)
 elif name in decay_2_last_12_linear_inter:
this_preds=decay_2_last_12_linear_inter_f(this_preds)
 elif name in decay_1_5:
this_preds=decay_1_5_f(this_preds)
 elif name in linear_last_12:
this_preds=linear_last_12_f(this_preds)
 elif name in acceleratorx2:
this_preds=acceleratorx2_f(this_preds)
 elif name in stay_same or "China" in name:
this_preds=stay_same_f(this_preds)
 if name in warm_st:
this_preds[0]=reserve
for j in range(preds_fatalities_cv.shape[1]):
if current_prediction==0 and(preds_confirmed_standard_cv[ii][j]>400 or "Malta" in name or "Somalia" in name):
current_prediction=1.
 if j==0 and "nan_Antigua and Barbuda" in name:
current_prediction=2.
 if j==0 and 'nan_Burma' in name:
current_prediction=3.
 current_prediction*=max(1,this_preds[j])
 preds_fatalities_standard_cv[ii][j]=current_prediction<categorify>
os.environ["WANDB_API_KEY"] = "0"
key_to_confirmed_rate={}
key_to_fatality_rate={}
key_to_confirmed={}
key_to_fatality={}
print(len(features_cv), len(name_cv),len(standard_confirmed_cv),len(standard_fatalities_cv))
print(preds_confirmed_cv.shape,preds_confirmed_standard_cv.shape,preds_fatalities_cv.shape,preds_fatalities_standard_cv.shape)
 for j in range(len(name_cv)) :
key_to_confirmed_rate[name_cv[j]]=preds_confirmed_cv[j,:].tolist()
key_to_fatality_rate[name_cv[j]]=preds_fatalities_cv[j,:].tolist()
key_to_confirmed[name_cv[j]] =preds_confirmed_standard_cv[j,:].tolist()
key_to_fatality[name_cv[j]]=preds_fatalities_standard_cv[j,:].tolist()<merge>
!pip install nlp
train_new=train[["Date","ConfirmedCases","Fatalities","key","rate_ConfirmedCases","rate_Fatalities"]]
test_new=pd.merge(test,train_new, how="left", left_on=["key","Date"], right_on=["key","Date"] ).reset_index(drop=True)
 test_new<categorify>
transformers.__version__
def fillin_columns(frame,key_column, original_name, training_horizon, test_horizon, unique_values, key_to_values):
keys=frame[key_column].values
original_values=frame[original_name].values.tolist()
print(len(keys), len(original_values), training_horizon ,test_horizon,len(key_to_values))
for j in range(unique_values):
current_index=(j *(training_horizon +test_horizon)) +training_horizon
current_key=keys[current_index]
values=key_to_values[current_key]
co=0
for g in range(current_index, current_index + test_horizon):
original_values[g]=values[co]
co+=1
frame[original_name]=original_values
all_days=int(test_new.shape[0]/len(unique_keys))
tr_horizon=all_days-horizon
print(all_days,tr_horizon, horizon)
 fillin_columns(test_new,"key", 'ConfirmedCases', tr_horizon, horizon, len(unique_keys), key_to_confirmed)
 fillin_columns(test_new,"key", 'Fatalities', tr_horizon, horizon, len(unique_keys), key_to_fatality)
 submission=test_new[["ForecastId","ConfirmedCases","Fatalities"]]
submission.to_csv("submission.csv", index=False )<load_from_csv>
Image.open(".. /input/tf-logo/tf_logo.png" )
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 map_Embarked = {'C':0,'Q':1,'S':2}
train_data['Embarked'] = train_data['Embarked'].map(map_Embarked)
 test_data['Embarked'] = test_data['Embarked'].map(map_Embarked)
 map_Sex = {'male':0,'female':1}
train_data['Sex'] = train_data['Sex'].map(map_Sex)
 test_data['Sex'] = test_data['Sex'].map(map_Sex)
 test_data['Age'].fillna(test_data['Age'].mean() , inplace=True)
 test_data['Fare'].fillna(test_data['Fare'].mean() , inplace=True)
 train_data.dropna(subset=['Pclass','Sex','Age','Fare','Embarked'],inplace=True)
 X_train, X_test, y_train, y_test = train_test_split(train_data[['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']],
train_data['Survived'], test_size=0.1, random_state=0)
 params = {'n_estimators':500, 'max_depth':4, 'min_samples_split': 2,
'learning_rate': 0.01, 'loss': 'ls'}
clf = GradientBoostingRegressor(**params)
 clf.fit(X_train, y_train)
 mse = mean_squared_error(y_test, clf.predict(X_test))
print("MSE: %.4f" % mse)
 test_data['Survived'] = clf.predict(test_data[['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']])
 result = test_data[['PassengerId','Survived']]
result['Survived'] = result['Survived'].apply(lambda x:round(x))
result.to_csv('submission.csv',index=False )<load_from_csv>
Image.open(".. /input/tpu-v38/tpu--sys-arch4 1.png" )
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 combine = [train, test]<feature_engineering>
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 except ValueError:
strategy = tf.distribute.get_strategy()
print('Number of replicas:', strategy.num_replicas_in_sync )
train["Title"] = train["Name"].str.extract('([A-Za-z]+)\.', expand=False)
 test["Title"] = test["Name"].str.extract('([A-Za-z]+)\.', expand=False )<feature_engineering>
train = pd.read_csv(".. /input/contradictory-my-dear-watson/train.csv")
 test = pd.read_csv(".. /input/contradictory-my-dear-watson/test.csv")
 submission = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/sample_submission.csv' )
train['Title'] = train['Title'].replace([ 'Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona', 'Countess', 'Lady', 'Sir'], 'Rare')
 train['Title'] = train['Title'].replace('Mlle', 'Miss')
 train['Title'] = train['Title'].replace('Ms', 'Miss')
 train['Title'] = train['Title'].replace('Mme', 'Mrs' )<categorify>
response = requests.get('https://miro.medium.com/max/700/1*Nv2NNALuokZEcV6hYEHdGA.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
test['Title'] = test['Title'].replace([ 'Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona', 'Countess', 'Lady', 'Sir'], 'Rare')
 test['Title'] = test['Title'].replace('Mlle', 'Miss')
 test['Title'] = test['Title'].replace('Ms', 'Miss')
 test['Title'] = test['Title'].replace('Mme', 'Mrs' )<groupby>
train.isnull().sum()
train[["Title","Age"]].groupby("Title" ).mean()<data_type_conversions>
train.isnull().sum()
for i in train["Title"]:
if i=="Master":
train["Age"]=train["Age"].fillna(4)
 elif i=="Miss":
train["Age"]=train["Age"].fillna(22)
 elif i=="Mr":
train["Age"]=train["Age"].fillna(32)
 elif i=="Mrs":
train["Age"]= train["Age"].fillna(36)
 elif i=="Major":
train["Age"]= train["Age"].fillna(46)
 else:
train["Age"]=train["Age"].fillna(41 )<count_missing_values>
model_xml_roberta = 'jplu/tf-xlm-roberta-large'
model_bert = 'bert-base-multilingual-cased'
n_epochs = 10
max_len = 80
batch_size = 16 * strategy.num_replicas_in_sync
train.isnull().sum()<count_missing_values>
Image.open(".. /input/table-accurancy-models/Table accurancy models.png" )
train.isnull().sum()<groupby>
response = requests.get('http://jalammar.github.io/images/bert-base-bert-large-encoders.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
test[["Title","Age"]].groupby("Title" ).mean()<data_type_conversions>
response = requests.get('http://jalammar.github.io/images/bert-encoders-input.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
for i in train["Title"]:
if i=="Master":
test["Age"]=test["Age"].fillna(7)
 elif i=="Miss":
test["Age"]=test["Age"].fillna(22)
 elif i=="Mr":
test["Age"]=test["Age"].fillna(32)
 elif i=="Mrs":
test["Age"]= test["Age"].fillna(38)
 elif i=="Major":
test["Age"]= test["Age"].fillna(44)
 else:
test["Age"]=test["Age"].fillna(41 )<count_missing_values>
response = requests.get('http://jalammar.github.io/images/bert-output-vector.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
test.isnull().sum()<count_missing_values>
response = requests.get('https://cdn.analyticsvidhya.com/wp-content/uploads/2019/09/bert_emnedding.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
test.isnull().sum()<groupby>
response = requests.get('https://cdn.analyticsvidhya.com/wp-content/uploads/2019/09/bert-vs-openai-.jpg')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
test[["Pclass","Fare"]].groupby("Pclass" ).mean()<feature_engineering>
response = requests.get('https://miro.medium.com/max/2272/1*9cRchmIyxP4LUnONXLM82g.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
test["Fare"] = test["Fare"].fillna(12 )<count_missing_values>
response = requests.get('https://images.deepai.org/converted-papers/1911.02116/x7.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
test.isnull().sum()<count_missing_values>
tokenizer_bert = AutoTokenizer.from_pretrained(model_bert )
test.isnull().sum()<count_missing_values>
train_text_bert = train[['premise', 'hypothesis']].values.tolist()
test_text_bert = test[['premise', 'hypothesis']].values.tolist()
train_encoded_bert = tokenizer_bert.batch_encode_plus(
 train_text_bert,
pad_to_max_length=True,
max_length=max_len 
)
 test_encoded_bert = tokenizer_bert.batch_encode_plus(
 test_text_bert,
pad_to_max_length=True,
max_length=max_len
)
train.isnull().sum()<count_missing_values>
x_train, x_valid, y_train, y_valid = train_test_split(
 train_encoded_bert['input_ids'], train.label.values,
test_size=0.2, random_state=2020 
)
 x_test = test_encoded_bert['input_ids']
train.isnull().sum()<count_values>
auto = tf.data.experimental.AUTOTUNE
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_train, y_train)) 
.repeat() 
.shuffle(2048)
.batch(batch_size)
.prefetch(auto)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_valid, y_valid)) 
.batch(batch_size)
.cache() 
.prefetch(auto)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(x_test)
.batch(batch_size)
 )
train["Embarked"].value_counts()<data_type_conversions>
with strategy.scope() :
transformer_encoder = TFAutoModel.from_pretrained(model_bert)
 input_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_ids")
 sequence_output = transformer_encoder(input_ids)[0]
cls_token = sequence_output[:, 0, :]
out = Dense(3, activation='softmax' )(cls_token)
 model = Model(inputs=input_ids, outputs=out)
 model.compile(
 Adam(lr=1e-5),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] 
)
 model.summary()
train["Embarked"]=train["Embarked"].fillna("S" )<categorify>
n_steps = len(x_train)// batch_size
train_history_bert = model.fit(
 train_dataset,
steps_per_epoch=n_steps,
validation_data=valid_dataset,
epochs=n_epochs
)
from sklearn import preprocessing 
<categorify>
tokenizer_xml_roberta = AutoTokenizer.from_pretrained(model_xml_roberta )
<categorify>
train_text_xml_roberta = train[['premise', 'hypothesis']].values.tolist()
test_text_xml_roberta = test[['premise', 'hypothesis']].values.tolist()
train_encoded_xml_roberta = tokenizer_xml_roberta.batch_encode_plus(
 train_text_xml_roberta,
pad_to_max_length=True,
max_length=max_len 
)
 test_encoded_xml_roberta = tokenizer_xml_roberta.batch_encode_plus(
 test_text_xml_roberta,
pad_to_max_length=True,
max_length=max_len
)
dumytitle=pd.get_dummies(train['Title'] )<concatenate>
x_train, x_valid, y_train, y_valid = train_test_split(
 train_encoded_xml_roberta['input_ids'], train.label.values,
test_size=0.2, random_state=2020 
)
 x_test = test_encoded_xml_roberta['input_ids']
train=pd.concat([train,dumytitle], axis=1)
 train.head()<categorify>
auto = tf.data.experimental.AUTOTUNE
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_train, y_train)) 
.repeat() 
.shuffle(2048)
.batch(batch_size)
.prefetch(auto)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_valid, y_valid)) 
.batch(batch_size)
.cache() 
.prefetch(auto)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(x_test)
.batch(batch_size)
 )
dumytitle2=pd.get_dummies(test['Title'] )<concatenate>
with strategy.scope() :
transformer_encoder = TFAutoModel.from_pretrained(model_xml_roberta)
 input_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_ids")
 sequence_output = transformer_encoder(input_ids)[0]
cls_token = sequence_output[:, 0, :]
out = Dense(3, activation='softmax' )(cls_token)
 model = Model(inputs=input_ids, outputs=out)
 model.compile(
 Adam(lr=1e-5),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] 
)
 model.summary()
test=pd.concat([test,dumytitle2], axis=1)
 test.head(1 )<feature_engineering>
n_steps = len(x_train)// batch_size
train_history_xml_roberta = model.fit(
 train_dataset,
steps_per_epoch=n_steps,
validation_data=valid_dataset,
epochs=n_epochs
)
train_name=train["Name"]
for i in train['Name']:
train['Name']= train['Name'].replace(i,len(i))<feature_engineering>
response = requests.get('https://miro.medium.com/max/398/1*BfvKeP4ykqi4J4C5g4EZzg.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
test_name=test["Name"]
for i in test['Name']:
test['Name']= test['Name'].replace(i,len(i))<feature_engineering>
mnli = nlp.load_dataset(path='glue', name='mnli' )
bins = [0,25,40, np.inf]
mylabels = ['s_name', 'm_name', 'l_name',]
train["Name_len"] = pd.cut(train["Name"], bins, labels = mylabels)
 test["Name_len"] = pd.cut(test["Name"], bins, labels = mylabels )<count_values>
mnli_train_df = pd.DataFrame(mnli['train'])
 mnli_train_df = mnli_train_df[['premise', 'hypothesis', 'label']]
mnli_train_df['lang_abv'] = 'en'
train["Name_len"].value_counts()<categorify>
xnli = nlp.load_dataset(path='xnli' )
Name_mapping = {'s_name': 1, 'm_name': 2 , 'l_name': 3}
train['Name_len'] = train['Name_len'].map(Name_mapping)
 test['Name_len'] = test['Name_len'].map(Name_mapping )<set_options>
buffer = {
'premise': [],
'hypothesis': [],
'label': [],
'lang_abv': []
}
for x in xnli['validation']:
label = x['label']
for idx, lang in enumerate(x['hypothesis']['language']):
hypothesis = x['hypothesis']['translation'][idx]
premise = x['premise'][lang]
buffer['premise'].append(premise)
 buffer['hypothesis'].append(hypothesis)
 buffer['label'].append(label)
 buffer['lang_abv'].append(lang)
 xnli_valid_df = pd.DataFrame(buffer)
 xnli_valid_df = xnli_valid_df[['premise', 'hypothesis', 'label', 'lang_abv']]
%matplotlib inline
warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', None)
<groupby>
response = requests.get('https://miro.medium.com/max/750/0*cZwrV8EyfJSeunag.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
train[["AgeGroup","Survived"]].groupby("AgeGroup" ).mean()<categorify>
response = requests.get('https://d3i71xaburhd42.cloudfront.net/f3ee8dcaaad5f47f347354fe5d740096097cbed5/1-Figure1-1.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult':5 , 'Adult': 6, 'Senior':7}
train['AgeGroup'] = train['AgeGroup'].map(age_mapping)
 test['AgeGroup'] = test['AgeGroup'].map(age_mapping )<feature_engineering>
!pip -q install googletrans
train["FamilySize"] =train["SibSp"]+train["Parch"]+1
train["FamilySize"].mean()<feature_engineering>
SEED = 42
os.environ['PYTHONHASHSEED']=str(SEED)
 random.seed(SEED)
 np.random.seed(SEED )
test["FamilySize"] =test["SibSp"]+test["Parch"]+1
test["FamilySize"].mean()<categorify>
def translate(words, dest):
dest_choices = ['zh-cn',
'ar',
'fr',
'sw',
'ur',
'vi',
'ru',
'hi',
'el',
'th',
'es',
'de',
'tr',
'bg'
]
if not dest:
dest = np.random.choice(dest_choices)
 translator = Translator()
decoded = translator.translate(words, dest=dest ).text
return decoded
def trans_parallel(df, dest):
premise_bag = bag.from_sequence(df.premise.tolist() ).map(translate, dest)
 hypo_bag = bag.from_sequence(df.hypothesis.tolist() ).map(translate, dest)
 with diagnostics.ProgressBar() :
premises = premise_bag.compute()
hypos = hypo_bag.compute()
df[['premise', 'hypothesis']] = list(zip(premises, hypos))
return df
train['Single'] = train['FamilySize'].map(lambda s: 1 if s == 1 else 0)
 train['SmallFam'] = train['FamilySize'].map(lambda s: 1 if s == 2 else 0)
 train['MedFam'] = train['FamilySize'].map(lambda s: 1 if 3 <= s <= 4 else 0)
 train['LargeFam'] = train['FamilySize'].map(lambda s: 1 if s >= 5 else 0 )<categorify>
eng = train.loc[train.lang_abv == "en"].copy().pipe(trans_parallel, dest=None)
 non_eng = train.loc[train.lang_abv != "en"].copy().pipe(trans_parallel, dest='en')
 train_data_translate = train.append([eng, non_eng] )
test['Single'] = test['FamilySize'].map(lambda s: 1 if s == 1 else 0)
 test['SmallFam'] = test['FamilySize'].map(lambda s: 1 if s == 2 else 0)
 test['MedFam'] = test['FamilySize'].map(lambda s: 1 if 3 <= s <= 4 else 0)
 test['LargeFam'] = test['FamilySize'].map(lambda s: 1 if s >= 5 else 0 )<categorify>
frames = [train_data_translate, xnli_valid_df, mnli_sample]
train_data_def = pd.concat(frames )
train = pd.get_dummies(train, columns = ["Embarked"], prefix="Em" )<categorify>
tf.tpu.experimental.initialize_tpu_system(tpu )
test = pd.get_dummies(test, columns = ["Embarked"], prefix="Em" )<categorify>
train_text_xml_roberta = train_data_def[['premise', 'hypothesis']].values.tolist()
test_text_xml_roberta = test[['premise', 'hypothesis']].values.tolist()
train_encoded_xml_roberta = tokenizer_xml_roberta.batch_encode_plus(
 train_text_xml_roberta,
pad_to_max_length=True,
max_length=max_len 
)
 test_encoded_xml_roberta = tokenizer_xml_roberta.batch_encode_plus(
 test_text_xml_roberta,
pad_to_max_length=True,
max_length=max_len
)
train["Pclass"] = train["Pclass"].astype("category")
 train = pd.get_dummies(train, columns = ["Pclass"],prefix="Pc" )<categorify>
x_train, x_valid, y_train, y_valid = train_test_split(
 train_encoded_xml_roberta['input_ids'], train_data_def.label.values,
test_size=0.2, random_state=2020 
)
 x_test = test_encoded_xml_roberta['input_ids']
test["Pclass"] = test["Pclass"].astype("category")
 test = pd.get_dummies(test, columns = ["Pclass"],prefix="Pc" )<feature_engineering>
auto = tf.data.experimental.AUTOTUNE
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_train, y_train)) 
.repeat() 
.shuffle(2048)
.batch(batch_size)
.prefetch(auto)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_valid, y_valid)) 
.batch(batch_size)
.cache() 
.prefetch(auto)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(x_test)
.batch(batch_size)
 )
train['Ticket1']=train['Ticket'].str[:1]<feature_engineering>
response = requests.get('https://miro.medium.com/max/1100/1*zfdW5zAyQxge85gA_mFPYg.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
test['Ticket1']=test['Ticket'].str[:1]<feature_engineering>
response = requests.get('https://paperswithcode.com/media/methods/Screen_Shot_2020-05-28_at_6.15.37_PM_apRrZCo.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
train['Ticket1']=train['Ticket1'].replace(['1', '2', '3', '4', '5', '6', '7', '8', '9'], 'N' )<feature_engineering>
response = requests.get('https://www.jeremyjordan.me/content/images/2018/02/Screen-Shot-2018-02-24-at-11.47.09-AM.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img
train['Ticket1']=train['Ticket1'].replace('N', 'NumberTicket' )<feature_engineering>
fine_tuning_lr = pd.read_csv(".. /input/fine-tuning/table tuning lr.csv", sep = ';')
 fine_tuning_lr
train['Ticket1']=train['Ticket1'].replace(['A','W', 'F', 'L'], 'OtherTicket' )<feature_engineering>
fine_tuning_lr = pd.read_csv(".. /input/fine-tuning/epsilon table.csv", sep = ';')
 fine_tuning_lr
train['Ticket1']=train['Ticket1'].replace(['S','P', 'C'], ['S_Ticket', 'P_Ticket', 'C_Ticket'] )<count_values>
with strategy.scope() :
transformer_encoder = TFAutoModel.from_pretrained(model_xml_roberta)
 input_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_ids")
 sequence_output = transformer_encoder(input_ids)[0]
cls_token = sequence_output[:, 0, :]
out = Dense(3, activation='softmax' )(cls_token)
 model = Model(inputs=input_ids, outputs=out)
 model.compile(
 Adam(lr=2e-5,beta_1=0.9, beta_2=0.999, epsilon=5e-08),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] 
)
 model.summary()
train["Ticket1"].value_counts()<categorify>
n_steps = len(x_train)// batch_size
train_history_xml_roberta_def = model.fit(
 train_dataset,
steps_per_epoch=n_steps,
validation_data=valid_dataset,
epochs=3
)
test['Ticket1']=test['Ticket1'].replace(['1', '2', '3', '4', '5', '6', '7', '8', '9'], 'N' )<feature_engineering>
test_preds = model.predict(test_dataset, verbose=1)
 submission['prediction'] = test_preds.argmax(axis=1 )
test['Ticket1']=test['Ticket1'].replace(['S','P', 'C', 'N'], ['S_Ticket', 'P_Ticket', 'C_Ticket','NumberTicket'] )<feature_engineering>
submission.to_csv("submission.csv", index = False )