Kaggle competition: Shelter Animal Outcomes
<SOS> metric: MulticlassLoss Kaggle data source: shelter-animal-outcomes<load_from_csv>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
sample_submission = pd.read_csv("/kaggle/input/shelter-animal-outcomes/sample_submission.csv.gz")
 train = pd.read_csv("/kaggle/input/shelter-animal-outcomes/train.csv.gz")
 test = pd.read_csv("/kaggle/input/shelter-animal-outcomes/test.csv.gz")
 print(train.describe())
 print(train.head())
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )<define_variables>
def binarize_name(df):
df = df.rename(columns={'Name': 'HasName'})
 df['HasName'] = pd.to_numeric(df['HasName'].str.isalpha())
 df['HasName'] = df['HasName'].fillna(0)
 return df
train = binarize_name(train)
 test = binarize_name(test)
 train['SexuponOutcome'] = train['SexuponOutcome'].fillna('Unknown')
 test['SexuponOutcome'] = test['SexuponOutcome'].fillna('Unknown')
 train = train.fillna({'AgeuponOutcome': '9999 days'})
 test = test.fillna({'AgeuponOutcome': '9999 days'})
 def string_to_num_days(series):
split_series = series.str.split(" ")
 split_df = pd.DataFrame(split_series.to_list() , columns=['multiplier', 'days'])
 convert_days = {'year': 365, 'years': 365, 'months': 30.5, 'month': 30.5, 'week': 7, 'weeks': 7, 'days': 1, 'day':1}
split_df['days'] = split_df['days'].map(convert_days)
 split_df['multiplier'] = split_df['multiplier'].astype(int)
 split_df['total_days'] = split_df['days'] * split_df['multiplier']
return split_df['total_days']
train['AgeuponOutcome'] = string_to_num_days(train['AgeuponOutcome'])
 test['AgeuponOutcome'] = string_to_num_days(test['AgeuponOutcome'])
 train = train.replace({'AgeuponOutcome': {9999: np.nan}})
 train['AgeuponOutcome'] = train['AgeuponOutcome'].fillna(train['AgeuponOutcome'].mean())
 test = test.replace({'AgeuponOutcome': {9999: np.nan}})
 test['AgeuponOutcome'] = test['AgeuponOutcome'].fillna(test['AgeuponOutcome'].mean())
train_img.show_batch(rows=3, figsize=(7,6))<find_best_params>
print('Distinct Colors: ', len(train['Color'].unique()))
print('Distinct Breeds: ', len(train['Breed'].unique()))
print(train_img.classes)
 print("No.of classes : {}".format(train_img.c))<choose_model_class>
def extract_color(df):
df['IsSolidColor'] = 1
mixed_entries = df['Color'].str.contains(r'\/')
 df.loc[mixed_entries, 'IsSolidColor'] = 0
df['Color'] = df['Color'].str.replace('\/.*', '')
 return df
train = extract_color(train)
 test = extract_color(test)
 def extract_markings(df):
for marking in ['Tabby','Point','Tiger','Brindle','Smoke','Tick','Merle']:
df[marking] = df['Color'].str.contains(marking ).astype(int)
 return df
train = extract_markings(train)
 test = extract_markings(test)
 print(train['IsSolidColor'].value_counts())
 for marking in ['Tabby','Point','Tiger','Brindle','Smoke','Tick','Merle']:
print(train[marking].value_counts() )
learner = cnn_learner(train_img, models.densenet161, metrics=[accuracy, error_rate] )<train_model>
train = train.drop(columns=['Tabby','Point','Tiger','Brindle','Smoke','Tick','Merle'] )
learner.fit_one_cycle(5, max_lr=slice(3e-02))<save_model>
def extract_purebred(df):
df['Purebred'] = 1
mixed_entries =(df['Breed'].str.contains('\/')) |(df['Breed'].str.contains('Mix'))
df.loc[mixed_entries, 'Purebred'] = 0
return df
train = extract_purebred(train)
 test = extract_purebred(test)
 def extract_aggression(df):
aggro_breeds = 'Staffordshire|Pit|Doberman|Chow|Rottweiler|German Shepherd|American Bulldog|Mastiff|Bullmastiff|Husky|Malamute|Akita|Boxer'
df['AggroBreed'] = 0
df.loc[df['Breed'].str.contains(aggro_breeds), 'AggroBreed'] = 1
return df
train = extract_aggression(train)
 test = extract_aggression(test)
 def extract_hypoallergenic(df):
hypoallergenic_breeds = 'Affenpinscher|Afghan|Hairless|Barbet|Bedlington|Bichon|Bolognese|Crested|Schnauzer|Water Spaniel|Kerry|Maltese|Poodle|Portuguese Water|Yorkshire'
df['HypoallergenicBreed'] = 0
df.loc[df['Breed'].str.contains(hypoallergenic_breeds), 'HypoallergenicBreed'] = 1
return df
train = extract_hypoallergenic(train)
 test = extract_hypoallergenic(test )
learner.save('stage-1' )<choose_model_class>
train['DateTime'] = pd.to_datetime(train['DateTime'], infer_datetime_format=True)
 test['DateTime'] = pd.to_datetime(test['DateTime'], infer_datetime_format=True)
 train['Hour'] = train['DateTime'].dt.round("H" ).dt.hour
test['Hour'] = test['DateTime'].dt.round("H" ).dt.hour
train['HourSin'] = np.sin(2 * np.pi * train['Hour']/23.0)
 train['HourCos'] = np.cos(2 * np.pi * train['Hour']/23.0)
 test['HourSin'] = np.sin(2 * np.pi * test['Hour']/23.0)
 test['HourCos'] = np.cos(2 * np.pi * test['Hour']/23.0)
 train['DayOfWeek'] = pd.to_datetime(train['DateTime'], infer_datetime_format=True ).dt.weekday.map({0:'Monday', 1:'Tuesday', 2:'Wednesday', 3:'Thursday', 4:'Friday',5:'Saturday', 6:'Sunday'})
 test['DayOfWeek'] = pd.to_datetime(test['DateTime'], infer_datetime_format=True ).dt.weekday.map({0:'Monday', 1:'Tuesday', 2:'Wednesday', 3:'Thursday', 4:'Friday',5:'Saturday', 6:'Sunday'})
interpreter = ClassificationInterpretation.from_learner(learner )<save_to_csv>
train['Dog'] = train['AnimalType'].str.contains('Dog' ).astype(int)
 test['Dog'] = test['AnimalType'].str.contains('Dog' ).astype(int)
 def extract_sex_info(df):
df['SexuponOutcome'].dropna(inplace=True)
 df['Male'] = df['SexuponOutcome'].str.contains("Male" ).astype(int)
 df['Intact'] = df['SexuponOutcome'].str.contains("Intact" ).astype(int)
 df['SexUnknown'] = df['SexuponOutcome'].str.contains("Unknown" ).astype(int)
 return df
train = extract_sex_info(train)
 test = extract_sex_info(test)
preds,_ = learner.get_preds(ds_type=DatasetType.Test)
 test_df.has_cactus = preds.numpy() [:, 0]
test_df.to_csv("submission.csv", index=False )<import_modules>
print(train[train['Hour'] == 0]['DateTime'].dt.strftime("%Y-%m-%d %H:%M:%S"))
print(train[train['Hour'] == 9]['DateTime'].dt.strftime("%Y-%m-%d %H:%M:%S"))
from fastai import *
from fastai.vision import *
import pandas as pd
import numpy as np<load_from_csv>
print('Outcomes for 0:00:00:
', train[(train['OnTheHour'] == 1)&(train['Hour'] == 0)]['OutcomeType'].value_counts() , '
')
 print('Outcomes for 9:00:00:
', train[(train['OnTheHour'] == 1)&(train['Hour'] == 9)]['OutcomeType'].value_counts() )
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )<create_dataframe>
def is_weekend(df):
df['weekend'] = df['DateTime'].dt.weekday.isin([5, 6] ).astype(int)
 df = df.drop(columns=['DayOfWeek'])
 return df
train = is_weekend(train)
 test = is_weekend(test )
test_img = ImageList.from_df(test_df,
path=data_folder/'test',
folder='test' 
)
 train_img =(
 ImageList.from_df(train_df,
path=data_folder/'train',
folder='train')
.split_by_rand_pct(0.2)
.label_from_df() 
.add_test(test_img)
.transform(get_transforms(flip_vert=True), size=128)
.databunch(path='.', bs=64)
.normalize(imagenet_stats)
 )<choose_model_class>
y_train = train['OutcomeType']
X_train = train[['HasName','AgeuponOutcome', 'AggroBreed','HourSin', 'HourCos', 'OnTheHour', 'Dog', 'Male', 'Intact', 'SexUnknown', 'weekend']]
X_test = test[['HasName','AgeuponOutcome', 'AggroBreed','HourSin', 'HourCos', 'OnTheHour', 'Dog', 'Male', 'Intact', 'SexUnknown', 'weekend']]
learn = cnn_learner(train_img,
models.resnet34,
metrics=[error_rate, accuracy]
)<train_model>
xgb_model = XGBClassifier(random_state = 42, n_estimators = 45)
 xgb_model.fit(X_train, y_train )
<save_model><EOS> 
xgb_pred = xgb_model.predict_proba(X_test)
 results = pd.DataFrame(xgb_pred, columns = xgb_model.classes_, index = test['ID'] ).reset_index()
results.to_csv('submission.csv', index=False )