Kaggle competition: Contradictory, My Dear Watson
KRR = KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5)
 score = rmsle_cv(KRR)
 print("Kernel Ridge score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
!pip install googletrans textAugment
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0005, random_state=1))
score = rmsle_cv(lasso)
 print("Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_train_metric>
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)
 print('Running on TPU ', tpu.master())
 except ValueError:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
score = rmsle_cv(ENet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_train_metric>
df_train = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/train.csv')
 df_test = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/test.csv' )
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5)
 score = rmsle_cv(GBoost)
 print("Gradient Boosting score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<train_model>
from textaugment import EDA
from googletrans import Translator
LassoMd = lasso.fit(train.values,y_train)
 ENetMd = ENet.fit(train.values,y_train)
 KRRMd = KRR.fit(train.values,y_train)
 GBoostMd = GBoost.fit(train.values,y_train )<predict_on_test>
for lang in df_train['language'].unique() :
number_of_training_samples = df_train[df_train['language'] == lang].shape[0] / df_train.shape[0]
number_of_testing_samples = df_test[df_test['language'] == lang].shape[0] / df_test.shape[0]
print('distribution of {} in training samples: {} and in testing samples {}'.format(lang, number_of_training_samples, number_of_testing_samples))
finalMd =(np.expm1(LassoMd.predict(test.values)) + np.expm1(ENetMd.predict(test.values)) + np.expm1(KRRMd.predict(test.values)) + np.expm1(GBoostMd.predict(test.values)))/ 4
finalMd<save_to_csv>
idx2label = {0: 'entailment', 1 :'neutral', 2: 'contradiction'}
for label in df_train['label'].unique() :
number_of_training_samples = df_train[df_train['label'] == label].shape[0] / df_train.shape[0]
print('distribution of {} in training samples: {}'.format(idx2label[label], number_of_training_samples))
sub = pd.DataFrame()
sub['Id'] = test_ID
sub['SalePrice'] = finalMd
sub.to_csv('submission.csv',index=False )<define_search_space>
tf.random.set_seed(seed )
%%writefile main.cpp
using namespace std;
using namespace std::chrono;
constexpr array<uint8_t, 12> DISTRIBUTION{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 5}; // You can setup how many families you need for swaps and what best choice use for each family
// {2, 5} it's mean the first random family will brute force for choices 1-2 and the second random family will brute force for choices 1-5
constexpr int MAX_OCCUPANCY = 300;
constexpr int MIN_OCCUPANCY = 125;
constexpr int NUMBER_FAMILIES = 6000;
constexpr int NUMBER_DAYS = 100;
constexpr int BEST_N = 10;
array<uint8_t, NUMBER_FAMILIES> n_people;
array<array<uint8_t, 10>, NUMBER_FAMILIES> choices;
array<array<uint16_t, 10>, NUMBER_FAMILIES> PCOSTM;
array<array<array<double, 5>, 176>, 176> ACOSTM;
void init_data() {
ifstream in(".. /input/santa-2019-revenge-of-the-accountants/family_data.csv");
assert(in && "family_data.csv");
string header;
int n,x;
char comma;
getline(in, header);
for(int j = 0; j < choices.size() ; ++j){
in >> x >> comma;
for(int i = 0; i < 10; ++i){
in >> x >> comma;
choices[j][i] = x-1;
}
in >> n;
n_people[j] = n;
}
array<int, 10> pc{0, 50, 50, 100, 200, 200, 300, 300, 400, 500};
array<int, 10> pn{0, 0, 9, 9, 9, 18, 18, 36, 36, 235};
for(int j = 0; j < PCOSTM.size() ; ++j)
 for(int i = 0; i < 10; ++i)
 PCOSTM[j][i] = pc[i] + pn[i] * n_people[j];
for(int i = 0; i < 176; ++i)
 for(int j = 0; j < 176; ++j)
 for(int k = 1; k <= 5; ++k)
 ACOSTM[i][j][k-1] = i * pow(i+MIN_OCCUPANCY, 0.5 + abs(i-j)/ 50.0)/ 400.0 / k / k;
}
array<uint8_t, NUMBER_FAMILIES> read_submission(string filename){
ifstream in(filename);
assert(in && "submission.csv");
array<uint8_t, NUMBER_FAMILIES> assigned_day{};
string header;
int id, x;
char comma;
getline(in, header);
for(int j = 0; j < choices.size() ; ++j){
in >> id >> comma >> x;
assigned_day[j] = x-1;
auto it = find(begin(choices[j]), end(choices[j]), assigned_day[j]);
if(it != end(choices[j]))
assigned_day[j] = distance(begin(choices[j]), it);
}
return assigned_day;
}
struct Index {
Index(array<uint8_t, NUMBER_FAMILIES> assigned_days_): assigned_days(assigned_days_){
setup() ;
}
array<uint8_t, NUMBER_FAMILIES> assigned_days;
array<uint16_t, NUMBER_DAYS> daily_occupancy_{};
int preference_cost_ = 0;
void setup() {
preference_cost_ = 0;
daily_occupancy_.fill(0);
for(int j = 0; j < assigned_days.size() ; ++j){
daily_occupancy_[choices[j][assigned_days[j]]] += n_people[j];
preference_cost_ += PCOSTM[j][assigned_days[j]];
}
}
double calc(const array<uint16_t, NUMBER_FAMILIES>& indices, const array<uint8_t, DISTRIBUTION.size() >& change){
double accounting_penalty = 0.0;
auto daily_occupancy = daily_occupancy_;
int preference_cost = preference_cost_;
for(int i = 0; i < DISTRIBUTION.size() ; ++i){
int j = indices[i];
daily_occupancy[choices[j][assigned_days[j]]] -= n_people[j];
daily_occupancy[choices[j][ change[i]]] += n_people[j];
preference_cost += PCOSTM[j][change[i]] - PCOSTM[j][assigned_days[j]];
}
for(auto occupancy : daily_occupancy)
 if(occupancy < MIN_OCCUPANCY)
 return 1e12*(MIN_OCCUPANCY-occupancy);
else if(occupancy > MAX_OCCUPANCY)
 return 1e12*(occupancy - MAX_OCCUPANCY);
for(int day = 0; day < NUMBER_DAYS; ++day)
 for(int j = 0; j < 5; ++j)
 accounting_penalty += ACOSTM[daily_occupancy[day]-MIN_OCCUPANCY][daily_occupancy[min(NUMBER_DAYS-1, day+j+1)]-MIN_OCCUPANCY][j];
return preference_cost + accounting_penalty;
}
void reindex(const array<uint16_t, DISTRIBUTION.size() >& indices, const array<uint8_t, DISTRIBUTION.size() >& change){
for(int i = 0; i < DISTRIBUTION.size() ; ++i){
assigned_days[indices[i]] = change[i];
}
setup() ;
}
};
double calc(const array<uint8_t, NUMBER_FAMILIES>& assigned_days, bool print=false){
int preference_cost = 0;
double accounting_penalty = 0.0;
array<uint16_t, NUMBER_DAYS> daily_occupancy{};
for(int j = 0; j < assigned_days.size() ; ++j){
preference_cost += PCOSTM[j][assigned_days[j]];
daily_occupancy[choices[j][assigned_days[j]]] += n_people[j];
}
for(auto occupancy : daily_occupancy)
 if(occupancy < MIN_OCCUPANCY)
 return 1e12*(MIN_OCCUPANCY-occupancy);
else if(occupancy > MAX_OCCUPANCY)
 return 1e12*(occupancy - MAX_OCCUPANCY);
for(int day = 0; day < NUMBER_DAYS; ++day)
 for(int j = 0; j < 5; ++j)
 accounting_penalty += ACOSTM[daily_occupancy[day]-MIN_OCCUPANCY][daily_occupancy[min(99, day+j+1)]-MIN_OCCUPANCY][j];
if(print){
cout << preference_cost << " " << accounting_penalty << " " << preference_cost+accounting_penalty << endl;
}
return preference_cost + accounting_penalty;
}
void save_sub(const array<uint8_t, NUMBER_FAMILIES>& assigned_day){
ofstream out("submission.csv");
out << "family_id,assigned_day" << endl;
for(int i = 0; i < assigned_day.size() ; ++i)
 out << i << "," << choices[i][assigned_day[i]]+1 << endl;
}
const vector<array<uint8_t, DISTRIBUTION.size() >> changes = []() {
vector<array<uint8_t, DISTRIBUTION.size() >> arr;
array<uint8_t, DISTRIBUTION.size() > tmp{};
for(int i = 0; true; ++i){
arr.push_back(tmp);
tmp[0] += 1;
for(int j = 0; j < DISTRIBUTION.size() ; ++j)
 if(tmp[j] >= DISTRIBUTION[j]){
if(j >= DISTRIBUTION.size() -1)
 return arr;
tmp[j] = 0;
++tmp[j+1];
}
}
return arr;
}() ;
template<class ExitFunction>
void stochastic_product_search(Index index, ExitFunction fn){
double best_local_score = calc(index.assigned_days);
thread_local std::mt19937 gen(std::random_device{}());
uniform_int_distribution<> dis(0, NUMBER_FAMILIES-1);
array<uint16_t, NUMBER_FAMILIES> indices;
iota(begin(indices), end(indices), 0);
array<uint16_t, DISTRIBUTION.size() > best_indices{};
array<uint8_t, DISTRIBUTION.size() > best_change{};
bool need_save = false;
for(; fn() ;){
bool found_better = false;
for(int k = 0; k < BEST_N; ++k){
for(int i = 0; i < DISTRIBUTION.size() ; ++i)//random swap
swap(indices[i], indices[dis(gen)]);
for(const auto& change : changes){
auto score = index.calc(indices, change);
if(score < best_local_score){
found_better = true;
best_local_score = score;
best_change = change;
copy_n(begin(indices), DISTRIBUTION.size() , begin(best_indices)) ;
}
}
}
if(found_better){ // reindex from N best if found better
need_save = true;
index.reindex(best_indices, best_change);
// save_sub(index.assigned_days);
calc(index.assigned_days, true);
}
}
if(need_save)
 save_sub(index.assigned_days);
}
int main() {
init_data() ;
auto assigned_day = read_submission(".. /input/mip-optimization-preference-cost-santa2019revenge/submission.csv");
Index index(assigned_day);
calc(index.assigned_days, true);
// auto forever = []() { return true; };
// auto count_exit = [start = 0]() mutable { return(++start <= 1000); };
auto time_exit = [start = high_resolution_clock::now() ]() {
return duration_cast<minutes>(high_resolution_clock::now() -start ).count() < 355; //5h55
};
stochastic_product_search(index, time_exit);
return 0;
}<set_options>
model_name = 'jplu/tf-xlm-roberta-large'
tokenizer = XLMRobertaTokenizer.from_pretrained(model_name )
!g++ -pthread -lpthread -O3 -std=c++17 -o main main.cpp<set_options>
def clean_word(value):
language = value[0]
word = value[1]
if language != 'English':
word = word.lower()
return word
word = word.lower()
word = re.sub(r'\?\?', 'e', word)
 word = re.sub('\.\.\.', '.', word)
 word = re.sub('\/', ' ', word)
 word = re.sub('--', ' ', word)
 word = re.sub('/\xad', '', word)
 word = word.strip(' ')
 return word
df_train['premise'] = df_train[['language', 'premise']].apply(lambda v: clean_word(v), axis=1)
 df_train['hypothesis'] = df_train[['language', 'hypothesis']].apply(lambda v: clean_word(v), axis=1)
 df_test['premise'] = df_test[['language', 'premise']].apply(lambda v: clean_word(v), axis=1)
 df_test['hypothesis'] = df_test[['language', 'hypothesis']].apply(lambda v: clean_word(v), axis=1 )
sns.set_style('whitegrid')
 %matplotlib inline 
<load_from_csv>
def build_model() :
with strategy.scope() :
bert_encoder = TFXLMRobertaModel.from_pretrained(model_name)
 input_word_ids = tf.keras.Input(shape=(None,), dtype=tf.int32, name="input_word_ids")
 input_mask = tf.keras.Input(shape=(None,), dtype=tf.int32, name="input_mask")
 embedding = bert_encoder([input_word_ids, input_mask])[0]
output_layer = tf.keras.layers.Dropout(0.25 )(embedding)
 output_layer = tf.keras.layers.GlobalAveragePooling1D()(output_layer)
 output_dense_layer = tf.keras.layers.Dense(64, activation='relu' )(output_layer)
 output_dense_layer = tf.keras.layers.Dense(32, activation='relu' )(output_dense_layer)
 output = tf.keras.layers.Dense(3, activation='softmax' )(output_dense_layer)
 model = tf.keras.Model(inputs=[input_word_ids, input_mask], outputs=output)
 model.compile(tf.keras.optimizers.Adam(lr=1e-5), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 return model
titanic_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 titanic_df.head()<categorify>
batch_size = 8 * strategy.num_replicas_in_sync
num_splits = 5
test_input = None
def get_title(name):
if '.' in name:
return name.split(',')[1].split('.')[0].strip()
else:
return 'Unknown'
def title_map(title):
if title in ['Mr']:
return 1
elif title in ['Master']:
return 3
elif title in ['Ms','Mlle','Miss']:
return 4
elif title in ['Mme','Mrs']:
return 5
else:
return 2
titanic_df['title'] = titanic_df['Name'].apply(get_title ).apply(title_map)
 test_df['title'] = test_df['Name'].apply(get_title ).apply(title_map)
 title_xt = pd.crosstab(titanic_df['title'], titanic_df['Survived'])
 title_xt_pct = title_xt.div(title_xt.sum(1 ).astype(float), axis=0)
 title_xt_pct.plot(kind='bar',
stacked=True,
title='Survival Rate by title')
 plt.xlabel('title')
 plt.ylabel('Survival Rate' )<drop_column>
auto = tf.data.experimental.AUTOTUNE
languages = [ 'zh-cn' if lang == 'zh' else lang for lang in df_train['lang_abv'].unique() ]
def make_dataset(train_input, train_label):
dataset = tf.data.Dataset.from_tensor_slices(
(
 train_input,
train_label 
)
 ).repeat().shuffle(batch_size ).batch(batch_size ).prefetch(auto)
 return dataset
titanic_df = titanic_df.drop(['PassengerId','Name','Ticket'], axis=1)
 test_df = test_df.drop(['Name','Ticket'], axis=1 )<feature_engineering>
def xlm_roberta_encode(hypotheses, premises, src_langs, augmentation=False):
num_examples = len(hypotheses)
 sentence_1 = [tokenizer.encode(s)for s in premises]
sentence_2 = [tokenizer.encode(s)for s in hypotheses]
input_word_ids = list(map(lambda x: x[0]+x[1], list(zip(sentence_1,sentence_2))))
input_mask = [np.ones_like(x)for x in input_word_ids]
inputs = {
'input_word_ids': tf.keras.preprocessing.sequence.pad_sequences(input_word_ids, padding='post'),
'input_mask': tf.keras.preprocessing.sequence.pad_sequences(input_mask, padding='post')
 }
return inputs
test_df["Fare"].fillna(test_df["Fare"].median() , inplace=True)
 titanic_df.loc[ titanic_df['Fare'] <= 7.91, 'Fare'] = 0
titanic_df.loc[(titanic_df['Fare'] > 7.91)&(titanic_df['Fare'] <= 14.454), 'Fare'] = 1
titanic_df.loc[(titanic_df['Fare'] > 14.454)&(titanic_df['Fare'] <= 31), 'Fare'] = 2
titanic_df.loc[ titanic_df['Fare'] > 31, 'Fare'] = 3
test_df.loc[ test_df['Fare'] <= 7.91, 'Fare'] = 0
test_df.loc[(test_df['Fare'] > 7.91)&(test_df['Fare'] <= 14.454), 'Fare'] = 1
test_df.loc[(test_df['Fare'] > 14.454)&(test_df['Fare'] <= 31), 'Fare'] = 2
test_df.loc[test_df['Fare'] > 31, 'Fare'] = 3
titanic_df['Fare'] = titanic_df['Fare'].astype(int)
 test_df['Fare'] = test_df['Fare'].astype(int)
<categorify>
train_df, validation_df = train_test_split(df_train, test_size=0.1)
 if test_input is None:
test_input = xlm_roberta_encode(df_test.hypothesis.values, df_test.premise.values, df_test.lang_abv.values,augmentation=False)
 df_train['prediction'] = 0
num_augmentation = 1
train_input = xlm_roberta_encode(train_df.hypothesis.values,train_df.premise.values, train_df.lang_abv.values, augmentation=False)
 train_label = train_df.label.values
train_sequence = make_dataset(train_input, train_label)
 n_steps =(len(train_label)) // batch_size
validation_input = xlm_roberta_encode(validation_df.hypothesis.values, validation_df.premise.values,validation_df.lang_abv.values, augmentation=False)
 validation_label = validation_df.label.values
tf.keras.backend.clear_session()
with strategy.scope() :
model = build_model()
history = model.fit(
 train_sequence, shuffle=True, steps_per_epoch=n_steps,
validation_data =(validation_input, validation_label), epochs=50, verbose=1,
callbacks=[
tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=10),
tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', factor=0.1, patience=5),
tf.keras.callbacks.ModelCheckpoint(
 'model.h5', monitor='val_accuracy', save_best_only=True,save_weights_only=True)
 ] 
)
 model.load_weights('model.h5')
 validation_predictions = model.predict(validation_input)
 validation_predictions = np.argmax(validation_predictions, axis=-1)
 validation_df['predictions'] = validation_predictions
acc = accuracy_score(validation_label, validation_predictions)
 print('Accuracy: {}'.format(acc))
test_split_predictions = model.predict(test_input)
 del train_input, train_label, validation_input, validation_label, model, train_sequence
gc.collect()
titanic_df['Age'] = titanic_df.groupby(['Pclass'])['Age'].transform(lambda x: x.fillna(x.mean()))
test_df['Age'] = test_df.groupby(['Pclass'])['Age'].transform(lambda x: x.fillna(x.mean()))<drop_column>
fig, axes = plt.subplots(nrows=4, ncols=4, figsize=(15,10))
labels = list(idx2label.values())
 for ax,language in zip(axes.flatten() [:validation_df['language'].nunique() ],validation_df['language'].unique()):
y_pred = validation_df[validation_df['language'] == language]['prediction'].values
y_true = validation_df[validation_df['language'] == language]['label'].values
lang_acc = accuracy_score(y_true, y_pred)
 print('Language {} has accuracy {}'.format(language, lang_acc))
cm = confusion_matrix(np.array([idx2label[v] for v in y_true]),
np.array([idx2label[v] for v in y_pred]),
labels=labels)
 cax = ConfusionMatrixDisplay(cm, display_labels=labels)
 cax.plot(ax=ax)
 ax.set_title(language)
 ax.set_xticklabels([''] + labels)
 ax.set_yticklabels([''] + labels)
 plt.title('Confusion matrix of the classifier')
 plt.xlabel('Predicted')
 plt.ylabel('True')
 plt.tight_layout()
plt.show()
titanic_df.drop("Cabin",axis=1,inplace=True)
 test_df.drop("Cabin",axis=1,inplace=True )<feature_engineering>
predictions = np.argmax(test_split_predictions, axis=-1)
 submission = df_test.id.copy().to_frame()
submission['prediction'] = predictions
<categorify><EOS> 
submission.head()
submission.to_csv("submission.csv", index = False )