Kaggle competition: PetFinder.my Adoption Prediction
fold_number = 0
train_dataset = DatasetRetriever(
 kinds=dataset[dataset['fold'] != fold_number].kind.values,
image_names=dataset[dataset['fold'] != fold_number].image_name.values,
labels=dataset[dataset['fold'] != fold_number].label.values,
transforms=get_train_transforms() , 
)
 validation_dataset = DatasetRetriever(
 kinds=dataset[dataset['fold'] == fold_number].kind.values,
image_names=dataset[dataset['fold'] == fold_number].image_name.values,
labels=dataset[dataset['fold'] == fold_number].label.values,
transforms=get_valid_transforms() ,
)<compute_train_metric>
@contextmanager
def timer(name):

 t0 = time.time()
yield
print(f'[{name}] done in {time.time() - t0:.0f} s')
 def confusion_matrix(rater_a, rater_b, min_rating=None, max_rating=None):

 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(rater_a + rater_b)
 if max_rating is None:
max_rating = max(rater_a + rater_b)
 num_ratings = int(max_rating - min_rating + 1)
 conf_mat = [[0 for i in range(num_ratings)]
for j in range(num_ratings)]
for a, b in zip(rater_a, rater_b):
conf_mat[a - min_rating][b - min_rating] += 1
return conf_mat
def histogram(ratings, min_rating=None, max_rating=None):

 if min_rating is None:
min_rating = min(ratings)
 if max_rating is None:
max_rating = max(ratings)
 num_ratings = int(max_rating - min_rating + 1)
 hist_ratings = [0 for x in range(num_ratings)]
for r in ratings:
hist_ratings[r - min_rating] += 1
return hist_ratings
def quadratic_weighted_kappa(y, y_pred):

 rater_a = y
rater_b = y_pred
min_rating=None
max_rating=None
rater_a = np.array(rater_a, dtype=int)
 rater_b = np.array(rater_b, dtype=int)
 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(min(rater_a), min(rater_b))
if max_rating is None:
max_rating = max(max(rater_a), max(rater_b))
conf_mat = confusion_matrix(rater_a, rater_b,
min_rating, max_rating)
 num_ratings = len(conf_mat)
 num_scored_items = float(len(rater_a))
hist_rater_a = histogram(rater_a, min_rating, max_rating)
 hist_rater_b = histogram(rater_b, min_rating, max_rating)
 numerator = 0.0
denominator = 0.0
for i in range(num_ratings):
for j in range(num_ratings):
expected_count =(hist_rater_a[i] * hist_rater_b[j]
/ num_scored_items)
 d = pow(i - j, 2.0)/ pow(num_ratings - 1, 2.0)
 numerator += d * conf_mat[i][j] / num_scored_items
denominator += d * expected_count / num_scored_items
return(1.0 - numerator / denominator)
 class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = quadratic_weighted_kappa(y, X_p)
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
def cloud(text, title, size =(10,7)) :
wordcloud = WordCloud(width=800, height=400,
collocations=True
).generate(" ".join(text))
fig = plt.figure(figsize=size, dpi=80, facecolor='k',edgecolor='k')
 plt.imshow(wordcloud,interpolation='bilinear')
 plt.axis('off')
 plt.title(title, fontsize=25,color='w')
 plt.tight_layout(pad=0)
 plt.show()
class AverageMeter(object):

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def alaska_weighted_auc(y_true, y_valid):

 tpr_thresholds = [0.0, 0.4, 1.0]
weights = [2, 1]
fpr, tpr, thresholds = metrics.roc_curve(y_true, y_valid, pos_label=1)
 areas = np.array(tpr_thresholds[1:])- np.array(tpr_thresholds[:-1])
 normalization = np.dot(areas, weights)
 competition_metric = 0
for idx, weight in enumerate(weights):
y_min = tpr_thresholds[idx]
y_max = tpr_thresholds[idx + 1]
mask =(y_min < tpr)&(tpr < y_max)
 x_padding = np.linspace(fpr[mask][-1], 1, 100)
 x = np.concatenate([fpr[mask], x_padding])
 y = np.concatenate([tpr[mask], [y_max] * len(x_padding)])
 y = y - y_min
score = metrics.auc(x, y)
 submetric = score * weight
best_subscore =(y_max - y_min)* weight
competition_metric += submetric
return competition_metric / normalization
class RocAucMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.y_true = np.array([0,1])
 self.y_pred = np.array([0.5,0.5])
 self.score = 0
def update(self, y_true, y_pred):
y_true = y_true.cpu().numpy().argmax(axis=1 ).clip(min=0, max=1 ).astype(int)
 y_pred = 1 - nn.functional.softmax(y_pred, dim=1 ).data.cpu().numpy() [:,0]
self.y_true = np.hstack(( self.y_true, y_true))
self.y_pred = np.hstack(( self.y_pred, y_pred))
self.score = alaska_weighted_auc(self.y_true, self.y_pred)
 @property
def avg(self):
return self.score<normalization>
print(os.listdir(".. /input/train"))
train = pd.read_csv(".. /input/train/train.csv")
 test = pd.read_csv(".. /input/test/test.csv")
 breed_label = pd.read_csv('.. /input/breed_labels.csv')
 train.Name = train.Name.astype(str)
 test.Name = test.Name.astype(str)
 train.Description = train.Description.astype(str)
 test.Description = test.Description.astype(str)
 display(train.sample(4))
class LabelSmoothing(nn.Module):
def __init__(self, smoothing = 0.05):
super(LabelSmoothing, self ).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
def forward(self, x, target):
if self.training:
x = x.float()
target = target.float()
logprobs = torch.nn.functional.log_softmax(x, dim = -1)
 nll_loss = -logprobs * target
nll_loss = nll_loss.sum(-1)
 smooth_loss = -logprobs.mean(dim=-1)
 loss = self.confidence * nll_loss + self.smoothing * smooth_loss
return loss.mean()
else:
return torch.nn.functional.cross_entropy(x, target )<init_hyperparams>
samplesentiment = pd.read_json('.. /input/train_sentiment/{}'.format("4fdebca57.json"), orient='index', typ='series')
 print("Document Sentiment")
 print(samplesentiment["documentSentiment"])
 print("
Entities")
 print(samplesentiment["entities"][0])
 print("
Sentences")
 print(samplesentiment['sentences'][0] )
warnings.filterwarnings("ignore")
 class Fitter:
def __init__(self, model, device, config):
self.config = config
self.epoch = 0
self.base_dir = './'
self.log_path = f'{self.base_dir}/log.txt'
self.best_summary_loss = 10**5
self.model = model
self.device = device
param_optimizer = list(self.model.named_parameters())
 no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.001},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
self.optimizer = torch.optim.AdamW(self.model.parameters() , lr=config.lr)
 self.scheduler = config.SchedulerClass(self.optimizer, **config.scheduler_params)
 self.criterion = LabelSmoothing().to(self.device)
 self.log(f'Fitter prepared.Device is {self.device}')
 def fit(self, train_loader, validation_loader):
for e in range(self.config.n_epochs):
if self.config.verbose:
lr = self.optimizer.param_groups[0]['lr']
timestamp = datetime.utcnow().isoformat()
self.log(f'
{timestamp}
LR: {lr}')
 t = time.time()
summary_loss, final_scores = self.train_one_epoch(train_loader)
 self.log(f'[RESULT]: Train.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, time: {(time.time() - t):.5f}')
 self.save(f'{self.base_dir}/last-checkpoint.bin')
 t = time.time()
summary_loss, final_scores = self.validation(validation_loader)
 self.log(f'[RESULT]: Val.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, time: {(time.time() - t):.5f}')
 if summary_loss.avg < self.best_summary_loss:
self.best_summary_loss = summary_loss.avg
self.model.eval()
self.save(f'{self.base_dir}/best-checkpoint-{str(self.epoch ).zfill(3)}epoch.bin')
 for path in sorted(glob(f'{self.base_dir}/best-checkpoint-*epoch.bin')) [:-3]:
os.remove(path)
 if self.config.validation_scheduler:
self.scheduler.step(metrics=summary_loss.avg)
 self.epoch += 1
def validation(self, val_loader):
self.model.eval()
summary_loss = AverageMeter()
final_scores = RocAucMeter()
t = time.time()
for step,(images, targets)in enumerate(val_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Val Step {step}/{len(val_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 with torch.no_grad() :
targets = targets.to(self.device ).float()
batch_size = images.shape[0]
images = images.to(self.device ).float()
outputs = self.model(images)
 loss = self.criterion(outputs, targets)
 final_scores.update(targets, outputs)
 summary_loss.update(loss.detach().item() , batch_size)
 return summary_loss, final_scores
def train_one_epoch(self, train_loader):
self.model.train()
summary_loss = AverageMeter()
final_scores = RocAucMeter()
t = time.time()
for step,(images, targets)in enumerate(train_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Train Step {step}/{len(train_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, final_score: {final_scores.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 targets = targets.to(self.device ).float()
images = images.to(self.device ).float()
batch_size = images.shape[0]
self.optimizer.zero_grad()
outputs = self.model(images)
 loss = self.criterion(outputs, targets)
 loss.backward()
final_scores.update(targets, outputs)
 summary_loss.update(loss.detach().item() , batch_size)
 self.optimizer.step()
if self.config.step_scheduler:
self.scheduler.step()
return summary_loss, final_scores
def save(self, path):
self.model.eval()
torch.save({
'model_state_dict': self.model.state_dict() ,
'optimizer_state_dict': self.optimizer.state_dict() ,
'scheduler_state_dict': self.scheduler.state_dict() ,
'best_summary_loss': self.best_summary_loss,
'epoch': self.epoch,
}, path)
 def load(self, path):
checkpoint = torch.load(path)
 self.model.load_state_dict(checkpoint['model_state_dict'])
 self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
 self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
 self.best_summary_loss = checkpoint['best_summary_loss']
self.epoch = checkpoint['epoch'] + 1
def log(self, message):
if self.config.verbose:
print(message)
 with open(self.log_path, 'a+')as logger:
logger.write(f'{message}
' )<choose_model_class>
with timer("Process Sentiment Train Data"):
sentiment_list = os.listdir('.. /input/train_sentiment')
 train_sentiment_df = pd.DataFrame()
for i,x in enumerate(sentiment_list):
samplesentiment = pd.read_json('.. /input/train_sentiment/{}'.format(x), orient='index', typ='series')
 sentences = json_normalize(samplesentiment.sentences ).loc[:,['sentiment.magnitude', 'sentiment.score']].agg(
 {
'sentiment.magnitude' : ['count','mean','std'],
'sentiment.score' : ['mean','std', 'sum']
} ).unstack().to_frame().sort_index(level=1 ).T
sentences.columns = sentences.columns.map('_'.join)
 sentiment = pd.concat([json_normalize(samplesentiment["documentSentiment"]),
sentences,
], axis =1)
 sentiment.index = [x[:9]]
train_sentiment_df = pd.concat([train_sentiment_df, sentiment], axis =0)
 display(train_sentiment_df.sample(5))
train = train.join(train_sentiment_df, on = "PetID")
 del train_sentiment_df
def get_net() :
net = EfficientNet.from_pretrained('efficientnet-b2')
 net._fc = nn.Linear(in_features=1408, out_features=4, bias=True)
 return net
net = get_net().cuda()<init_hyperparams>
with timer("Process Sentiment Test Data"):
sentiment_list = os.listdir('.. /input/test_sentiment/')
 test_sentiment_df = pd.DataFrame()
for i,x in enumerate(sentiment_list):
samplesentiment = pd.read_json('.. /input/test_sentiment/{}'.format(x), orient='index', typ='series')
 sentences = json_normalize(samplesentiment.sentences ).loc[:,['sentiment.magnitude', 'sentiment.score']].agg(
 {
'sentiment.magnitude' : ['count','mean','std'],
'sentiment.score' : ['mean','std', 'sum']
} ).unstack().to_frame().sort_index(level=1 ).T
sentences.columns = sentences.columns.map('_'.join)
 sentiment = pd.concat([json_normalize(samplesentiment["documentSentiment"]),
sentences,
], axis =1)
 sentiment.index = [x[:9]]
test_sentiment_df = pd.concat([test_sentiment_df, sentiment], axis =0)
 display(test_sentiment_df.sample(5))
test = test.join(test_sentiment_df, on = "PetID")
 del test_sentiment_df
class TrainGlobalConfig:
num_workers = 4
batch_size = 16
n_epochs = 25
lr = 0.001
verbose = True
verbose_step = 1
step_scheduler = False
validation_scheduler = True
SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau
scheduler_params = dict(
 mode='min',
factor=0.5,
patience=1,
verbose=False,
threshold=0.0001,
threshold_mode='abs',
cooldown=0,
min_lr=1e-8,
eps=1e-08 
)
<load_pretrained>
sample_image_metadata = pd.read_json('.. /input/train_metadata/{}'.format("c161afd26-3.json"), orient='index', typ='series')
 print("Label Annotations")
 print(sample_image_metadata['labelAnnotations'])
 print("
Image Properties Annotations")
 print(sample_image_metadata['imagePropertiesAnnotation'])
 print("
Crop Hints Annotation")
 print(sample_image_metadata['cropHintsAnnotation'] )
def run_training() :
device = torch.device('cuda:0')
 train_loader = torch.utils.data.DataLoader(
 train_dataset,
sampler=BalanceClassSampler(labels=train_dataset.get_labels() , mode="downsampling"),
batch_size=TrainGlobalConfig.batch_size,
pin_memory=False,
drop_last=True,
num_workers=TrainGlobalConfig.num_workers, 
)
 val_loader = torch.utils.data.DataLoader(
 validation_dataset,
batch_size=TrainGlobalConfig.batch_size,
num_workers=TrainGlobalConfig.num_workers,
shuffle=False,
sampler=SequentialSampler(validation_dataset),
pin_memory=False, 
)
 fitter = Fitter(model=net, device=device, config=TrainGlobalConfig)
 fitter.fit(train_loader, val_loader )<load_from_csv>
train.set_index("PetID", inplace= True)
 test.set_index("PetID", inplace= True)
 traindex = train.index
testdex = test.index
y = train.AdoptionSpeed
train.drop('AdoptionSpeed', axis =1 , inplace=True)
 df = pd.concat([train,test], axis = 0)
 df = df.merge(breed_label, left_on = 'Breed1', right_on = 'BreedID', how = 'left', suffixes = ['','b1'] ).drop('BreedID',axis=1)
 df.rename(columns = {'BreedName':'b1name'}, inplace=True)
 df = df.merge(breed_label, left_on = 'Breed2', right_on = 'BreedID', how = 'left', suffixes = ['','b2'] ).drop('BreedID',axis=1)
 df.rename(columns = {'BreedName':'b2name'}, inplace=True )
file = open('.. /input/alaska2-public-baseline/log.txt', 'r')
 for line in file.readlines() :
print(line[:-1])
 file.close()<load_pretrained>
print("Encode Variables")
 categorical = ["Name"]
print("Encoding :",categorical)
 df.Name = df.Name.str.lower()
lbl = preprocessing.LabelEncoder()
for col in categorical:
df["encoded_" + col] = lbl.fit_transform(df[col].astype(str))
df.drop('RescuerID', axis =1 ,inplace=True )
checkpoint = torch.load('.. /input/alaska2-public-baseline/best-checkpoint-033epoch.bin')
 net.load_state_dict(checkpoint['model_state_dict']);
net.eval() ;<data_type_conversions>
textfeats = ["Description"]
for cols in textfeats:
df[cols] = df[cols].astype(str)
 df[cols] = df[cols].astype(str ).fillna('missing')
 df[cols] = df[cols].str.lower()
df[cols + '_num_words'] = df[cols].apply(lambda comment: len(comment.split()))
df[cols + '_num_unique_words'] = df[cols].apply(lambda comment: len(set(w for w in comment.split())))
 df[cols + '_words_vs_unique'] = df[cols+'_num_unique_words'] / df[cols+'_num_words'] * 100
class DatasetSubmissionRetriever(Dataset):
def __init__(self, image_names, transforms=None):
super().__init__()
self.image_names = image_names
self.transforms = transforms
def __getitem__(self, index: int):
image_name = self.image_names[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/Test/{image_name}', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image_name, image
def __len__(self)-> int:
return self.image_names.shape[0]<define_variables>
df['name_and_breeds'] = df['Name'].astype(str)+ ' ' + df['b1name'].astype(str)+ ' ' + df['b2name'].astype(str)
 df['name_and_breeds'] = df['name_and_breeds'].str.lower()
df.drop(['Name', 'b1name','b2name'], axis =1, inplace= True )
dataset = DatasetSubmissionRetriever(
 image_names=np.array([path.split('/')[-1] for path in glob('.. /input/alaska2-image-steganalysis/Test/*.jpg')]),
transforms=get_valid_transforms() , 
)
 data_loader = DataLoader(
 dataset,
batch_size=8,
shuffle=False,
num_workers=2,
drop_last=False,
)<save_to_csv>
train = df.iloc[0:len(traindex),:]
test = df.iloc[len(traindex):,:]
del df; gc.collect() ;
submission = pd.DataFrame(result)
 submission.to_csv('submission.csv', index=False)
 submission.head()<import_modules>
word_vect = TfidfVectorizer(
 sublinear_tf=True,
strip_accents='unicode',
analyzer='word',
token_pattern=r'\w{1,}',
stop_words='english',
ngram_range=(1, 2),
max_features=20000)
 char_vectorizer = TfidfVectorizer(
 sublinear_tf=True,
strip_accents='unicode',
analyzer='word',
ngram_range=(2,5),
max_features=3000)
 with timer("Word Grams TFIDF"):
word_vect.fit(train['Description'])
 train_word_features = word_vect.transform(train['Description'])
 test_word_features = word_vect.transform(test['Description'])
 with timer("Character Grams TFIDF"):
char_vectorizer.fit(train['name_and_breeds'])
 train_char_features = char_vectorizer.transform(train['name_and_breeds'])
 test_char_features = char_vectorizer.transform(test['name_and_breeds'])
 num_features = [f_ for f_ in train.columns
if f_ not in ["Description", "name_and_breeds"]]
feature_names =(word_vect.get_feature_names() +
char_vectorizer.get_feature_names() +
num_features)
 with timer("Sparse Combine"):
X = hstack(
 [
train_char_features,
train_word_features,
train[num_features]
]
).tocsr()
del train_char_features
gc.collect()
testing = hstack(
 [
test_char_features,
test_word_features,
test[num_features]
]
).tocsr()
del test_char_features; gc.collect()
!pip install -q efficientnet_pytorch
Compose, HorizontalFlip, CLAHE, HueSaturationValue,
RandomBrightness, RandomContrast, RandomGamma, OneOf, Resize,
ToFloat, ShiftScaleRotate, GridDistortion, ElasticTransform, JpegCompression, HueSaturationValue,
RGBShift, RandomBrightness, RandomContrast, Blur, MotionBlur, MedianBlur, GaussNoise, CenterCrop,
IAAAdditiveGaussianNoise, GaussNoise, OpticalDistortion, RandomSizedCrop, VerticalFlip 
)
<set_options>
lgtrain = lgb.Dataset(X, label=y, feature_name= feature_names,
free_raw_data=False, categorical_feature = categorical_vars)
 feature_importance_df = pd.DataFrame()
n_fold = 0
print("Light Gradient Boosting Classifier: ")
 lgbm_params = {
'application': 'regression',
'boosting': 'gbdt',
'metric': 'rmse',
'num_leaves': 150,
'max_depth': -1,
'learning_rate': 0.01,
'bagging_fraction': 0.85,
'feature_fraction': 0.8,
'min_split_gain': 0.01,
'min_child_samples': 150,
'min_child_weight': 0.1,
'verbosity': -1,
'data_random_seed': 3,
'early_stop': 100,
'verbose_eval': 100,
'num_rounds': 3000}
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
 fold_preds = np.zeros(( testing.shape[0],))
oof_preds = np.zeros(( X.shape[0],))
all_coefficients = np.zeros(( 5, 4))
lgtrain.construct()
modelstart = time.time()
for trn_idx, val_idx in folds.split(X, y):
clf = lgb.train(
 params=lgbm_params,
train_set=lgtrain.subset(trn_idx),
valid_sets=lgtrain.subset(val_idx),
categorical_feature = categorical_vars,
num_boost_round=1500,
early_stopping_rounds=150,
verbose_eval=300 
)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = feature_names
fold_importance_df["importance"] = clf.feature_importance(importance_type='gain')
 fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 pred_test_y = clf.predict(X[val_idx], num_iteration=clf.best_iteration)
 optR = OptimizedRounder()
optR.fit(pred_test_y, y.iloc[val_idx].values)
 coefficients = optR.coefficients()
pred_test_y_k = optR.predict(pred_test_y, coefficients)
 all_coefficients[n_fold, :] = coefficients
print("Valid Counts = ", Counter(y.iloc[val_idx].values))
print("Predicted Counts = ", Counter(pred_test_y_k))
print("Coefficients = ", coefficients)
 qwk = quadratic_weighted_kappa(y.iloc[val_idx].values, pred_test_y_k)
 print("QWK = ", qwk)
 print('Predict 2/2')
 fold_preds += clf.predict(testing, num_iteration=clf.best_iteration)/ folds.n_splits
n_fold += 1
print("Model Runtime: %0.2f Minutes"%(( time.time() - modelstart)/60))
seed = 42
print(f'setting everything to seed {seed}')
 random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True<define_variables>
optR = OptimizedRounder()
coefficients_ = np.mean(all_coefficients, axis=0)
 print(coefficients_)
 optR = OptimizedRounder()
test_predictions = optR.predict(fold_preds, coefficients_ ).astype(int)
 Counter(test_predictions)
 submission = pd.DataFrame({'PetID': testdex, 'AdoptionSpeed': test_predictions})
 submission.to_csv('submission.csv', index=False)
 submission.head()
<load_from_csv><EOS> 
print("Train Dependent Variable Class Distribution:
", y.value_counts())
 print("Prediction Dependent Variable Class Distribution:
", submission['AdoptionSpeed'].value_counts())
 print("Notebook Runtime: %0.2f Minutes"%(( time.time() - notebookstart)/60))