Kaggle competition: PetFinder.my Adoption Prediction
<SOS> metric: QuadraticWeightedKappa Kaggle data source: petfinder.my-adoption-prediction<save_to_csv>
import pandas as pd
import numpy as np
import json
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
import statsmodels.api as sm
from sklearn import metrics
import hypertools as hyp
from imblearn.over_sampling import SMOTE
from xgboost import XGBClassifier, DMatrix, cv
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.ensemble import RandomForestClassifier
from sklearn.decomposition import PCA
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.model_selection import KFold
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RandomizedSearchCV
test_df['time'] = [ "{0:.4f}".format(v)for v in test_df['time'].to_array() ]
test_df[TARGET] = y_test.astype(np.int32)
 test_df.to_csv('submission.csv', index=False, columns=["time", TARGET] )<load_from_csv>
pets = pd.read_csv('.. /input/train/train.csv')
 pets2 = pd.read_csv('.. /input/test/test.csv' )
p1 = pd.read_csv('.. /input/top-ion-subs/submission1.csv' ).open_channels.values
p2 = pd.read_csv('.. /input/top-ion-subs/submission2.csv' ).open_channels.values
p3 = pd.read_csv('.. /input/top-ion-subs/submission3.csv' ).open_channels.values<save_to_csv>
petID = np.asarray(pets2.PetID )
sub = pd.read_csv('.. /input/liverpool-ion-switching/sample_submission.csv')
 sub.open_channels = np.median(np.stack([p1,p2,p3] ).T, axis=1 ).astype('int8')
 sub.to_csv('submission.csv', index=False, float_format='%.4f' )<count_values>
def add_pure(dataframe):
dataframe['pure_bred'] = 0
for i in range(0,(len(pets))):
try:
if dataframe.Breed2[i] == 0 or(dataframe.Breed1[i] == dataframe.Breed2[i]):
dataframe.iat[i, dataframe.columns.get_loc('pure_bred')] = 1
except:
continue
return(dataframe)
 def add_sentiments(dataframe, path):
dataframe['sentiment_score'] = float(0)
 dataframe['sentiment_magnitude'] = float(0)
 for i in range(0,(len(dataframe))):
filename = path + dataframe.PetID[i] + '.json'
try:
with open(filename, 'r')as f:
sentiments = json.load(f)
 dataframe.iat[i, dataframe.columns.get_loc('sentiment_score')] = sentiments['documentSentiment']['score']
dataframe.iat[i, dataframe.columns.get_loc('sentiment_magnitude')] = sentiments['documentSentiment']['magnitude']
except FileNotFoundError:
continue
return(dataframe)
 def create_cats(dataframe):
for i in dataframe.columns:
if i not in ['Age', 'AdoptionSpeed', 'FurLength', 'Quantity', 'Fee', 'VideoAmt', 'PhotoAmt', 'sentiment_score', 'sentiment_magnitude', 'desc_len', 'vertex_x', 'vertex_y','bounding_confidence', 'bounding_importance','dominant_blue', 'dominant_green', 'dominant_red', 'dominant_pixel_frac', 'dominant_score', 'label_score']:
dataframe[i] = dataframe[i].astype('category')
 def sentiment_to_float(dataframe):
dataframe.sentiment_score = dataframe.sentiment_score.astype(float)
 dataframe.sentiment_magnitude = dataframe.sentiment_magnitude.astype(float )
sub.open_channels.value_counts()<set_options>
def image_data(dataframe, ext):
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 dataframe.PetID:
try:
with open(ext + 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)
 dataframe.loc[:, 'vertex_x'] = vertex_xs
dataframe.loc[:, 'vertex_y'] = vertex_ys
dataframe.loc[:, 'bounding_confidence'] = bounding_confidences
dataframe.loc[:, 'bounding_importance'] = bounding_importance_fracs
dataframe.loc[:, 'dominant_blue'] = dominant_blues
dataframe.loc[:, 'dominant_green'] = dominant_greens
dataframe.loc[:, 'dominant_red'] = dominant_reds
dataframe.loc[:, 'dominant_pixel_frac'] = dominant_pixel_fracs
dataframe.loc[:, 'dominant_score'] = dominant_scores
dataframe.loc[:, 'label_description'] = label_descriptions
dataframe.loc[:, 'label_score'] = label_scores
%matplotlib inline 
<import_modules>
def fillna(dataframe):
for i in dataframe.columns:
if i not in ['Name', 'Description', 'PetID', 'RescuerID']:
if i not in ['Age', 'Quantity', 'PhotoAmt', 'VideoAmt', 'sentiment_score', 'sentiment_magnitude', 'Fee']:
dataframe[i].fillna(dataframe[i].mode() [0], inplace = True)
 else:
dataframe[i].fillna(dataframe[i].mean() , inplace = True )
from cuml.neighbors import KNeighborsClassifier, NearestNeighbors
import cuml; cuml.__version__<load_pretrained>
def add_length(dataframe):
dataframe['desc_len'] = 0
for i in range(0, len(dataframe)) :
dataframe.iat[i, dataframe.columns.get_loc('desc_len')] = len(str(dataframe.Description[i]))
def sqrt_age(dataframe):
dataframe.Age = np.sqrt(dataframe.Age )
%%time
NAME='KNN'
NMINUS=3
NPLUS=3
weights=[4, 1, 0.5, 0.25, 1, 0.5, 0.25]
NFOLDS=5
RS=42
PATH=Path('/kaggle/input/is-eda-sine-50hz-exp')
 print(f'Loading data for model {NAME}
')
 train=feather.read_dataframe(PATH/'train.feather')
 test=feather.read_dataframe(PATH/'test.feather')
 with open(PATH/'folds_train.pickle', 'rb')as infile:
folds_train = pickle.load(infile)
 with open(PATH/'folds_val.pickle', 'rb')as infile:
folds_val = pickle.load(infile )<define_variables>
def find_weights(dataframe):
weights = []
count = 0
for i in range(0,5):
count = 0
weight = 0.0
for j in dataframe.AdoptionSpeed:
if j == i:
count += 1
weight = count/len(dataframe.AdoptionSpeed)
 weights.append(weight)
 return weights
cols=['signal_no_drift']
target=['open_channels']
for shift in range(1, NMINUS+1):
feature='signal_shift_-' + str(shift)
 cols.append(feature)
 for shift in range(1, NPLUS+1):
feature='signal_shift_+' + str(shift)
 cols.append(feature)
 print(f"The list of features included in the {NAME} model:
")
 print(cols )<drop_column>
add_pure(pets)
 add_pure(pets2)
 add_sentiments(pets, '.. /input/train_sentiment/')
 add_sentiments(pets2, '.. /input/test_sentiment/')
 image_data(pets, '.. /input/train_metadata/')
 image_data(pets2, '.. /input/test_metadata/')
train=train[cols+target+['model', 'batch', 'time']]
test=test[cols+['model', 'batch', 'segment', 'time']]<create_dataframe>
create_cats(pets)
 create_cats(pets2)
 sentiment_to_float(pets)
 sentiment_to_float(pets2 )
classes=np.array(['class_'+str(i)for i in range(11)])
 oof=pd.DataFrame(data=np.zeros(( len(train), 11)) , index=train.index, columns=classes)
 oof_preds=np.zeros(len(train))
preds_proba=pd.DataFrame(data=np.zeros(( len(test), 11)) , index=test.index, columns=classes)
 f1_folds=[]<feature_engineering>
add_length(pets)
 add_length(pets2)
 pets = pets.drop(['Name', 'PetID', 'Description','RescuerID'], axis = 1)
 pets2 = pets2.drop(['Name', 'PetID', 'Description','RescuerID'], axis = 1)
 pets = pd.get_dummies(pets)
 pets2 = pd.get_dummies(pets2)
for c, w in zip(cols, weights):
train[c]=w*train[c]
test[c]=w*test[c]<prepare_x_and_y>
def mat_interactions(dataframe):
dataframe['mat_int_1_1'] = dataframe.MaturitySize_1 * dataframe.Type_1
dataframe['mat_int_1_2'] = dataframe.MaturitySize_2 * dataframe.Type_1
dataframe['mat_int_1_3'] = dataframe.MaturitySize_3 * dataframe.Type_1
dataframe['mat_int_1_4'] = dataframe.MaturitySize_4 * dataframe.Type_1
dataframe['mat_int_2_1'] = dataframe.MaturitySize_1 * dataframe.Type_2
dataframe['mat_int_2_2'] = dataframe.MaturitySize_2 * dataframe.Type_2
dataframe['mat_int_2_3'] = dataframe.MaturitySize_3 * dataframe.Type_2
dataframe['mat_int_2_4'] = dataframe.MaturitySize_4 * dataframe.Type_2
%%time
KNN=100
batch = 1024
for fold_num in range(1, NFOLDS+1):
print('-'*50)
 print(f'Fold {fold_num}:')
 train_index=folds_train[fold_num]
val_index=folds_val[fold_num]
X_train, Y_train = train.iloc[train_index, :], train.loc[train_index, target]
X_val, Y_val = train.iloc[val_index, :], train.loc[val_index, target]
for m in models:
mask_model_train=(X_train['model']==m)
 mask_model_val=(X_val['model']==m)
 mask_model_test=(test['model']==m)
 X_mod=X_train.loc[mask_model_train, cols].values
Y_mod=Y_train[mask_model_train].values.reshape(-1,)
 X_val_mod=X_val.loc[mask_model_val, cols].values
Y_val_mod=Y_val[mask_model_val].values.reshape(-1,)
 X_test=test.loc[mask_model_test, cols].copy()
if m=='M4':
shift=1
else:
shift=0
clf = NearestNeighbors(n_neighbors=KNN)
 clf.fit(X_mod)
 distances, indices = clf.kneighbors(X_val_mod)
 ct = indices.shape[0]
pred = np.zeros(( ct,KNN),dtype=np.int8)
 Y_val_pred = np.zeros(( ct,len(np.unique(Y_mod))),dtype=np.float32)
 it = ct//batch + int(ct%batch!=0)
 for k in range(it):
a = batch*k; b = batch*(k+1); b = min(ct,b)
 pred[a:b,:] = Y_mod[ indices[a:b].astype(int)]
for j in np.unique(Y_mod):
Y_val_pred[a:b,j-shift] = np.sum(pred[a:b,]==j,axis=1)/KNN
distances, indices = clf.kneighbors(X_test.values)
 ct = indices.shape[0]
pred = np.zeros(( ct,KNN),dtype=np.int8)
 Y_test_pred = np.zeros(( ct,len(np.unique(Y_mod))),dtype=np.float32)
 it = ct//batch + int(ct%batch!=0)
 for k in range(it):
a = batch*k; b = batch*(k+1); b = min(ct,b)
 pred[a:b,:] = Y_mod[ indices[a:b].astype(int)]
for j in np.unique(Y_mod):
Y_test_pred[a:b,j-shift] = np.sum(pred[a:b,]==j,axis=1)/KNN
classes_mod=classes[np.unique(Y_mod)]
oof.loc[val_index[mask_model_val], classes_mod]=Y_val_pred
preds_proba.loc[mask_model_test, classes_mod]+=Y_test_pred
Y_val_pred=np.argmax(Y_val_pred, axis=1 ).astype(int ).reshape(-1,)+ int(shift)
 f1_model=f1_score(Y_val_mod, Y_val_pred, average='macro')
 print(f'Model {m}: done! Macro F1 score = {f1_model:.5f}')
 oof_preds[val_index]=np.argmax(oof.iloc[val_index, :].values, axis=1 ).astype(int ).reshape(-1,)
 Y_val_OC=train.loc[val_index, 'open_channels'].values.astype(np.uint8 ).reshape(-1,)
 f1_fold=f1_score(Y_val_OC, oof_preds[val_index], average='macro')
 f1_folds.append(f1_fold)
 print(f'
Fold {fold_num} is done! Macro F1 score = {f1_fold:.5f}')
 preds_proba/=NFOLDS
preds=np.argmax(preds_proba.values, axis=1 ).astype(int ).reshape(-1,)
 print('-'*50)
 print('Summary:')
 for m in models:
print(f"
Model {m}:")
 mask_model=train['model']==m
f1_model=f1_score(train.loc[mask_model, 'open_channels'].values.reshape(-1,),
oof_preds[mask_model], average='macro')
 print(classification_report(train.loc[mask_model, 'open_channels'].values.reshape(-1,),
oof_preds[mask_model], digits=5))
print(f'Macro F1 score for model {m} = {f1_model:.5f}')
 f1_av=np.array(f1_folds ).mean()
f1_std=np.std(f1_folds)
 print(f'Macro F1 score = {f1_av:.5f}(average across the folds); std = {f1_std:.5f}')
 f1=f1_score(train['open_channels'].values.reshape(-1,), oof_preds, average='macro')
 print(f'Macro F1 score = {f1:.5f}(out-of-folds)' )<feature_engineering>
def age_interactions(dataframe):
dataframe['age_int_1_1'] = dataframe.Age * dataframe.Health_1
dataframe['age_int_1_2'] = dataframe.Age * dataframe.Health_2
dataframe['age_int_1_3'] = dataframe.Age * dataframe.Health_3
for c, w in zip(cols, weights):
train[c]=train[c]/w
test[c]=test[c]/w<compute_train_metric>
pets_vars = pets.columns.tolist()
adopt = ['AdoptionSpeed']
X=[i for i in pets_vars if i not in adopt]
%%time
print(classification_report(train['open_channels'].values.reshape(-1,), oof_preds, digits=5))<load_from_csv>
k=find_weights(pets)
 balance_weight = {0 :int(( max(k)*len(pets)) -(min(k)*len(pets)))}
%%time
PATH=Path('/kaggle/input/is-gmm-cv5-b7-repl-seq-folds/sub_GMM_110.csv')
 preds_GMM=pd.read_csv(PATH)
 mask_M1_GMM=(test['model']=='M1')&(preds_GMM['open_channels']>1)
 preds[mask_M1_GMM]=preds_GMM.loc[mask_M1_GMM, 'open_channels']<load_from_csv>
train_y = pets[adopt]
smote = SMOTE(sampling_strategy = 'not majority', k_neighbors = 100)
 new_XX, train_y = smote.fit_resample(pets[X], train_y.values.ravel())
PATH=Path('/kaggle/input/liverpool-ion-switching/')
 sub=pd.read_csv(PATH/'sample_submission.csv' )<count_values>
model = ExtraTreesClassifier()
model.fit(new_XX, train_y)
 feature_list = {}
for i in range(0, len(model.feature_importances_)) :
feature_list.update({i:model.feature_importances_[i]})
 feature_list = sorted(feature_list.items() , key=lambda kv: kv[1], reverse = True )
sub['open_channels']=preds
sub['open_channels'].value_counts().sort_index()<define_variables>
j = feature_list[0:50]
new_X = []
for i in j:
new_X.append(X[i[0]])
 new_X
time_zone = pytz.timezone('America/Chicago')
 current_datetime = datetime.now(time_zone)
 ts=current_datetime.strftime("%m%d%H%M%S")
 sub_file_name='sub_'+NAME+'_'+ts+'.csv'
oof_file_name='oof_'+NAME+'.feather'
preds_file_name='preds_'+NAME+'.feather'
ts, sub_file_name, oof_file_name, preds_file_name<save_to_csv>
train_X = pd.DataFrame(data = new_XX, columns=X)
 train_x = train_X[new_X]
test_x = pets2[new_X]
%%time
oof.to_feather(oof_file_name)
 preds_proba.to_feather(preds_file_name)
 sub.to_csv(sub_file_name, index=False, float_format='%.4f' )<define_variables>
rf_model = RandomForestClassifier(class_weight = 'balanced', n_estimators = 1000, random_state = 42)
 rf_model.fit(train_x, train_y )
low={'M1':-4, 'M2':-4, 'M3':-5, 'M4':-5, 'M5':-5}
high={'M1':2, 'M2':0, 'M3':6, 'M4':10, 'M5':5}<compute_test_metric>
parameters = {'learning_rate':0.15, 'max_depth':6,'subsample':0.5,'objective':'multi:softmax', 'num_class':5}
model = XGBClassifier(**parameters )
y_true=train['open_channels'].values
mask=np.equal(y_true, oof_preds)
 for m in models:
show_results(y_true[~mask], mod=m, lag=-1, df=train[~mask], s=0.7, mk_scale=5 )<feature_engineering>
np.unique(train_y, return_counts = True )
high['M1']=4<define_variables>
model.fit(train_x,train_y, eval_metric = 'merror', verbose = True)
 pred_xg = model.predict(test_x)
 pred_xg = np.round(pred_xg)
 pred_xg = pred_xg.astype(int )
batches_order=np.array([0, 1, 2, 6, 3, 7, 4, 9, 5, 8] )<data_type_conversions>
df = {'PetID': petID, 'AdoptionSpeed': pred_xg}
df = pd.DataFrame(df)
 df.AdoptionSpeed = df.AdoptionSpeed.astype('int32' )
%%time
train['open_channels']=oof_preds
train['open_channels']=train['open_channels'].astype(np.uint8)
 signal_scatter_plots(train, col='signal_no_drift' )<data_type_conversions>
df.AdoptionSpeed.value_counts(dropna= False )
<set_options><EOS> 
df.to_csv('submission.csv', index = False )