Kaggle competition: PetFinder.my Adoption Prediction
<SOS> metric: QuadraticWeightedKappa Kaggle data source: petfinder.my-adoption-prediction<load_pretrained>
%matplotlib inline
archive_train = zipfile.ZipFile("/kaggle/input/whats-cooking/train.json.zip", 'r')
 archive_submission = zipfile.ZipFile("/kaggle/input/whats-cooking/test.json.zip", 'r' )<load_from_disk>
import scipy as sp
from collections import Counter
from functools import partial
dataset_train = pd.read_json(archive_train.read('train.json'))
dataset_submission = pd.read_json(archive_submission.read('test.json'))
dataset_train_copy = dataset_train.copy()
dataset_submission_copy = dataset_submission.copy()<feature_engineering>
data_path=".. /input/petfinder-adoption-prediction"
first_kernel_path=".. /input/pets-adoption-simple-pandas-random-forest"
image_kernel_path=".. /input/pet-adoption-only-images"
svd_kernel_path=".. /input/pet-adoption-only-text-svd"
train_dict_count = {}
for entry_list in dataset_train_copy['ingredients']:
for entry in entry_list:
if entry in train_dict_count.keys() :
train_dict_count[entry] += 1
else:
train_dict_count[entry] = 1<feature_engineering>
train = pd.read_csv(data_path+"/train/train.csv")
 test = pd.read_csv(data_path+"/test/test.csv")
 color_labels = pd.read_csv(data_path+"/color_labels.csv")
 breed_labels = pd.read_csv(data_path+"/breed_labels.csv")
 state_labels = pd.read_csv(data_path+"/state_labels.csv" )
submission_dict_count = {}
for entry_list in dataset_submission_copy['ingredients']:
for entry in entry_list:
if entry in submission_dict_count.keys() :
submission_dict_count[entry] += 1
else:
submission_dict_count[entry] = 1<feature_engineering>
warnings.filterwarnings('ignore' )
for key in train_dict_count:
if key not in submission_dict_count.keys() :
submission_dict_count[key] = 0<feature_engineering>
df_all0 = pd.read_csv(first_kernel_path+"/df_all0.csv")
 df_all0.head()
for cols in train_dict_count.keys() :
dataset_train_copy[cols] = np.zeros(( dataset_train_copy.shape[0], 1))<feature_engineering>
txt_data = pd.read_csv(first_kernel_path+"/txt_data.csv")
 txt_data.columns = ['PetID','sent_magnitude','sent_score','sent_language']
txt_data.head()
for cols in submission_dict_count.keys() :
dataset_submission_copy[cols] = np.zeros(( dataset_submission_copy.shape[0], 1))<feature_engineering>
img_df1a = pd.read_csv(image_kernel_path+"/img_df1a_local.csv")
 img_df1a.columns = ['PetID','ImageID','img_met_score','img_met_description']
img_df1a.head()
for column_name in dataset_train_copy.columns:
if column_name not in ['id', 'cuisine', 'ingredients']:
for index, each_entry_list in enumerate(dataset_train_copy['ingredients']):
if column_name in each_entry_list:
dataset_train_copy.loc[index, column_name] = 1<feature_engineering>
img_df1c = pd.read_csv(image_kernel_path+"/img_df1c_local.csv")
 img_df1c.columns = ['PetID','ImageID','img_crp_x','img_crp_y','img_crp_conf','img_crp_if']
img_df1c.head()
for column_name in dataset_submission_copy.columns:
if column_name not in ['id', 'ingredients']:
for index, each_entry_list in enumerate(dataset_submission_copy['ingredients']):
if column_name in each_entry_list:
dataset_submission_copy.loc[index, column_name] = 1<drop_column>
img_df1p = pd.read_csv(image_kernel_path+"/img_df1p_local.csv")
 img_df1p.columns = ['PetID','ImageID','img_par_red','img_par_green','img_par_blue','img_par_score','img_par_pf']
img_df1p.head()
dataset_submission_copy = dataset_submission_copy[dataset_train_copy.drop('cuisine', axis=1 ).columns]<drop_column>
des_svd_df = pd.read_csv(svd_kernel_path+"/des_svd_df.csv")
 des_svd_df.iloc[:,0:10].head()
dataset_train_copy.drop(['id', 'ingredients', 'cuisine'], axis=1, inplace=True)
 dataset_submission_copy.drop(['id', 'ingredients'], axis=1, inplace=True )<prepare_x_and_y>
prev_subm = pd.read_csv(".. /input/pets-adoption-simple-pandas-random-forest/submission.csv")
 prev_subm.head()
X = dataset_train_copy.values
y = dataset_train.iloc[:,1].values
X_submission = dataset_submission_copy.values<categorify>
rescuers=df_all0.groupby(by='RescuerID')['RescuerID'].count()
df_rescuers=pd.DataFrame(rescuers)
 df_rescuers.columns=['ResLev']
df_rescuers.reset_index(inplace=True)
 df_rescuers.head()
le = LabelEncoder()
y = le.fit_transform(y)
 print(y )<train_model>
dfm=df_all0.merge(df_rescuers,on='RescuerID')
 numeric_cols=['Age','PhotoAmt','Quantity','Fee','DescriptionLength','ResLev']
categorical_cols=['Sterilized','FurLength','Breed1','State','AdoptionSpeed','Breed2','MaturitySize','Gender','Dewormed','Color1','Color2','Color3','Health']
cols=['PetID']+numeric_cols+categorical_cols
dfm=dfm[cols]
dfm.shape
classifier = LogisticRegression(random_state = 0)
 classifier.fit(X, y )<save_to_csv>
dfm=dfm.merge(txt_data,on='PetID', how='left')
 categorical_cols=categorical_cols+['sent_language']
dfm.shape
y_submission = classifier.predict(X_submission)
 y_submission = le.inverse_transform(y_submission)
 Id = dataset_submission.iloc[:,0]
submission_dataset = pd.DataFrame({'id':Id, 'cuisine':y_submission})
 submission_dataset.set_index('id', inplace=True)
 submission_dataset.to_csv('Submission.csv' )<load_from_disk>
n_svd=32
svd=des_svd_df.iloc[:,0:n_svd+3]
svd.drop('Description',axis=1,inplace=True)
 svd.drop('AdoptionSpeed',axis=1,inplace=True)
 dfm=dfm.merge(svd,on='PetID', how='left')
 dfm.shape
with open('//kaggle/input/whats-cooking/train.json', 'r')as myfile:
train_data=myfile.read()
with open('//kaggle/input/whats-cooking/test.json', 'r')as myfile:
test_data=myfile.read()
train_obj = json.loads(train_data)
 test_obj = json.loads(test_data )<define_variables>
img_df1ad=img_df1a.groupby(['PetID'])['img_met_description'].apply(', '.join ).reset_index()
ingredients=[]
categories=[]
recipeCount=0
for recipe in train_obj:
recipeCount+=1
category=recipe['cuisine']
ingredientList=recipe['ingredients']
if category not in categories:
categories.append(category)
 for ingredient in ingredientList:
if ingredient not in ingredients:
ingredients.append(ingredient )<count_values>
img_df1ad['img_met_description1']=img_df1ad['img_met_description'].apply(lambda s:s.split(',')).apply(set ).apply(','.join )
print(recipeCount,'recipes, ',len(categories),'categories, ', len(ingredients),' ingredients' )<prepare_x_and_y>
img_df1ad['img_met_description1'].loc[0]
xtrain=np.zeros(( recipeCount,len(ingredients)))
 ytrain=np.empty(recipeCount,dtype=object)
 for i in range(recipeCount):
ytrain[i]=train_obj[i]['cuisine']
for ingredient in train_obj[i]['ingredients']:
index=ingredients.index(ingredient)
 xtrain[i][index]=1<feature_engineering>
img_df1ad['img_met_description'].loc[0]
l=len(test_obj)
 xtest=np.zeros(( l,len(ingredients)))
 testIndex=np.zeros(l,dtype=int)
 for i in range(l):
testIndex[i]=test_obj[i]['id']
for ingredient in test_obj[i]['ingredients']:
if ingredient in ingredients:
index=ingredients.index(ingredient)
 xtest[i][index]=1<train_model>
img_df1ad.drop('img_met_description',axis=1,inplace=True)
 img_df1ad.head()
lr=LogisticRegression(random_state=0, solver='sag',multi_class='auto')
 ypred=lr.fit(xtrain,ytrain ).predict(xtest )<save_to_csv>
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
assert(testIndex.size==ypred.size)
 data = {'id':testIndex, 'cuisine':ypred}
submit = pd.DataFrame(data)
 submit.to_csv('submission.csv',index = None, header=True )<data_type_conversions>
def find_svd(df,txt_col_name,n_comp):
tfv = TfidfVectorizer(analyzer='word', stop_words = 'english', token_pattern=r'\b[a-zA-Z]\w+\b',
min_df=1, max_features=10000, strip_accents='unicode',
ngram_range=(1, 32), use_idf=1, smooth_idf=1, sublinear_tf=1,)
 corpus=list(df[txt_col_name])
 txt_trasf=tfv.fit_transform(corpus)
 svd = TruncatedSVD(n_components=n_comp, n_iter=10, tol=1.0)
 svd.fit(txt_trasf)
 txt_svd=svd.transform(txt_trasf)
 txt_svd_df=pd.DataFrame(txt_svd)
 return(txt_svd_df )
train_df = pd.read_json(".. /input/train.json")
 categories = sorted(train_df['cuisine'].astype("category" ).unique())
 train_df['cuisine'] = train_df['cuisine'].astype("category" ).cat.codes
train_df['ingredients'] = train_df['ingredients'].apply(lambda x: " ".join(x))<prepare_x_and_y>
nc=16
img_met_svd=find_svd(img_df1ad,'img_met_description1',nc)
 img_met_svd.columns=['SVD_'+str(c)for c in range(0,nc)]
img_met_svd['PetID']=img_df1ad['PetID']
train_docs = train_df['ingredients']
train_tf = TfidfVectorizer(lowercase=False, preprocessor=None)
 train_tf.fit(train_docs)
 train_X= train_tf.transform(train_docs)
 train_Y = train_df['cuisine']<train_model>
img_df1ar=img_met_svd
img_df1ar['img_met_score_max']=img_df1a.groupby(by=['PetID','ImageID'],as_index=False ).agg({'img_met_score': 'max'})['img_met_score']
img_df1ar['img_met_score_min']=img_df1a.groupby(by=['PetID','ImageID'],as_index=False ).agg({'img_met_score': 'min'})['img_met_score']
img_df1ar.head()
clf = svm.SVC(decision_function_shape='ovr')
 clf.fit(train_X, train_Y)
<prepare_x_and_y>
img_df1cr1=img_df1c[img_df1c['ImageID']==1]
img_df1cr1.drop('ImageID', axis=1, inplace=True)
 img_df1cr1.columns=['PetID','img_crp_x1','img_crp_y1','img_crp_conf1','img_crp_if1']
img_df1cr2=img_df1c[img_df1c['ImageID']==2]
img_df1cr2.drop('ImageID', axis=1, inplace=True)
 img_df1cr2.columns=['PetID','img_crp_x2','img_crp_y2','img_crp_conf2','img_crp_if2']
img_df1cr3=img_df1c[img_df1c['ImageID']==3]
img_df1cr3.drop('ImageID', axis=1, inplace=True)
 img_df1cr3.columns=['PetID','img_crp_x3','img_crp_y3','img_crp_conf3','img_crp_if3']
img_df1cr=img_df1cr1
img_df1cr.head()
test_df = pd.read_json(".. /input/test.json")
 test_df['ingredients'] = test_df['ingredients'].apply(lambda x: " ".join(x))
test_docs = test_df['ingredients']
test_vecs = train_tf.transform(test_docs)
 Y = clf.decision_function(test_vecs )<feature_engineering>
img_df1cr=img_df1cr.merge(img_df1cr2,on='PetID')
 img_df1cr=img_df1cr.merge(img_df1cr3,on='PetID')
 img_df1cr.head()
test_df['pred'] = np.argmax(Y, axis=1)
 test_df.head()<feature_engineering>
img_df1cr.isna().sum()
test_df['cuisine'] = test_df['pred'].apply(lambda x : categories[x] )<save_to_csv>
img_df1cr.fillna(-1, inplace=True )
test_df.to_csv('submission.csv',columns = ['id','cuisine'],index = False )<load_from_csv>
img_df1pg=img_df1p.groupby(by=['PetID','ImageID'],as_index=False ).agg({'img_par_score': 'max',
'img_par_red':'first',
'img_par_green':'first',
'img_par_blue':'first',
'img_par_pf':'first'})
 img_df1pg.head()
glove_submission = pd.read_csv(".. /input/bi-gru-cnn-poolings-gpu-kernel-version/submission.csv")
 fasttext_submission = pd.read_csv(".. /input/bi-gru-lstm-cnn-poolings-fasttext/submission.csv" )<define_variables>
img_df1pr1=img_df1pg[img_df1pg['ImageID']==1]
img_df1pr1.drop('ImageID', axis=1, inplace=True)
 img_df1pr1.columns=['PetID','img_par_red1','img_par_green1','img_par_blue1','img_par_pf1','img_par_score1']
img_df1pr2=img_df1pg[img_df1pg['ImageID']==2]
img_df1pr2.drop('ImageID', axis=1, inplace=True)
 img_df1pr2.columns=['PetID','img_par_red2','img_par_green2','img_par_blue2','img_par_pf2','img_par_score2']
img_df1pr3=img_df1pg[img_df1pg['ImageID']==3]
img_df1pr3.drop('ImageID', axis=1, inplace=True)
 img_df1pr3.columns=['PetID','img_par_red3','img_par_green3','img_par_blue3','img_par_pf3','img_par_score3']
img_df1pr=img_df1pr1
img_df1pr.head()
categories = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']<prepare_output>
img_df1pr=img_df1pr.merge(img_df1pr2,on='PetID')
 img_df1pr=img_df1pr.merge(img_df1pr3,on='PetID')
 img_df1pr.head()
blend = fasttext_submission.copy()
blend[categories] =(0.5*fasttext_submission[categories].values +
0.5*glove_submission[categories].values)
 blend.head()<save_to_csv>
img=img_df1ar
img=img.merge(img_df1cr,on=['PetID'], how='left')
 img=img.merge(img_df1pr,on=['PetID'], how='left')
 img.shape
blend.to_csv("blend.csv", index=False )<import_modules>
img.fillna(-1,inplace=True)
 img.head()
np.random.seed(42)
 warnings.filterwarnings('ignore')
 os.environ['OMP_NUM_THREADS'] = '4'
eng_stopwords = set(stopwords.words("english"))<load_from_csv>
dfm=dfm.merge(img,on='PetID', how='left')
 dfm.shape
train = pd.read_csv(".. /input/processing-helps-boosting-about-0-0005-on-lb/train_processed.csv")
 test = pd.read_csv(".. /input/processing-helps-boosting-about-0-0005-on-lb/test_processed.csv")
<prepare_x_and_y>
df_all['sent_magnitude'].fillna(-1, inplace=True)
 df_all['sent_score'].fillna(-1, inplace=True)
 df_all['sent_language'].fillna('en', inplace=True )
special_character_removal = re.compile(r'[^A-Za-z\.\-\?\!\,\
def clean_text(x):
x_ascii = unidecode(x)
 x_clean = special_character_removal.sub('',x_ascii)
 return x_clean
train['clean_text'] = train['comment_text'].apply(lambda x: clean_text(str(x)))
 test['clean_text'] = test['comment_text'].apply(lambda x: clean_text(str(x)))
 X_train = train['clean_text'].fillna("something" ).values
y_train = train[["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]].values
X_test = test['clean_text'].fillna("something" ).values 
<feature_engineering>
df_all.fillna(-1, inplace=True )
def add_features(df):
df['comment_text'] = df['comment_text'].apply(lambda x:str(x))
df['total_length'] = df['comment_text'].apply(len)
 df['capitals'] = df['comment_text'].apply(lambda comment: sum(1 for c in comment if c.isupper()))
df['caps_vs_length'] = df.apply(lambda row: float(row['capitals'])/float(row['total_length']),
axis=1)
 df['num_words'] = df.comment_text.str.count('\S+')
 df['num_unique_words'] = df['comment_text'].apply(lambda comment: len(set(w for w in comment.split())))
 df['words_vs_unique'] = df['num_unique_words'] / df['num_words']
return df
train = add_features(train)
 test = add_features(test)
 features = train[['caps_vs_length', 'words_vs_unique']].fillna(0)
 test_features = test[['caps_vs_length', 'words_vs_unique']].fillna(0)
 ss = StandardScaler()
ss.fit(np.vstack(( features, test_features)))
 features = ss.transform(features)
 test_features = ss.transform(test_features )<string_transform>
df_all[categorical_cols]=df_all[categorical_cols].apply(lambda c : c.astype('category'))
max_features = 10000
maxlen = 50
tokenizer = text.Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(X_train)+ list(X_test))
X_train_sequence = tokenizer.texts_to_sequences(X_train)
 X_test_sequence = tokenizer.texts_to_sequences(X_test)
 x_train = sequence.pad_sequences(X_train_sequence, maxlen=maxlen)
 x_test = sequence.pad_sequences(X_test_sequence, maxlen=maxlen)
 print(len(tokenizer.word_index))<load_pretrained>
df_all.to_csv('df_all.csv')
 df_all.head()
spell_model = gensim.models.KeyedVectors.load_word2vec_format(EMBEDDING_FILE_FASTTEXT )<feature_engineering>
df_all.dtypes[df_all.dtypes=='object']
words = spell_model.index2word
w_rank = {}
for i,word in enumerate(words):
w_rank[word] = i
WORDS = w_rank
def words(text): return re.findall(r'\w+', text.lower())
 def P(word):
"Probability of `word`."
return - WORDS.get(word, 0)
 def correction(word):
"Most probable spelling correction for word."
return max(candidates(word), key=P)
 def candidates(word):
"Generate possible spelling corrections for word."
return(known([word])or known(edits1(word)) or known(edits2(word)) or [word])
 def known(words):
"The subset of `words` that appear in the dictionary of WORDS."
return set(w for w in words if w in WORDS)
 def edits1(word):
"All edits that are one edit away from `word`."
letters = 'abcdefghijklmnopqrstuvwxyz'
splits = [(word[:i], word[i:])for i in range(len(word)+ 1)]
deletes = [L + R[1:] for L, R in splits if R]
transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]
replaces = [L + c + R[1:] for L, R in splits if R for c in letters]
inserts = [L + c + R for L, R in splits for c in letters]
return set(deletes + transposes + replaces + inserts)
 def edits2(word):
"All edits that are two edits away from `word`."
return(e2 for e1 in edits1(word)for e2 in edits1(e1))
def singlify(word):
return "".join([letter for i,letter in enumerate(word)if i == 0 or letter != word[i-1]] )<feature_engineering>
dftrain=df_all[np.invert(df_all['AdoptionSpeed']==-1)].copy()
dftest=df_all[df_all['AdoptionSpeed']==-1].copy()
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.zeros(( nb_words,501))
something_tw = embeddings_index_tw.get("something")
 something_ft = embeddings_index_ft.get("something")
 something = np.zeros(( 501,))
something[:300,] = something_ft
something[300:500,] = something_tw
something[500,] = 0
def all_caps(word):
return len(word)> 1 and word.isupper()
def embed_word(embedding_matrix,i,word):
embedding_vector_ft = embeddings_index_ft.get(word)
 if embedding_vector_ft is not None:
if all_caps(word):
last_value = np.array([1])
 else:
last_value = np.array([0])
 embedding_matrix[i,:300] = embedding_vector_ft
embedding_matrix[i,500] = last_value
embedding_vector_tw = embeddings_index_tw.get(word)
 if embedding_vector_tw is not None:
embedding_matrix[i,300:500] = embedding_vector_tw
for word, i in word_index.items() :
if i >= max_features: continue
if embeddings_index_ft.get(word)is not None:
embed_word(embedding_matrix,i,word)
 else:
if len(word)> 0:
embedding_matrix[i] = something
else:
word2 = correction(word)
 if embeddings_index_ft.get(word2)is not None:
embed_word(embedding_matrix,i,word2)
 else:
word2 = correction(singlify(word))
if embeddings_index_ft.get(word2)is not None:
embed_word(embedding_matrix,i,word2)
 else:
embedding_matrix[i] = something<compute_train_metric>
dftrain = dftrain.drop(['PetID'],axis=1)
 dftest = dftest.drop(['PetID'],axis=1 )
class RocAucEvaluation(Callback):
def __init__(self, validation_data=() , interval=1):
super(Callback, self ).__init__()
self.interval = interval
self.X_val, self.y_val = validation_data
self.max_score = 0
self.not_better_count = 0
def on_epoch_end(self, epoch, logs={}):
if epoch % self.interval == 0:
y_pred = self.model.predict(self.X_val, verbose=1)
 score = roc_auc_score(self.y_val, y_pred)
 print("
ROC-AUC - epoch: %d - score: %.6f
" %(epoch+1, score))
if(score > self.max_score):
print("*** New High Score(previous: %.6f)
 " % self.max_score)
 model.save_weights("best_weights.h5")
 self.max_score=score
self.not_better_count = 0
else:
self.not_better_count += 1
if self.not_better_count > 3:
print("Epoch %05d: early stopping, high score = %.6f" %(epoch,self.max_score))
self.model.stop_training = True<choose_model_class>
XT = dftest.drop('AdoptionSpeed',axis=1)
 y = dftrain['AdoptionSpeed']
X = dftrain.drop('AdoptionSpeed',axis=1 )
def get_model(features,clipvalue=1.,num_filters=40,dropout=0.5,embed_size=501):
features_input = Input(shape=(features.shape[1],))
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x, x_h, x_c = Bidirectional(GRU(num_filters, return_sequences=True, return_state = True))(x)
 avg_pool = GlobalAveragePooling1D()(x)
 max_pool = GlobalMaxPooling1D()(x)
 x = concatenate([avg_pool, x_h, max_pool,features_input])
 outp = Dense(6, activation="sigmoid" )(x)
 model = Model(inputs=[inp,features_input], outputs=outp)
 adam = optimizers.adam(clipvalue=clipvalue)
 model.compile(loss='binary_crossentropy',
optimizer=adam,
metrics=['accuracy'])
 return model<split>
from sklearn.model_selection import train_test_split
from sklearn.metrics import cohen_kappa_score
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import KFold
model = get_model(features)
 batch_size = 32
epochs = 1
gc.collect()
K.clear_session()
num_folds = 2
predict = np.zeros(( test.shape[0],6))
kf = KFold(n_splits=num_folds, shuffle=True, random_state=239)
 for train_index, test_index in kf.split(x_train):
kfold_y_train,kfold_y_test = y_train[train_index], y_train[test_index]
kfold_X_train = x_train[train_index]
kfold_X_features = features[train_index]
kfold_X_valid = x_train[test_index]
kfold_X_valid_features = features[test_index]
gc.collect()
K.clear_session()
model = get_model(features)
 ra_val = RocAucEvaluation(validation_data=([kfold_X_valid,kfold_X_valid_features], kfold_y_test), interval = 1)
 model.fit([kfold_X_train,kfold_X_features], kfold_y_train, batch_size=batch_size, epochs=epochs, verbose=1,
callbacks = [ra_val])
 gc.collect()
model.load_weights("best_weights.h5")
 predict += model.predict([x_test,test_features], batch_size=batch_size,verbose=1)/ num_folds
print("Done")
 sample_submission = pd.read_csv(".. /input/jigsaw-toxic-comment-classification-challenge/sample_submission.csv")
 class_names = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
sample_submission[class_names] = predict
sample_submission.to_csv('model_9872_baseline_submission.csv',index=False)
<import_modules>
import lightgbm as lgb
import numpy as np
import pandas as pd
import re, string
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.utils.validation import check_X_y, check_is_fitted
from sklearn.linear_model import LogisticRegression
from sklearn.multioutput import ClassifierChain
from sklearn.pipeline import Pipeline
from scipy import sparse<predict_on_test>
cat_features=[x for x in categorical_cols if x!='AdoptionSpeed']
class NbSvmClassifier(BaseEstimator, ClassifierMixin):
def __init__(self, C=1.0, dual=False, n_jobs=1):
self.C = C
self.dual = dual
self.n_jobs = n_jobs
self.classes_ = [0,1]
def predict(self, x):
check_is_fitted(self, ['_r', '_clf'])
 return self._clf.predict(x.multiply(self._r))
def predict_proba(self, x):
check_is_fitted(self, ['_r', '_clf'])
 return self._clf.predict_proba(x.multiply(self._r))
def fit(self, x, y):
x, y = check_X_y(x, y, accept_sparse=True)
 def pr(x, y_i, y):
p = x[y==y_i].sum(0)
 return(p+1)/(( y==y_i ).sum() +1)
 self._r = sparse.csr_matrix(np.log(pr(x,1,y)/ pr(x,0,y)))
 x_nb = x.multiply(self._r)
 self._clf = LogisticRegression(C=self.C, dual=self.dual, n_jobs=self.n_jobs ).fit(x_nb, y)
 return self<define_variables>
parameters = {'application': 'regression',
'boosting': 'gbdt',
'metric': 'rmse',
'max_bin' : 8,
'num_leaves': 12,
'max_depth': 4,
'learning_rate': 0.01,
'bagging_fraction': 0.8,
'feature_fraction': 0.8,
'min_split_gain': 0.01,
'min_child_samples': 128,
'min_child_weight': 0.1,
'data_random_seed': 123,
'verbosity': -1,
'early_stopping_rounds': 50,
'num_rounds': 3000}
evals_result={}
labels = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']<load_from_csv>
def qks(a,b):
return cohen_kappa_score(np.round(a), np.round(b), weights='quadratic' )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 sub = pd.read_csv('.. /input/sample_submission.csv' )<categorify>
kf_splits=10
k_fold = KFold(n_splits=kf_splits, shuffle=True)
 k=0
df_qks=pd.DataFrame(columns=['best_round','qks_train','qks_valid'])
 df_y=pd.DataFrame(index=XT.index)
 perf_list=[]
for train_idx, valid_idx in k_fold.split(X,y):
k=k+1
print('Step k={}'.format(k))
X_train = X.iloc[train_idx, :]
X_valid = X.iloc[valid_idx, :]
y_train = y.iloc[train_idx]
y_valid = y.iloc[valid_idx]
lgb_train_set = lgb.Dataset(X_train, label=y_train, categorical_feature=cat_features,free_raw_data=False)
 lgb_valid_set = lgb.Dataset(X_valid, label=y_valid,free_raw_data=False)
 lgbm=lgb.LGBMRegressor()
lgbm = lgb.train(parameters,
train_set=lgb_train_set,
valid_sets=[lgb_train_set,lgb_valid_set],
verbose_eval=100,
evals_result=evals_result)
 best_round=lgbm.best_iteration
y_train_pred = lgbm.predict(X_train,num_iteration=best_round)
 y_valid_pred = lgbm.predict(X_valid,num_iteration=best_round)
 y_test_pred = lgbm.predict(XT,num_iteration=best_round)
 qks1=qks(y_train_pred,y_train)
 qks2=qks(y_valid_pred,y_valid)
 df_qks.loc[k]=[best_round,qks1,qks2]
perf_list=perf_list+[evals_result]
df_y[k]=y_test_pred
train['comment_text'].fillna('unknown', inplace=True)
 test['comment_text'].fillna('unknown', inplace=True )<string_transform>
df_all[df_all['AdoptionSpeed'].astype(int)>=0]['AdoptionSpeed'].value_counts()
def tokenize(s):
pattern = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])')
 return pattern.sub(r' \1 ', s ).split()<feature_engineering>
sum(ym>3.5 )
vec = TfidfVectorizer(ngram_range=(1,2), tokenizer=tokenize,
min_df=3, max_df=0.9, strip_accents='unicode', use_idf=1,
smooth_idf=1, sublinear_tf=1)
 X_train = vec.fit_transform(train['comment_text'])
 X_test = vec.transform(test['comment_text'] )<prepare_x_and_y>
def distrib_err(coef,test_proba,train_label):
test_predictions = pd.cut(test_proba, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4])
 N_CLASS=5
freq_train=np.zeros(N_CLASS)
 freq_test=np.zeros(N_CLASS)
 delta_freq=np.zeros(N_CLASS)
 for i in range(0,N_CLASS):
freq_train[i]=100*Counter(train_label)[i]/len(train_label)
 freq_test[i]=100*Counter(test_predictions)[i]/len(test_predictions)
 delta_freq[i]=freq_test[i]-freq_train[i]
return np.sum(delta_freq**2 )
y_train = train[labels].values<choose_model_class>
initial_coef = [2.0, 2.5, 3.0, 3.5]
distrib_err_partial = partial(distrib_err, test_proba=ym, train_label=y)
 final_coef = sp.optimize.minimize(distrib_err_partial, initial_coef, method='nelder-mead')
 final_coef
chains = [ClassifierChain(NbSvmClassifier() , order='random', random_state=i)for i in range(len(labels)) ]<train_model>
def apply_lim(y_calc,limits):
y_round=np.zeros(len(y_calc))
for i,yc in enumerate(y_calc):
if(yc<=limits[0]):
y_round[i]=0
if(( yc>limits[0])&(yc<=limits[1])) :
y_round[i]=1
if(( yc>limits[1])&(yc<=limits[2])) :
y_round[i]=2
if(( yc>limits[2])&(yc<=limits[3])) :
y_round[i]=3
if(yc>limits[3]):
y_round[i]=4
return y_round
for chain in chains:
chain.fit(X_train, y_train )<predict_on_test>
y_test_pred_r = apply_lim(ym,final_coef['x'] )
y_pred_chains = np.array([chain.predict_proba(X_test)for chain in chains] )<feature_engineering>
y_pred = y_test_pred_r.astype('int' )
y_pred_ensemble = y_pred_chains.mean(axis=0 )<prepare_output>
subm=pd.DataFrame({'PetID': dftest_ids,'AdoptionSpeed': y_pred})
 subm.head()
sub[labels] = y_pred_ensemble<save_to_csv>
subm['AdoptionSpeed'].value_counts()
<load_from_csv><EOS> 
subm.to_csv('submission.csv', index=False )