Kaggle competition: PetFinder.my Adoption Prediction
<SOS> metric: QuadraticWeightedKappa Kaggle data source: petfinder.my-adoption-prediction<set_options>
warnings.simplefilter(action='ignore', category=FutureWarning)
 sns.set_style('darkgrid')
 sns.set(rc={'figure.figsize':(12, 10)})
 print(os.listdir(".. /input"))
del df_train
gc.collect<categorify>
train_df = pd.read_csv('.. /input/train/train.csv')
 test_df = pd.read_csv('.. /input/test/test.csv')
 y = train_df['AdoptionSpeed']
train_df['dataset_type'] = 'train'
test_df['dataset_type'] = 'test'
train_id = train_df['PetID']
test_id = test_df['PetID']
all_data0 = pd.concat([train_df, test_df])
 all_data = all_data0.drop(columns = 'AdoptionSpeed')
 train_df.head()
NAME_MIN_DF=10
MAX_FEAT_DESCP=50000
print("Encodings")
 print("Condition Encoders")
 vect_condition=LabelBinarizer(sparse_output=True)
 X_Condition=vect_condition.fit_transform(df["item_condition_id"])
 print("Shipping Encoders")
 vect_shipping=LabelBinarizer(sparse_output=True)
 X_Shipping=vect_shipping.fit_transform(df["shipping"])
 print("Name Encoders")
 count_name=CountVectorizer(min_df=NAME_MIN_DF)
 X_name=count_name.fit_transform(df["name"])
 print("Category Encoders")
 count_category=CountVectorizer()
X_category=count_category.fit_transform(df["category_name"])
 print("Brand Encoders")
 count_brand=CountVectorizer()
X_brand=count_brand.fit_transform(df["brand_name"])
 print("Descp Encoders")
 count_descp=TfidfVectorizer(max_features=MAX_FEAT_DESCP,
ngram_range=(1,3),
stop_words="english")
 X_descp=count_descp.fit_transform(df["item_description"])
<set_options>
train_df['Mixed_Breed'] = train_df.apply(lambda x: 0 if x.Breed2==0 and x.Breed1!=307 else 1, axis=1)
 train_df['Num_Color'] = train_df.apply(lambda x: 3-sum([y==0 for y in [x.Color1, x.Color2, x.Color3]]), axis=1)
 train_df['Description'].fillna("", inplace=True)
 train_df['Description_Length'] = train_df.Description.map(len )
del df
gc.collect 
<concatenate>
test_df['Mixed_Breed'] = test_df.apply(lambda x: 0 if x.Breed2==0 and x.Breed1!=307 else 1, axis=1)
 test_df['Num_Color'] = test_df.apply(lambda x: 3-sum([y==0 for y in [x.Color1, x.Color2, x.Color3]]), axis=1)
 test_df['Description'].fillna("", inplace=True)
 test_df['Description_Length'] = test_df.Description.map(len )
X=scipy.sparse.hstack([X_Shipping,X_Condition,X_brand,
X_category,X_descp,X_name] ).tocsr() 
<drop_column>
def get_desc_anly(type, recalc):
if recalc == 1:
if type == "train":
path = ".. /input/train_sentiment/"
elif type == "test":
path = ".. /input/test_sentiment/"
print("Getting description sentiment analysis for",type+"_sentiment.csv")
 files = [f for f in os.listdir(path)if(f.endswith('.json')& os.path.isfile(path+f)) ]
df = pd.DataFrame(columns=["PetID", "DescScore", "DescMagnitude"])
 i = 0
for f in files:
with open(path+f, encoding="utf8")as json_data:
data = json.load(json_data)
 df.loc[i]= [f[:-5],data["documentSentiment"]["score"],data["documentSentiment"]["magnitude"]]
i = i+1
df.to_csv(type+"_sentiment.csv", index=False)
 elif recalc == 0:
df = pd.read_csv(type+"_sentiment.csv")
 return df
train_snt = get_desc_anly("train", 1)
 test_snt = get_desc_anly("test", 1)
 train_df = train_df.set_index("PetID" ).join(train_snt.set_index("PetID")).reset_index()
test_df = test_df.set_index("PetID" ).join(test_snt.set_index("PetID")).reset_index()
train_df["DescScore"].fillna(0, inplace=True)
 train_df["DescMagnitude"].fillna(0, inplace=True)
 train_df["Name"].fillna("none", inplace=True)
 test_df["DescScore"].fillna(0, inplace=True)
 test_df["DescMagnitude"].fillna(0, inplace=True)
 test_df["Name"].fillna("", inplace=True)
 train_df["NameLength"] = train_df["Name"].str.len()
test_df["NameLength"] = test_df["Name"].str.len()
del X_descp
del X_brand
del X_category
del X_name
del X_Shipping
del X_Condition
gc.collect 
<prepare_x_and_y>
vertex_xs = []
vertex_ys = []
bounding_confidences = []
bounding_importance_fracs = []
dominant_blues = []
dominant_greens = []
dominant_reds = []
dominant_pixel_fracs = []
dominant_scores = []
label_descriptions = []
label_scores = []
nf_count = 0
nl_count = 0
for pet in train_id:
try:
with open('.. /input/train_metadata/' + pet + '-1.json', 'r')as f:
data = json.load(f)
 vertex_x = data['cropHintsAnnotation']['cropHints'][0]['boundingPoly']['vertices'][2]['x']
vertex_xs.append(vertex_x)
 vertex_y = data['cropHintsAnnotation']['cropHints'][0]['boundingPoly']['vertices'][2]['y']
vertex_ys.append(vertex_y)
 bounding_confidence = data['cropHintsAnnotation']['cropHints'][0]['confidence']
bounding_confidences.append(bounding_confidence)
 bounding_importance_frac = data['cropHintsAnnotation']['cropHints'][0].get('importanceFraction', -1)
 bounding_importance_fracs.append(bounding_importance_frac)
 dominant_blue = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['color']['blue']
dominant_blues.append(dominant_blue)
 dominant_green = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['color']['green']
dominant_greens.append(dominant_green)
 dominant_red = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['color']['red']
dominant_reds.append(dominant_red)
 dominant_pixel_frac = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['pixelFraction']
dominant_pixel_fracs.append(dominant_pixel_frac)
 dominant_score = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['score']
dominant_scores.append(dominant_score)
 if data.get('labelAnnotations'):
label_description = data['labelAnnotations'][0]['description']
label_descriptions.append(label_description)
 label_score = data['labelAnnotations'][0]['score']
label_scores.append(label_score)
 else:
nl_count += 1
label_descriptions.append('nothing')
 label_scores.append(-1)
 except FileNotFoundError:
nf_count += 1
vertex_xs.append(-1)
 vertex_ys.append(-1)
 bounding_confidences.append(-1)
 bounding_importance_fracs.append(-1)
 dominant_blues.append(-1)
 dominant_greens.append(-1)
 dominant_reds.append(-1)
 dominant_pixel_fracs.append(-1)
 dominant_scores.append(-1)
 label_descriptions.append('nothing')
 label_scores.append(-1)
 print(nf_count)
 print(nl_count)
 train_df.loc[:, 'vertex_x'] = vertex_xs
train_df.loc[:, 'vertex_y'] = vertex_ys
train_df.loc[:, 'bounding_confidence'] = bounding_confidences
train_df.loc[:, 'bounding_importance'] = bounding_importance_fracs
train_df.loc[:, 'dominant_blue'] = dominant_blues
train_df.loc[:, 'dominant_green'] = dominant_greens
train_df.loc[:, 'dominant_red'] = dominant_reds
train_df.loc[:, 'dominant_pixel_frac'] = dominant_pixel_fracs
train_df.loc[:, 'dominant_score'] = dominant_scores
train_df.loc[:, 'label_description'] = label_descriptions
train_df.loc[:, 'label_score'] = label_scores
vertex_xs = []
vertex_ys = []
bounding_confidences = []
bounding_importance_fracs = []
dominant_blues = []
dominant_greens = []
dominant_reds = []
dominant_pixel_fracs = []
dominant_scores = []
label_descriptions = []
label_scores = []
nf_count = 0
nl_count = 0
for pet in test_id:
try:
with open('.. /input/test_metadata/' + pet + '-1.json', 'r')as f:
data = json.load(f)
 vertex_x = data['cropHintsAnnotation']['cropHints'][0]['boundingPoly']['vertices'][2]['x']
vertex_xs.append(vertex_x)
 vertex_y = data['cropHintsAnnotation']['cropHints'][0]['boundingPoly']['vertices'][2]['y']
vertex_ys.append(vertex_y)
 bounding_confidence = data['cropHintsAnnotation']['cropHints'][0]['confidence']
bounding_confidences.append(bounding_confidence)
 bounding_importance_frac = data['cropHintsAnnotation']['cropHints'][0].get('importanceFraction', -1)
 bounding_importance_fracs.append(bounding_importance_frac)
 dominant_blue = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['color']['blue']
dominant_blues.append(dominant_blue)
 dominant_green = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['color']['green']
dominant_greens.append(dominant_green)
 dominant_red = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['color']['red']
dominant_reds.append(dominant_red)
 dominant_pixel_frac = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['pixelFraction']
dominant_pixel_fracs.append(dominant_pixel_frac)
 dominant_score = data['imagePropertiesAnnotation']['dominantColors']['colors'][0]['score']
dominant_scores.append(dominant_score)
 if data.get('labelAnnotations'):
label_description = data['labelAnnotations'][0]['description']
label_descriptions.append(label_description)
 label_score = data['labelAnnotations'][0]['score']
label_scores.append(label_score)
 else:
nl_count += 1
label_descriptions.append('nothing')
 label_scores.append(-1)
 except FileNotFoundError:
nf_count += 1
vertex_xs.append(-1)
 vertex_ys.append(-1)
 bounding_confidences.append(-1)
 bounding_importance_fracs.append(-1)
 dominant_blues.append(-1)
 dominant_greens.append(-1)
 dominant_reds.append(-1)
 dominant_pixel_fracs.append(-1)
 dominant_scores.append(-1)
 label_descriptions.append('nothing')
 label_scores.append(-1)
 print(nf_count)
 test_df.loc[:, 'vertex_x'] = vertex_xs
test_df.loc[:, 'vertex_y'] = vertex_ys
test_df.loc[:, 'bounding_confidence'] = bounding_confidences
test_df.loc[:, 'bounding_importance'] = bounding_importance_fracs
test_df.loc[:, 'dominant_blue'] = dominant_blues
test_df.loc[:, 'dominant_green'] = dominant_greens
test_df.loc[:, 'dominant_red'] = dominant_reds
test_df.loc[:, 'dominant_pixel_frac'] = dominant_pixel_fracs
test_df.loc[:, 'dominant_score'] = dominant_scores
test_df.loc[:, 'label_description'] = label_descriptions
test_df.loc[:, 'label_score'] = label_scores
X_train=X[:nrow_train]
X_test=X[nrow_train:]
dtest=xgb.DMatrix(X_test )<set_options>
train_df_num = train_df.drop(columns = train_df.dtypes[train_df.dtypes=='object'].index )
del X
gc.collect<split>
test_df_num = test_df.drop(columns = test_df.dtypes[test_df.dtypes=='object'].index )
print("Cross-Validation")
 xgb_pred_val_index = np.zeros(X_train.shape[0])
 ridge_pred_val_index = np.zeros(X_train.shape[0])
 lgb_pred_val_index = np.zeros(X_train.shape[0])
 xgb_pred_all_sum=[]
ridge_pred_all_sum=[]
lgb_pred_all_sum=[]
xgb_cv_RMSLE_sum=0
ridge_cv_RMSLE_sum=0
lgb_cv_RMSLE_sum=0
folds=3
kf = KFold(n_splits=folds, random_state=1001)
 for i,(train_index, val_index)in enumerate(kf.split(X_train, Y_train)) :
x_train, x_val = X_train[train_index], X_train[val_index]
y_train, y_val = Y_train[train_index], Y_train[val_index]
dtrain=xgb.DMatrix(x_train,y_train)
 dval=xgb.DMatrix(x_val)
 deval=xgb.DMatrix(x_val,y_val)
 params = {
'booster': 'gblinear',
'objective': 'reg:linear',
'gamma': 0,
'max_depth': 10,
'lambda': 0,
'subsample': 0.85,
'colsample_bytree': 0.9,
'min_child_weight': 17,
'silent': 1,
'eta': 0.4,
'seed': 1001,
'nthread': 4,
'eval_metric':'rmse'
}
plst = params.items()
evallist = [(deval, 'eval'),(dtrain, 'train')]
num_round=300
model=xgb.train(plst,dtrain,num_round,evallist, verbose_eval=100,early_stopping_rounds=100)
 xgb_pred_val=model.predict(dval)
 xgb_RMSLE=np.sqrt(mean_squared_error(xgb_pred_val,y_val))
print('
Fold %02d XGBoost RMSLE: %.6f' %(( i + 1), xgb_RMSLE))
xgb_pred_all=model.predict(dtest)
 del dtrain
del dval
del deval
gc.collect()
params = {
'boosting': 'gbdt',
'max_depth': 7,
'min_data_in_leaf': 80,
'num_leaves': 30,
'learning_rate': 0.4,
'objective': 'regression',
'metric': 'rmse',
'nthread': 4,
'bagging_freq': 1,
'subsample': 0.9,
'colsample_bytree': 0.7,
'min_child_weight': 17,
'is_unbalance': False,
'verbose': -1,
'seed': 1001,
'max_bin':511,
'num_threads':4
}
dtrain = lgb.Dataset(x_train, label=y_train)
 deval = lgb.Dataset(x_val, label=y_val)
 watchlist = [dtrain, deval]
watchlist_names = ['train', 'val']
model = lgb.train(params,
train_set=dtrain,
num_boost_round=3000,
valid_sets=watchlist,
valid_names=watchlist_names,
early_stopping_rounds=100,
verbose_eval=300)
 lgb_pred_val = model.predict(x_val)
 lgb_RMSLE = np.sqrt(mean_squared_error(lgb_pred_val,y_val))
print(' Fold %02d LightGBM RMSLE: %.6f' %(( i + 1), lgb_RMSLE))
lgb_pred_all = model.predict(X_test)
 del dtrain
del deval
gc.collect()
model=Ridge(solver='sag',alpha=4.75)
 model.fit(x_train,y_train)
 ridge_pred_val=model.predict(x_val)
 ridge_RMSLE=np.sqrt(mean_squared_error(ridge_pred_val,y_val))
print('
Fold %02d Ridge RMSLE: %.6f' %(( i + 1), ridge_RMSLE))
ridge_pred_all=model.predict(X_test)
 del x_train
del y_train
del x_val
del y_val
gc.collect()
xgb_pred_val_index[val_index] = xgb_pred_val
ridge_pred_val_index[val_index] = ridge_pred_val
lgb_pred_val_index[val_index] = lgb_pred_val
if i > 0:
xgb_pred_all_sum = xgb_pred_all_sum + xgb_pred_all
ridge_pred_all_sum = ridge_pred_all_sum + ridge_pred_all
lgb_pred_all_sum = lgb_pred_all_sum + lgb_pred_all
else:
xgb_pred_all_sum = xgb_pred_all
ridge_pred_all_sum = ridge_pred_all
lgb_pred_all_sum = lgb_pred_all
xgb_cv_RMSLE_sum = xgb_cv_RMSLE_sum + xgb_RMSLE
ridge_cv_RMSLE_sum = ridge_cv_RMSLE_sum + ridge_RMSLE
lgb_cv_RMSLE_sum = lgb_cv_RMSLE_sum + lgb_RMSLE
xgb_cv_avg_score=xgb_cv_RMSLE_sum/folds
ridge_cv_avg_score=ridge_cv_RMSLE_sum/folds
lgb_cv_avg_score=lgb_cv_RMSLE_sum/folds
xgb_val_real_RMSLE=np.sqrt(mean_squared_error(xgb_pred_val_index,Y_train))
ridge_val_real_RMSLE=np.sqrt(mean_squared_error(ridge_pred_val_index,Y_train))
lgb_val_real_RMSLE=np.sqrt(mean_squared_error(lgb_pred_val_index,Y_train))
print('
Average XGBoost RMSLE(cv):\t%.6f' % xgb_cv_avg_score)
 print(' Out-of-fold XGBoost RMSLE:\t%.6f' % xgb_val_real_RMSLE)
 print('
Average LightGBM RMSLE(cv):\t%.6f' % lgb_cv_avg_score)
 print(' Out-of-fold LightGBM RMSLE:\t%.6f' % lgb_val_real_RMSLE)
 print('
Average Ridge RMSLE(cv):\t%.6f' % ridge_cv_avg_score)
 print(' Out-of-fold Ridge RMSLE:\t%.6f' % ridge_val_real_RMSLE)
 xgb_pred_all_avg=xgb_pred_all_sum/folds
ridge_pred_all_avg=ridge_pred_all_sum/folds
lgb_pred_all_avg=lgb_pred_all_sum/folds 
<compute_test_metric>
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score, cohen_kappa_score, make_scorer
from sklearn.model_selection import StratifiedKFold
from sklearn.utils import shuffle
def rmse_min_func(weights):
final_prediction=0
for weight,prediction in zip(weights,blend_train):
final_prediction+=weight*prediction
return np.sqrt(mean_squared_error(Y_train,final_prediction))
blend_train = []
blend_test = []
blend_train.append(xgb_pred_val_index)
 blend_train.append(lgb_pred_val_index)
 blend_train.append(ridge_pred_val_index)
 blend_train=np.array(blend_train)
 blend_test.append(xgb_pred_all_avg)
 blend_test.append(lgb_pred_all_avg)
 blend_test.append(ridge_pred_all_avg)
 blend_test=np.array(blend_test)
 print('
Finding Blending Weights...')
 res_list=[]
weight_list=[]
for k in range(20):
starting_value=np.random.uniform(-1,1,len(blend_train))
bounds=[(-1,1)]*len(blend_train)
 res=minimize(
 rmse_min_func,
starting_value,
method='L-BFGS-B',
bounds=bounds,
options={'disp':False,
'maxiter':100000})
 res_list.append(res['fun'])
 weight_list.append(res['x'])
 print('{iter}\tScore: {score}\tWeights: {weights}'.format(
 iter=(k+1),
score=round(res['fun'],6),
weights='\t'.join([str(round(item,10)) for item in res['x']])))
 bestSC=np.min(res_list)
 bestweight=weight_list[np.argmin(res_list)]
print('
Ensemble Score:{best_score}'.format(best_score=bestSC))
print('
Best Weights:{weight}'.format(weight=bestweight))
test_price=np.zeros(len(blend_test[0]))
train_price =np.zeros(len(blend_train[0]))
print('
Your final model:')
 for k in range(len(blend_test)) :
print('%.6f * model-%d'%(bestweight[k],(k+1)))
 test_price+=blend_test[k]*bestweight[k]
train_price+= blend_train[k] * bestweight[k]
df_test["price"]=np.expm1(test_price)
 print("Generatig File")
 df_test[["test_id","price"]].to_csv("submission.csv",index=False )<define_variables>
x = train_df_num.drop(columns = 'AdoptionSpeed')
 y = train_df_num.AdoptionSpeed
Image(filename='.. /input/landmark-retrieval-2020/train/0/0/0/000014b1f770f640.jpg')
<import_modules>
x_tst = test_df_num
K.set_image_data_format('channels_last')
 K.set_learning_phase(1)
<normalization>
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=0, shuffle=True )
print(device_lib.list_local_devices())
 tf.debugging.set_log_device_placement(True)
 a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
 b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
 c = tf.matmul(a, b)
 print(c )<load_from_csv>
ExtraTreesClassifier, AdaBoostClassifier
RANDOM_STATE = 0
train = pd.read_csv(".. /input/landmark-retrieval-2020/train.csv")
 def get_paths(sub):
index = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
paths = []
for a in index:
for b in index:
for c in index:
try:
paths.extend([f".. /input/landmark-retrieval-2020/{sub}/{a}/{b}/{c}/" + x for x in os.listdir(f".. /input/landmark-retrieval-2020/{sub}/{a}/{b}/{c}")])
 except:
pass
return paths<feature_engineering>
classifiers = [KNeighborsClassifier(n_jobs=-1),
LogisticRegression() ,
LogisticRegression(C=0.005, penalty='l1', class_weight=None, fit_intercept=False, max_iter=100, tol=0.01),
LogisticRegression(C=0.0001, penalty='l2', class_weight=None, fit_intercept=True, max_iter=150, tol=1e-06),
LogisticRegression(C=96, penalty='l1', class_weight='balanced', fit_intercept=True, max_iter=50, tol=0.0001),
LogisticRegression(C=54, penalty='l2', class_weight='balanced', fit_intercept=False, max_iter=450, tol=0.001),
GradientBoostingClassifier(n_estimators=50, learning_rate=0.005, max_depth=12, max_features=0.8, min_samples_leaf=2, subsample=0.2),
GradientBoostingClassifier(n_estimators=50, learning_rate=0.01, max_depth=5, max_features=0.6, min_samples_leaf=10, subsample=0.8),
GradientBoostingClassifier(n_estimators=200, learning_rate=0.001, max_depth=90, max_features=0.5, min_samples_leaf=20, subsample=0.2),
GradientBoostingClassifier(n_estimators=50, learning_rate=0.01, max_depth=20, max_features=0.6, min_samples_leaf=24, subsample=0.7),
GradientBoostingClassifier(n_estimators=300, learning_rate=0.01, max_depth=90, max_features=0.5, min_samples_leaf=20, subsample=0.2),
RandomForestClassifier(n_jobs=-1),
RandomForestClassifier(n_estimators=225, bootstrap=True, max_depth=83, max_features='auto', min_samples_leaf=5, min_samples_split=5, n_jobs=-1),
RandomForestClassifier(criterion='gini', n_estimators=100, min_samples_split=12, min_samples_leaf=1, oob_score=True, n_jobs=-1),
ExtraTreesClassifier(n_jobs=-1),
ExtraTreesClassifier(n_estimators=200, bootstrap=False, max_depth=80, max_features='auto', min_samples_leaf=6, min_samples_split=5, n_jobs=-1),
AdaBoostClassifier() ,
AdaBoostClassifier(n_estimators=225, algorithm='SAMME.R', learning_rate=0.2),
DecisionTreeClassifier() ,
DecisionTreeClassifier(criterion='entropy', max_depth=110, max_features='auto', min_samples_leaf=6, min_samples_split=330)
 ]
classifiers_names = ['knn1',
'lr1', 'lr2', 'lr3', 'lr4', 'lr5',
'gb1', 'gb2', 'gb3', 'gb4', 'gb5',
'rf1', 'rf2', 'rf3',
'et1', 'et2',
'adb1', 'adb2',
'dt1','dt2',
]
train_path = train
train_path["id"] = train_path.id.map(lambda path: f".. /input/landmark-retrieval-2020/train/{path[0]}/{path[1]}/{path[2]}/{path}.jpg" )<feature_engineering>
classifiers_predictions = pd.DataFrame()
for name, classifier in zip(classifiers_names, classifiers):
classifier.fit(X_train, y_train)
 train_predictions = pd.Series(classifier.predict(X_train))
test_predictions = classifier.predict(X_test)
 classifiers_predictions[name] = test_predictions
print('{0}:({1} - {2})'.format(name,
cohen_kappa_score(y_train, train_predictions, weights='quadratic'),
cohen_kappa_score(y_test, test_predictions, weights='quadratic')) )
<define_variables>
classifiers_predictions = classifiers_predictions[['knn1',
'lr1', 'lr2', 'lr3', 'lr4', 'lr5',
'gb1', 'gb2', 'gb3', 'gb4', 'gb5',
'rf1', 'rf2', 'rf3',
'et1', 'et2',
'adb1', 'adb2',
'dt1','dt2',
]]
batch_size = 4
seed = 42
IMAGE_SIZE = 128
shape =(IMAGE_SIZE, IMAGE_SIZE, 3)
 val_sample = 0.2 
<load_from_csv>
classifiers = [
LogisticRegression(C=96, penalty='l1', class_weight='balanced', fit_intercept=True, max_iter=50, tol=0.0001),
GradientBoostingClassifier(n_estimators=50, learning_rate=0.005, max_depth=12, max_features=0.8, min_samples_leaf=2, subsample=0.2),
GradientBoostingClassifier(n_estimators=50, learning_rate=0.01, max_depth=5, max_features=0.6, min_samples_leaf=10, subsample=0.8),
GradientBoostingClassifier(n_estimators=200, learning_rate=0.001, max_depth=90, max_features=0.5, min_samples_leaf=20, subsample=0.2),
GradientBoostingClassifier(n_estimators=50, learning_rate=0.01, max_depth=20, max_features=0.6, min_samples_leaf=24, subsample=0.7),
RandomForestClassifier(n_estimators=225, bootstrap=True, max_depth=83, max_features='auto', min_samples_leaf=5, min_samples_split=5, n_jobs=-1),
RandomForestClassifier(criterion='gini', n_estimators=100, min_samples_split=12, min_samples_leaf=1, oob_score=True, n_jobs=-1),
ExtraTreesClassifier(n_estimators=200, bootstrap=False, max_depth=80, max_features='auto', min_samples_leaf=6, min_samples_split=5, n_jobs=-1),
AdaBoostClassifier() ,
AdaBoostClassifier(n_estimators=225, algorithm='SAMME.R', learning_rate=0.2),
DecisionTreeClassifier(criterion='entropy', max_depth=110, max_features='auto', min_samples_leaf=6, min_samples_split=330)
 ]
classifiers_names = ['lr4',
'gb1','gb2', 'gb3', 'gb4',
'rf2', 'rf3',
'et2',
'adb1','adb2',
'dt2',
]
train_labels = pd.read_csv('.. /input/landmark-retrieval-2020/train.csv')
 train_labels.head()<create_dataframe>
def simple_blending(basic_algorithms, meta_algorithm, X_train, X_test, y_train, test_df, part1_ratio=0.9, random_state=None):
tr = pd.DataFrame()
tst = pd.DataFrame()
y = pd.DataFrame()
X_train_part1, X_train_part2,\
y_train_part1, y_train_part2 = train_test_split(X_train, y_train, test_size=1-part1_ratio, random_state=random_state)
 for index, basic_algorithm in enumerate(basic_algorithms):
basic_algorithm.fit(X_train_part1, y_train_part1)
 part2_predictions = basic_algorithm.predict(X_train_part2)
 tr[index] = part2_predictions
test_predictions = basic_algorithm.predict(X_test)
 tst[index] = test_predictions
test_pred = basic_algorithm.predict(test_df)
 y[index] = test_pred
meta_algorithm.fit(tr, y_train_part2)
 return meta_algorithm.predict(tst), meta_algorithm.predict(y )
k =train[['id','landmark_id']].groupby(['landmark_id'] ).agg({'id':'count'})
 k.rename(columns={'id':'Count_class'}, inplace=True)
 k.reset_index(level=(0), inplace=True)
 freq_ct_df = pd.DataFrame(k)
 freq_ct_df.head()<merge>
r = pd.DataFrame()
experiments = list()
for i in range(1, 10):
simple_blending_predictions, result = simple_blending(classifiers,
LogisticRegression(C=5, random_state=RANDOM_STATE),
X_train, X_test, y_train, x_tst,
part1_ratio=0.9,
random_state=i)
 r[i] = result
print(cohen_kappa_score(y_test, simple_blending_predictions, weights='quadratic'))
experiments.append(cohen_kappa_score(y_test, simple_blending_predictions, weights='quadratic'))
print('mean kappa: {0}
std: {1}'.format(round(np.mean(experiments), 4), round(np.std(experiments), 5)) )
train_labels = pd.merge(train,freq_ct_df, on = ['landmark_id'], how='left')
 train_labels.head()<sort_values>
r['avg'] = r.mean(axis=1 ).round(0 ).astype(int)
 r.avg.head()
freq_ct_df.sort_values(by=['Count_class'],ascending=False,inplace=True)
 freq_ct_df.head()<define_variables>
def simple_blending_features(basic_algorithms, meta_algorithm, X_train, X_test, y_train, test_df, part1_ratio=0.5, random_state=None):
tr = pd.DataFrame()
tst = pd.DataFrame()
y = pd.DataFrame()
X_train_part1, X_train_part2,\
y_train_part1, y_train_part2 = train_test_split(X_train, y_train, test_size=1-part1_ratio, random_state=random_state)
 tr = tr.append(X_train_part2)
 tst = tst.append(X_test)
 y = y.append(test_df)
 for index, basic_algorithm in enumerate(basic_algorithms):
basic_algorithm.fit(X_train_part1, y_train_part1)
 part2_predictions = basic_algorithm.predict(X_train_part2)
 tr[index] = part2_predictions
test_predictions = basic_algorithm.predict(X_test)
 tst[index] = test_predictions
test_pred = basic_algorithm.predict(test_df)
 y[index] = test_pred
meta_algorithm.fit(tr, y_train_part2)
 return meta_algorithm.predict(tst), meta_algorithm.predict(y )
TOP = 40000<split>
r2 = pd.DataFrame()
experiments = list()
for i in range(1, 10):
simple_blending_features_predictions, result2 = simple_blending_features(classifiers,
LogisticRegression(C=5, random_state=RANDOM_STATE),
X_train, X_test, y_train, x_tst,
part1_ratio=0.9,
random_state=i)
 r2[i] = result2
print(cohen_kappa_score(y_test, simple_blending_features_predictions, weights='quadratic'))
experiments.append(cohen_kappa_score(y_test, simple_blending_features_predictions, weights='quadratic'))
print('mean kappa: {0}
std: {1}'.format(round(np.mean(experiments), 4), round(np.std(experiments), 5)) )
freq_ct_df_top = freq_ct_df.iloc[:TOP]
top_class = freq_ct_df_top['landmark_id'].tolist()<categorify>
r2['avg'] = r2.mean(axis=1 ).round(0 ).astype(int)
 r2.avg.head()
def getTrainParams() :
print("Encoding labels")
 data = topclass_train.copy()
le = preprocessing.LabelEncoder()
print("fitting LabelENcoder")
 data['label'] = le.fit_transform(data['landmark_id'])
 print("Success in LabelENcoder")
 lbls = topclass_train['landmark_id'].tolist()
lb = LabelBinarizer(sparse_output=False)
 print("fitting LabelBinarizer")
 labels = lb.fit_transform(lbls)
 print("Success in LabelBinarizer")
 x = np.array(topclass_train['id'].tolist())
 print("id to array")
 y = labels
print("labels to array")
 return x,y,le<define_variables>
submission = pd.DataFrame(data={'PetID' : test_df['PetID'], 'AdoptionSpeed' : r['avg']})
 submission.to_csv('submission.csv', index=False)