Kaggle competition: Shelter Animal Outcomes
<SOS> metric: MulticlassLoss Kaggle data source: shelter-animal-outcomes<choose_model_class>
%matplotlib inline
interp = ClassificationInterpretation.from_learner(learn )<train_model>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 test_ID = test_df['ID']
train_OutcomeType = train_df['OutcomeType']
train_df = train_df.drop(['AnimalID','OutcomeSubtype','OutcomeType'] , axis=1)
 test_df = test_df.drop('ID', axis=1)
 combine_df = pd.concat([train_df, test_df], axis=0 )
learn.fit_one_cycle(1 )<find_best_params>
combine_df['HasName']=combine_df['Name'].notnull().astype(int )
learn.lr_find()<train_model>
interval = pd.to_datetime(combine_df['DateTime'])-pd.to_datetime('2013-01-01')
 combine_df['DateTime_day'] = interval /np.timedelta64(1, 'D')
 interval =(( interval/np.timedelta64(1, 'D')) %1*24)
 interval[(interval>=0)&(interval)<6] = 0
interval[(interval>=6)&(interval<12)] = 1
interval[(interval>=12)&(interval<18)] = 2
interval[(interval>=18)&(interval<22)] = 3
interval[(interval>=22)&(interval<24)] =0
combine_df['DateTime_mmae'] = interval
learn.fit_one_cycle(4, max_lr=slice(1e-05))<predict_on_test>
combine_df['AnimalType'] = combine_df['AnimalType'].map({'Dog':0, 'Cat':1} )
preds,_ = learn.get_preds(ds_type=DatasetType.Test )<filter>
combine_df['SexuponOutcome'] = combine_df['SexuponOutcome'].fillna(combine_df['SexuponOutcome'].mode() [0])
 combine_df['Sex'] = combine_df['SexuponOutcome'].map({'Intact Female':0,'Spayed Female':0,'Intact Male':1,'Neutered Male':1,'Unknown':2})
 combine_df['IsIntact'] = combine_df['SexuponOutcome'].map({'Intact Female':0,'Intact Male':0, 'Neutered Male':1,'Spayed Female':1,'Unknown':2})
 combine_df[['SexuponOutcome','Sex','IsIntact']].head()
test_df.has_cactus = preds.numpy() [:, 0]<save_to_csv>
combine_df['AgeuponOutcome'] = combine_df['AgeuponOutcome'].fillna(combine_df['AgeuponOutcome'].mode() [0])
 def convert(x):
a = str(x ).split(' ')[0]
b = str(x ).split(' ')[-1]
if 'year' in str(b):
return int(a)*12*52*7
elif 'month' in str(b):
return int(a)*4*7
elif 'week' in str(b):
return int(a)*7
else:
return int(a)
 combine_df['AgeuponOutcome'] = combine_df['AgeuponOutcome'].map(convert )
test_df.to_csv('submission.csv', index=False )<set_options>
combine_df['IsMix'] = combine_df['Breed'].map(lambda x: 'Mix' in x ).astype(int)
 combine_df['Breed'] = combine_df['Breed'].map(lambda x:x.strip(' Mix' ).split('/')[0].split(' ')[-1] )
warnings.filterwarnings('ignore')
<define_variables>
combine_df['IsPure'] = combine_df['Color'].map(lambda x: '/' not in x ).astype(int)
 combine_df[['Color','IsPure']].head()
combine_df['Color'] = combine_df['Color'].map(lambda x: x.split('/')[0].split(' ')[0] )
MAIN_DIR = '.. /input/'
TRAIN_DIR = MAIN_DIR + "train/train/"
TEST_DIR = MAIN_DIR + "test/test/"
IMG_ROWS = 32
IMG_COLS = 32
CHANNELS = 3
IMG_SHAPE =(IMG_ROWS, IMG_COLS, CHANNELS)
 sns.set(font_scale=1.3 )<load_from_csv>
combine_df_DateTime_mmae = pd.get_dummies(combine_df['DateTime_mmae'], prefix='DateTime_mmae')
 combine_df_breed = pd.get_dummies(combine_df['Breed'], prefix='Breed')
 combine_df_color = pd.get_dummies(combine_df['Color'], prefix='Color')
 combine_df['DateTime_day'] =(combine_df['DateTime_day'] - combine_df['DateTime_day'].min())/(combine_df['DateTime_day'].max() - combine_df['DateTime_day'].min())
 combine_df['AgeuponOutcome'] =(combine_df['AgeuponOutcome'] - combine_df['AgeuponOutcome'].min())/(combine_df['AgeuponOutcome'].max() - combine_df['AgeuponOutcome'].min() )
def plot_pictures(nb_rows=6, nb_cols=6, figsize=(14, 14)) :
fig, ax = plt.subplots(nb_rows, nb_cols, figsize=figsize, gridspec_kw=None)
 fig.subplots_adjust(wspace=0.4, hspace=0.4)
 for i in range(0, nb_rows):
for j in range(0, nb_cols):
data = pd.read_csv(MAIN_DIR + "train.csv")
 index = np.random.randint(0, data.shape[0])
 file = data.loc[index]['id']
img_ = cv2.imread(TRAIN_DIR + file)
 ax[i, j].grid(False)
 ax[i, j].axis('off')
 ax[i, j].imshow(img_)
 ax[i, j].set_title("Label : " + str(data.loc[index]['has_cactus']))<load_from_csv>
X = combine_df[['HasName','DateTime_day', 'AnimalType', 'AgeuponOutcome','Sex','IsIntact','IsMix','IsPure']]
X = pd.concat([X, combine_df_breed, combine_df_color, combine_df_DateTime_mmae], axis=1)
 train_x = X.iloc[0:train_df.shape[0],]
test_x = X.iloc[train_df.shape[0]:,]
train_y = train_OutcomeType.map({'Adoption':0,'Died':1,'Euthanasia':2,'Return_to_owner':3,'Transfer':4} )
df_train = pd.read_csv(MAIN_DIR + "train.csv" )<count_values>
tr_x, va_x, tr_y, va_y = train_test_split(train_x, train_y,test_size=0.3,random_state=0 )
df_train['has_cactus'].value_counts()<load_from_csv>
from sklearn.metrics import accuracy_score, log_loss
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import GaussianNB, MultinomialNB, BernoulliNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
def load_data(dataframe=None, batch_size=16, mode='categorical'):
gen = ImageDataGenerator(
 zoom_range=0.1,
validation_split=0.1,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
 df = pd.read_csv(MAIN_DIR + 'train.csv')
 df['has_cactus'] = df['has_cactus'].apply(str)
 data_train = gen.flow_from_dataframe(df,
directory=TRAIN_DIR,
x_col='id',
y_col='has_cactus',
has_ext=True,
target_size=(32, 32),
class_mode=mode,
batch_size=batch_size,
shuffle=True,
subset='training')
 data_test = gen.flow_from_dataframe(df,
directory=TRAIN_DIR,
x_col='id',
y_col='has_cactus',
has_ext=True,
target_size=(32, 32),
class_mode=mode,
batch_size=batch_size,
shuffle=True,
subset='validation')
 return data_train, data_test<choose_model_class>
clf1 = KNeighborsClassifier(3)
 clf1.fit(tr_x, tr_y)
 name = clf1.__class__.__name__
print(name),
va_pred = clf1.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf1.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
def create_model() :
model = Sequential()
model.add(Conv2D(16,(3, 3), input_shape=(32, 32, 3), padding='same', use_bias=False, name="first_conv"))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, name="second_conv"))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Conv2D(256,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(256,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Conv2D(512,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(512,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-4), metrics=['accuracy'])
 return model<train_model>
clf2 = SVC(kernel="rbf", C=0.025, probability=True)
 clf2.fit(tr_x, tr_y)
 name = clf2.__class__.__name__
print(name),
va_pred = clf2.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf2.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
def train(train_generator, val_generator):
model = create_model()
cbs = [ReduceLROnPlateau(monitor='loss', factor=0.5, patience=1, min_lr=1e-7, verbose=0),
EarlyStopping(monitor='val_loss', min_delta=0.0001,
patience=10, verbose=1, mode='min', restore_best_weights=True)]
history = model.fit_generator(train_generator,
steps_per_epoch=(train_generator.n//train_generator.batch_size),
epochs=100,
validation_data=val_generator,
validation_steps=len(val_generator),
shuffle=True,
callbacks=cbs,
verbose=1)
 return model, history<train_model>
clf3 = DecisionTreeClassifier()
clf3.fit(tr_x, tr_y)
 name = clf3.__class__.__name__
print(name),
va_pred = clf3.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf3.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
train_generator, val_generator = load_data(batch_size=32)
 model, history = train(train_generator, val_generator )<predict_on_test>
clf4 = RandomForestClassifier()
clf4.fit(tr_x, tr_y)
 name = clf4.__class__.__name__
print(name),
va_pred = clf4.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf4.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
def predict(model, sample_submission):
pred = np.empty(( sample_submission.shape[0],))
for n in tqdm(range(sample_submission.shape[0])) :
data = np.array(Image.open(TEST_DIR + sample_submission.id[n]))
pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)))[0][1]
sample_submission['has_cactus'] = pred
return sample_submission<save_to_csv>
clf5 = AdaBoostClassifier()
clf5.fit(tr_x, tr_y)
 name = clf5.__class__.__name__
print(name),
va_pred = clf5.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf5.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
sample_submission = pd.read_csv(MAIN_DIR + 'sample_submission.csv')
 df_prediction = predict(model, sample_submission)
 df_prediction.to_csv('submission.csv', index=False )<define_variables>
clf6 = GradientBoostingClassifier()
clf6.fit(tr_x, tr_y)
 name = clf6.__class__.__name__
print(name),
va_pred = clf6.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf6.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
path = Path('.. /input')
 tfms = get_transforms(do_flip=True , flip_vert= True , max_rotate=10.0 , max_zoom=1.1 ,
max_lighting =0.2 , max_warp = 0.2 , p_affine = 0.75 , p_lighting = 0.75)
<load_from_csv>
clf7 = GaussianNB()
clf7.fit(tr_x, tr_y)
 name = clf7.__class__.__name__
print(name),
va_pred = clf7.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf7.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/sample_submission.csv' )<concatenate>
clf8 = MultinomialNB()
clf8.fit(tr_x, tr_y)
 name = clf8.__class__.__name__
print(name),
va_pred = clf8.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf8.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
no_cactus = train.query("has_cactus == 0")
 train = train.append([no_cactus]*2, ignore_index=True )<choose_model_class>
clf9 = BernoulliNB()
clf9.fit(tr_x, tr_y)
 name = clf9.__class__.__name__
print(name),
va_pred = clf9.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf9.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
learn = cnn_learner(data , models.densenet161 , metrics = error_rate )<train_model>
clf10 = LinearDiscriminantAnalysis()
clf10.fit(tr_x, tr_y)
 name = clf10.__class__.__name__
print(name),
va_pred = clf10.predict(va_x)
 acc = accuracy_score(va_y, va_pred)
 print("Accuracy: {:.4%}".format(acc)) ,
va_pred = clf10.predict_proba(va_x)
 ll = log_loss(va_y, va_pred)
 print("Log Loss: {:.4f}".format(ll))
learn.fit_one_cycle(10, max_lr=slice(1e-6))<create_dataframe>
model = GradientBoostingClassifier()
model.fit(train_x, train_y)
 test_prediction = model.predict_proba(test_x )
test_data = ImageList.from_df(test, path=path/'test', folder='test')
 data.add_test(test_data )<predict_on_test>
LABELS = sorted(train_OutcomeType.unique())
 PredictResult = pd.DataFrame(test_prediction,columns=LABELS)
 PredictResult.columns.names = ['ID']
PredictResult.index.names = ['ID']
PredictResult.index += 1
PredictResult.to_csv('GBC_pred_1.csv', index_label='ID' )