Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<compute_test_metric>
%matplotlib inline
def sigmoid(x):
return 1.0 /(1.0 + np.exp(-x))
bert_pred = np.array(bert_pred_lst ).mean(0)
 cased_pred = np.array(cased_pred_lst ).mean(0)
 xlnet_pred = np.array(xlnet_pred_lst ).mean(0)
 bert_pred = sigmoid(bert_pred)
 cased_pred = sigmoid(cased_pred)
 xlnet_pred = sigmoid(xlnet_pred)
 test_pred = bert_pred*0.4 + cased_pred*0.2 + xlnet_pred*0.4<load_pretrained>
train_df = pd.read_csv('.. /input/train.csv')
 train_df.fillna(0,inplace=True)
 train_df
lgbm_models = pickle.load(open(".. /input/quest-lgbm/lgbm_question_type_spelling.pkl", 'rb'))
count_vectorizers = pickle.load(open(".. /input/quest-lgbm/tfidf_vectorizers.pkl", 'rb'))<concatenate>
le = LabelEncoder().fit(train_df.species)
 labels = le.transform(train_df.species)
 labels
dfs = []
for idx, col_name in enumerate(["question_title", "question_body", "answer"]):
X = count_vectorizers[idx].transform(test[col_name])
 feat = [f"{col_name}_{c}".encode("utf-8")for c in count_vectorizers[idx].get_feature_names() ]
df = pd.DataFrame(X.toarray() , columns=feat)
 dfs.append(df)
 test_x = pd.concat(dfs, axis=1)
 test_x.shape<predict_on_test>
df.ix[:,2:] = MinMaxScaler().fit_transform(train_df.ix[:,2:])
 df
lgbm_pred = np.zeros(len(test_x))
for fold in range(4):
lgbm_pred += lgbm_models[fold].predict(test_x)/4
lgbm_pred.shape<compute_test_metric>
X = df.as_matrix() [:,2:]
y = df.as_matrix() [:,1]
def spearman_corr(y_true, y_pred):
if np.ndim(y_pred)== 2:
corr = np.nan_to_num([stats.spearmanr(y_true[:, i], y_pred[:, i])[0] for i in range(y_true.shape[1])] ).mean()
else:
corr = stats.spearmanr(y_true, y_pred)[0]
return corr
class OptimizedRounder(object):
def __init__(self,
n_overall: int = 5,
n_classwise: int = 5,
n_classes: int = 7,
metric: str = "qwk"):
self.n_overall = n_overall
self.n_classwise = n_classwise
self.n_classes = n_classes
self.coef = [1.0 / n_classes * i for i in range(1, n_classes)]
self.metric_str = metric
self.metric = spearman_corr
def _loss(self, X: np.ndarray, y: np.ndarray)-> float:
X_p = np.digitize(X, self.coef)
 ll = -self.metric(y, X_p)
 return ll
def fit(self, X: np.ndarray, y: np.ndarray):
golden1 = 0.618
golden2 = 1 - golden1
ab_start = [ 
(0.01, 1.0 / self.n_classes + 0.05),
]
for i in range(1, self.n_classes):
ab_start.append(( i * 1.0 / self.n_classes + 0.05, 
(i + 1)* 1.0 / self.n_classes + 0.05))
for _ in range(self.n_overall):
for idx in range(self.n_classes - 1):
a, b = ab_start[idx]
self.coef[idx] = a
la = self._loss(X, y)
 self.coef[idx] = b
lb = self._loss(X, y)
 for it in range(self.n_classwise):
if la > lb:
a = b -(b - a)* golden1
self.coef[idx] = a
la = self._loss(X, y)
 else:
b = b -(b - a)* golden2
self.coef[idx] = b
lb = self._loss(X, y)
 def predict(self, X: np.ndarray)-> np.ndarray:
X_p = np.digitize(X, self.coef)
 return X_p<predict_on_test>
params = {'C':[1500, 2000, 2500], 'tol': [0.0001]}
log_reg = linear_model.LogisticRegression(solver='lbfgs', multi_class='multinomial', max_iter=400)
 clf = GridSearchCV(log_reg, params, scoring='log_loss', refit='True', n_jobs=-1, cv=5)
 clf.fit(X, y)
 print("best params: " + str(clf.best_params_))
for params, mean_score, scores in clf.grid_scores_:
print("%0.3f(+/-%0.03f)for %r" %(mean_score, scores.std() , params))
print(scores )
optR_lst = pickle.load(open(".. /input/quest-optimizedrounder/optR_lst_10fold_ensemble_v3.pkl", 'rb'))
lst = []
for idx, optR in enumerate(optR_lst):
coeff = optR.predict(test_pred[:, idx])
 lst.append(coeff/norm_dict[sub.columns[1:][idx]])
 opt_preds = np.array(lst ).T
test_pred = opt_preds
test_pred[:,19] = lgbm_pred<feature_engineering>
estimator = clf
test_pred[test[test["category"] != "CULTURE"].index, 19] = 0.0
test["host_info"] = test["question_user_page"].map(lambda x: x.split("/")[2].replace(".stackexchange.com", ""))
test_pred[test[test["host_info"].map(lambda x: x not in ["english", "ell"])].index, 19] = 0.0<save_to_csv>
test_data = pd.read_csv('.. /input/test.csv')
 test_df = DataFrame(MinMaxScaler().fit_transform(test_data.ix[:,1:]))
test_df
sub[sub.columns[1:]] = test_pred
sub.to_csv("submission.csv", index=False)
 sub.head()<install_modules>
species = train_df.species.unique()
species.sort()
predict = estimator.predict_proba(test_df.as_matrix())
 result = DataFrame(predict,columns=species)
 result
%%time
!pip install /kaggle/input/pythonmag/mag > /dev/null
!pip install.. /input/sacremoses/sacremoses-master/ > /dev/null
!pip install /kaggle/input/sacrebleu/sacreBLEU-master/ > /dev/null
!pip install /kaggle/input/fairseq-hacked/fairseq > /dev/null<import_modules>
result.insert(0,'id',test_data.id)
 result.to_csv('result.csv',index=False )