Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
%matplotlib inline
warnings.filterwarnings('ignore' )
df_all['Description_isna'] = df_all['Description'].isna().astype(int)
 df_all['len_words_Description'] = df_all.Description.apply(lambda x: len(str(x ).split()))
n = 6
df_all['len_words_Description_bins']= pd.cut(np.log1p(df_all.len_words_Description),n,labels=['Description_{}'.format(e)for e in range(n)])
 le = LabelEncoder()
df_all['len_words_Description_bins'] = le.fit_transform(df_all['len_words_Description_bins'])
 df_all['len_isupper_Description'] = df_all.Description.apply(lambda x: len([c for c in str(x)if c.isupper() ]))
df_all['len_islower_Description'] = df_all.Description.apply(lambda x: len([c for c in str(x)if c.islower() ]))
count = lambda l1, l2: len(list(filter(lambda c: c in l2, l1)))
 df_all['len_punctuation_Description'] = df_all.Description.apply(lambda x: count(str(x), string.punctuation))
df_all['len_letters_Description'] = df_all.Description.apply(lambda x: count(str(x), string.ascii_letters))
df_all['len_characters_Description'] = df_all.Description.apply(lambda x: len(str(x)))
 df_all['len_isupper_Description_to_words'] = df_all['len_isupper_Description'] / df_all['len_words_Description']
df_all['len_islower_Description_to_words'] = df_all['len_islower_Description'] / df_all['len_words_Description']
df_all['len_punctuation_Description_to_words'] = df_all['len_punctuation_Description'] / df_all['len_words_Description']
df_all['len_letters_Description_to_words'] = df_all['len_letters_Description'] / df_all['len_words_Description']
df_all['len_characters_Description_to_words'] = df_all['len_characters_Description'] / df_all['len_words_Description']
df_all['len_isupper_Description_to_characters'] = df_all['len_isupper_Description'] / df_all['len_characters_Description']
df_all['len_islower_Description_to_characters'] = df_all['len_islower_Description'] / df_all['len_characters_Description']
df_all['len_punctuation_Description_to_characters'] = df_all['len_punctuation_Description'] / df_all['len_characters_Description']
df_all['len_letters_Description_to_characters'] = df_all['len_letters_Description'] / df_all['len_characters_Description']<define_variables>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 full_data = [train, test]
print(train.columns.values )
description_columns = [
'Description_isna',
'len_words_Description',
'len_words_Description_bins',
'len_isupper_Description',
'len_islower_Description',
'len_punctuation_Description',
'len_letters_Description',
'len_characters_Description',
'len_isupper_Description_to_words',
'len_islower_Description_to_words',
'len_punctuation_Description_to_words',
'len_letters_Description_to_words',
'len_isupper_Description_to_characters',
'len_islower_Description_to_characters',
'len_punctuation_Description_to_characters',
'len_letters_Description_to_characters',
'len_characters_Description_to_words',
]<concatenate>
print(pd.isnull(train ).sum() )
all_columns.extend(description_columns )<create_dataframe>
print(pd.isnull(test ).sum() )
nlp = spacy.load('.. /input/spacyen-vectors-web-lg/spacy-en_vectors_web_lg/en_vectors_web_lg')
 desc_embd_cols = ['Describe_spacy_{}'.format(e)for e in range(300)]
desc_embd = pd.DataFrame(np.vstack(df_all[['Description']].dropna().apply(lambda x: nlp(x['Description'] ).vector,axis=1 ).values),
index=df_all[['Description']].dropna().index)
 desc_embd.columns= desc_embd_cols 
<set_options>
train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
del nlp; gc.collect()<create_dataframe>
train[["Sex", "Survived"]].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
Describe_SVD = True
if Describe_SVD:
n_components = 32
svd_ = TruncatedSVD(n_components=n_components, random_state=1881)
 svd_col = svd_.fit_transform(desc_embd)
 svd_col_df = pd.DataFrame(svd_col,index=df_all[['Description']].dropna().index)
 svd_col_df = svd_col_df.add_prefix('Describe_spacy_SVD')
 df_all = df_all.join(svd_col_df)
 all_columns.extend(svd_col_df.columns)
 else:
df_all = df_all.join(desc_embd)
 all_columns.extend(desc_embd_cols )<set_options>
train[["SibSp", "Survived"]].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
del svd_col_df
del desc_embd; gc.collect()<feature_engineering>
train[["Parch", "Survived"]].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
images_df = df_all[['PetID', 'is_train', 'is_dog']].copy()
images_df['img_path'] = images_df.apply(lambda x: f".. /input/petfinder-adoption-prediction/{'train' if x['is_train'] else 'test'}_images/{x['PetID']}-1.jpg", axis=1)
 images_df = images_df[images_df.img_path.map(os.path.exists)].reset_index(drop=True )<train_model>
train = train.drop(['Ticket', 'Cabin'], axis=1)
 test = test.drop(['Ticket', 'Cabin'], axis=1)
 full_data = [train, test]
def load_image(img_path, preprocess_fn, img_size):
image = cv2.imread(img_path)
 image = cv2.resize(image,(img_size, img_size))
image = preprocess_fn(image)
 return image<drop_column>
freq_port = train.Embarked.dropna().mode() [0]
freq_port
images_df = images_df.set_index('PetID' )<define_search_model>
for dataset in full_data:
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
 train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
inp = Input(( 256,256,3))
backbone = DenseNet121(input_tensor = inp,
weights=".. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5",
include_top = False)
 x = backbone.output
x = GlobalAveragePooling2D()(x)
 x = Lambda(lambda x: K.expand_dims(x,axis = -1))(x)
 x = AveragePooling1D(4 )(x)
 out = Lambda(lambda x: x[:,:,0] )(x)
 m = Model(inp,out )<define_variables>
for dataset in full_data:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train['Title'], train['Sex'] )
batch_size = 256
pet_ids = images_df.index.values
n_batches = len(pet_ids)// batch_size + 1
features = {}
for b in tqdm(range(n_batches)) :
start = b*batch_size
end =(b+1)*batch_size
batch_pets = pet_ids[start:end]
batch_images = np.zeros(( len(batch_pets),256,256,3))
for i,pet_id in enumerate(batch_pets):
image_ins = images_df.loc[pet_id]
batch_images[i] = load_image(image_ins['img_path'], preprocess_input, 256)
 batch_preds = m.predict(batch_images)
 for i,pet_id in enumerate(batch_pets):
features[pet_id] = batch_preds[i]<create_dataframe>
for dataset in full_data:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',\
'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
img_feats = pd.DataFrame.from_dict(features, orient='index')
 img_feats.columns = [f'pic_{i}' for i in range(img_feats.shape[1])]
img_feats = img_feats.reset_index()
img_feats.rename({'index': 'PetID'}, axis='columns', inplace=True )<concatenate>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in full_data:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train.head()
n_components = 32
svd_ = TruncatedSVD(n_components=n_components, random_state=1337)
 features = img_feats[[f'pic_{i}' for i in range(len(img_feats.columns)- 1)]].values
svd_col = svd_.fit_transform(features)
 svd_col = pd.DataFrame(svd_col)
 svd_col = svd_col.add_prefix('IMG_SVD_Dense_')
 img_feats = pd.concat([all_ids, svd_col], axis=1 )<merge>
train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1)
 full_data = [train, test]
df_all = df_all.merge(img_feats, on='PetID', how='left')
 all_columns.extend(img_feats.columns.values)
 del img_feats
del m; gc.collect()
K.clear_session()<define_search_model>
sex_mapping = {"male": 0, "female": 1}
for dataset in full_data:
dataset['Sex'] = dataset['Sex'].map(sex_mapping)
 train.head()
inp = Input(shape=(224, 224, 3))
backbone = InceptionV3(
 input_tensor=inp,
weights='.. /input/keras-pretrained-models/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5',
include_top=False,
pooling=None, 
)
 x = backbone.output
x = GlobalAveragePooling2D()(x)
 x = Lambda(lambda x: K.expand_dims(x,axis = -1))(x)
 x = AveragePooling1D(4 )(x)
 out = Lambda(lambda x: x[:,:,0] )(x)
 m = Model(inp,out )<define_variables>
for dataset in full_data:
age_avg = dataset['Age'].mean()
age_std = dataset['Age'].std()
age_null_count = dataset['Age'].isnull().sum()
age_null_random_list = np.random.randint(age_avg - age_std, age_avg + age_std, size=age_null_count)
 dataset['Age'][np.isnan(dataset['Age'])] = age_null_random_list
dataset['Age'] = dataset['Age'].astype(int)
 train['AgeGroup'] = pd.cut(train['Age'], 5)
 print(train[['AgeGroup', 'Survived']].groupby(['AgeGroup'], as_index=False ).mean().sort_values(by='AgeGroup', ascending=True))
train.head()
batch_size = 256
pet_ids = images_df.index.values
n_batches = len(pet_ids)// batch_size + 1
features = {}
for b in tqdm(range(n_batches)) :
start = b*batch_size
end =(b+1)*batch_size
batch_pets = pet_ids[start:end]
batch_images = np.zeros(( len(batch_pets),224, 224,3))
for i,pet_id in enumerate(batch_pets):
image_ins = images_df.loc[pet_id]
batch_images[i] = load_image(image_ins['img_path'], preprocess_input, 224)
 batch_preds = m.predict(batch_images)
 for i,pet_id in enumerate(batch_pets):
features[pet_id] = batch_preds[i]<create_dataframe>
for dataset in full_data:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4
train.head()
img_feats = pd.DataFrame.from_dict(features, orient='index')
 img_feats.columns = [f'pic_inception{i}' for i in range(img_feats.shape[1])]
img_feats = img_feats.reset_index()
img_feats.rename({'index': 'PetID'}, axis='columns', inplace=True )<concatenate>
train = train.drop(['AgeGroup'], axis=1)
 full_data = [train, test]
train.head()
n_components = 32
svd_ = TruncatedSVD(n_components=n_components, random_state=1337)
 features = img_feats[[f'pic_inception{i}' for i in range(len(img_feats.columns)- 1)]].values
svd_col = svd_.fit_transform(features)
 svd_col = pd.DataFrame(svd_col)
 svd_col = svd_col.add_prefix('IMG_SVD_Incep_')
 img_feats = pd.concat([all_ids, svd_col], axis=1 )<merge>
for dataset in full_data:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 train = train.drop(['FareBand'], axis=1)
 full_data = [train, test]
train.head()
df_all = df_all.merge(img_feats, on='PetID', how='left')
 all_columns.extend(img_feats.columns.values)
 del img_feats
del m; gc.collect()
K.clear_session()<define_search_model>
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
for dataset in full_data:
dataset['Embarked'] = dataset['Embarked'].map(embarked_mapping)
 train.head()
inp = Input(shape=(224, 224, 3))
backbone = Xception(
 input_tensor=inp,
weights='.. /input/keras-pretrained-models/xception_weights_tf_dim_ordering_tf_kernels_notop.h5',
include_top=False,
pooling=None, 
)
 x = backbone.output
x = GlobalAveragePooling2D()(x)
 x = Lambda(lambda x: K.expand_dims(x,axis = -1))(x)
 x = AveragePooling1D(4 )(x)
 out = Lambda(lambda x: x[:,:,0] )(x)
 m = Model(inp,out )<define_variables>
for dataset in full_data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
print(train[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False))
batch_size = 256
pet_ids = images_df.index.values
n_batches = len(pet_ids)// batch_size + 1
features = {}
for b in tqdm(range(n_batches)) :
start = b*batch_size
end =(b+1)*batch_size
batch_pets = pet_ids[start:end]
batch_images = np.zeros(( len(batch_pets),224, 224,3))
for i,pet_id in enumerate(batch_pets):
image_ins = images_df.loc[pet_id]
batch_images[i] = load_image(image_ins['img_path'], preprocess_input, 224)
 batch_preds = m.predict(batch_images)
 for i,pet_id in enumerate(batch_pets):
features[pet_id] = batch_preds[i]<create_dataframe>
for dataset in full_data:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
print(train[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean().sort_values(by='Survived', ascending=False))
img_feats = pd.DataFrame.from_dict(features, orient='index')
 img_feats.columns = [f'pic_xception{i}' for i in range(img_feats.shape[1])]
img_feats = img_feats.reset_index()
img_feats.rename({'index': 'PetID'}, axis='columns', inplace=True )<concatenate>
train = train.drop(['Parch', 'SibSp', 'FamilySize',], axis=1)
 test = test.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 full_data = [train, test]
train.head()
n_components = 32
svd_ = TruncatedSVD(n_components=n_components, random_state=1337)
 features = img_feats[[f'pic_xception{i}' for i in range(len(img_feats.columns)- 1)]].values
svd_col = svd_.fit_transform(features)
 svd_col = pd.DataFrame(svd_col)
 svd_col = svd_col.add_prefix('IMG_SVD_Xcep_')
 img_feats = pd.concat([all_ids, svd_col], axis=1 )<merge>
predictors = train.drop(['Survived', 'PassengerId'], axis=1)
 target = train["Survived"]
x_train, x_test, y_train, y_test = train_test_split(predictors, target, random_state = 0 )
df_all = df_all.merge(img_feats, on='PetID', how='left')
 all_columns.extend(img_feats.columns.values)
 del img_feats
del images_df
del m; gc.collect()
K.clear_session()<load_from_csv>
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
 y_pred = knn.predict(x_test)
 acc_knn = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_knn )
df_external_1 = pd.read_csv('.. /input/petfindercomexternal/petfinder.com_external.csv')
 df_external_1 = df_external_1.drop('attr_other_names',axis=1)
 df_external_1 = df_external_1.rename(columns={'BreedID':'pred_breed'} )<merge>
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_test)
 acc_logreg = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_logreg )
df_all = df_all.merge(df_external_1,how='left',on='pred_breed' )<concatenate>
gaussian = GaussianNB()
gaussian.fit(x_train, y_train)
 y_pred = gaussian.predict(x_test)
 acc_gaussian = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_gaussian )
all_columns.extend(df_external_1.columns[:].values )<define_variables>
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_test)
 acc_svc = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_svc )
with open('.. /input/cat-and-dog-breeds-parameters/rating.json', 'r')as f:
ratings = json.load(f)
 cat_ratings = ratings['cat_breeds']
dog_ratings = ratings['dog_breeds']
breed_id = {}
for id_,name in zip(labels_breed.BreedID,labels_breed.BreedName):
breed_id[name] = id_
breed_names_1 = [i for i in cat_ratings.keys() ]
breed_names_2 = [i for i in dog_ratings.keys() ]<create_dataframe>
linear_svc = LinearSVC()
linear_svc.fit(x_train, y_train)
 y_pred = linear_svc.predict(x_test)
 acc_linear_svc = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_linear_svc )
df_cat_breeds = pd.DataFrame(cat_ratings ).T
df_dog_breeds = pd.DataFrame(dog_ratings ).T
df_detail_breeds = pd.concat([df_cat_breeds,df_dog_breeds],axis=1)
 df_detail_breeds = df_detail_breeds.reset_index().rename(columns={'index':'Breed1'} )<train_model>
perceptron = Perceptron()
perceptron.fit(x_train, y_train)
 y_pred = perceptron.predict(x_test)
 acc_perceptron = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_perceptron )
df_detail_breeds = df_detail_breeds.append({'Breed1':'Terrier',
'Affectionate with Family': 4.606060606060606,
'Friendly Toward Strangers': 3.757575757575758,
'General Health': 4.0606060606060606,
'Intelligence': 4.090909090909091,
' Adaptability': 3.212121212121212,
' All Around Friendliness': 4.121212121212121,
' Exercise Needs': 4.454545454545454,
' Health Grooming': 2.8181818181818183,
' Trainability': 3.606060606060606,
'Adapts Well to Apartment Living': 3.9696969696969697,
'Amount Of Shedding': 2.515151515151515,
'Dog Friendly': 3.1818181818181817,
'Drooling Potential': 1.2424242424242424,
'Easy To Groom': 3.1515151515151514,
'Easy To Train': 3.3636363636363638,
'Energy Level': 4.212121212121212,
'Exercise Needs': 4.090909090909091,
'Good For Novice Owners': 3.0606060606060606,
'Incredibly Kid Friendly Dogs': 4.212121212121212,
'Intensity': 4.03030303030303,
'Potential For Mouthiness': 2.8484848484848486,
'Potential For Playfulness': 4.666666666666667,
'Potential For Weight Gain': 3.3333333333333335,
'Prey Drive': 3.5757575757575757,
'Sensitivity Level': 3.5454545454545454,
'Size': 2.0,
'Tendency To Bark Or Howl': 3.212121212121212,
'Tolerates Being Alone': 2.1818181818181817,
'Tolerates Cold Weather': 3.0303030303030303,
'Tolerates Hot Weather': 3.242424242424242,
'Wanderlust Potential': 3.6363636363636362}, ignore_index=True )<define_variables>
decisiontree = DecisionTreeClassifier()
decisiontree.fit(x_train, y_train)
 y_pred = decisiontree.predict(x_test)
 acc_decisiontree = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_decisiontree )
breed_mapper = {"White German Shepherd":"German Shepherd Dog",
"Jack Russell Terrier(Parson Russell Terrier)": "Jack Russell Terrier",
"Belgian Shepherd Dog Sheepdog": "Belgian Sheepdog",
"Shetland Sheepdog Sheltie": "Shetland Sheepdog",
"English Pointer": "Pointer",
"Appenzell Mountain Dog": "Bernese Mountain Dog",
"Yorkshire Terrier Yorkie": "Yorkshire Terrier",
"Thai Ridgeback": "Rhodesian Ridgeback",
"Spitz": "Finnish Spitz",
"Standard Poodle": "Poodle",
"Havana": "Havana Brown",
"Munsterlander": "Small Munsterlander Pointer",
"English Bulldog": "Bulldog",
"Husky": "Siberian Husky",
"Entlebucher": "Entlebucher Mountain Dog",
"Wire-haired Pointing Griffon": "Wirehaired Pointing Griffon",
"Chocolate Labrador Retriever": "Labrador Retriever",
"Belgian Shepherd Malinois": "Belgian Sheepdog",
"German Spitz": "Finnish Spitz",
"German Spitz": "Japanese Spitz",
"Smooth Fox Terrier": "Fox Terrier",
"Belgian Shepherd Tervuren": "Belgian Tervuren",
"Wirehaired Terrier": "German Wirehaired Pointer",
"Galgo Spanish Greyhound": "Greyhound",
"Welsh Corgi": "Cardigan Welsh Corgi",
"Eskimo Dog": "American Eskimo Dog",
"Sheep Dog": "Old English Sheepdog",
"American Hairless Terrier": "American Staffordshire Terrier",
"Retriever": "Labrador Retriever",
"Caucasian Sheepdog(Caucasian Ovtcharka)": "Caucasian Shepherd Dog",
"Oriental Tabby": "Oriental",
"Flat-coated Retriever": "Flat-Coated Retriever",
"Oriental Short Hair": "Oriental",
"Exotic Shorthair": "American Shorthair",
"Spaniel": "Cocker Spaniel",
"Wire Fox Terrier": "Fox Terrier",
"Oriental Long Hair": "Oriental",
"Chinese Crested Dog": "Chinese Crested",
"Applehead Siamese": "Siamese Cat",
"Klee Kai": "Alaskan Klee Kai",
"Dandi Dinmont Terrier": "Dandie Dinmont Terrier",
"Yellow Labrador Retriever": "Labrador Retriever",
"Bobtail": "American Bobtail",
"Anatolian Shepherd": "Anatolian Shepherd Dog",
"Cane Corso Mastiff": "Cane Corso",
"Bengal": "Bengal Cats",
"Pit Bull Terrier": "American Pit Bull Terrier",
"Shepherd": "German Shepherd Dog",
"Scottish Terrier Scottie": "Scottish Terrier",
"Mountain Dog": "Bernese Mountain Dog",
"Jindo": "Korean Jindo Dog",
"Foxhound": "American Foxhound",
"Bouvier des Flanders": "Bouvier des Flandres",
"Schnauzer": "Standard Schnauzer",
"Newfoundland Dog": "Newfoundland",
"Cattle Dog": "Australian Cattle Dog",
"West Highland White Terrier Westie": "West Highland White Terrier",
"Australian Cattle Dog/Blue Heeler": "Australian Cattle Dog",
"Maremma Sheepdog": "Belgian Sheepdog",
"Ragdoll": "Ragdoll Cats",
"Wheaten Terrier": "Soft Coated Wheaten Terrier",
"Setter": "English Setter",
"Siamese": "Siamese Cat",
"Black Labrador Retriever": "Labrador Retriever",
"Norwegian Forest Cat": "Norwegian Forest",
"English Coonhound": "American English Coonhound",
"Coonhound": "American English Coonhound",
"English Shepherd": "Old English Sheepdog",
"Plott Hound": "Plott",
"Brittany Spaniel": "Brittany",
"Corgi": "Cardigan Welsh Corgi",
"Illyrian Sheepdog": "Old English Sheepdog",
"Patterdale Terrier(Fell Terrier)": "Terrier",
"Nova Scotia Duck-Tolling Retriever": "Nova Scotia Duck Tolling Retriever",
"Hound": "Afghan Hound",
"Belgian Shepherd Laekenois": "Belgian Sheepdog",
"Sphynx(hairless cat)": "Sphynx",
"Mountain Cur": "Bernese Mountain Dog",
"Kai Dog": "Alaskan Klee Kai",}<categorify>
randomforest = RandomForestClassifier()
randomforest.fit(x_train, y_train)
 y_pred = randomforest.predict(x_test)
 acc_randomforest = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_randomforest )
labels_breed_temp = labels_breed.copy()
choose = labels_breed_temp.BreedName.isin(list(breed_mapper.keys()))
labels_breed_temp['NewBreedName'] = labels_breed_temp['BreedName']
labels_breed_temp.loc[choose,'NewBreedName'] = labels_breed_temp.loc[choose,'BreedName'].map(breed_mapper)
 breed_mapper_all = labels_breed_temp.set_index('BreedID')['NewBreedName'].to_dict()<merge>
sgd = SGDClassifier()
sgd.fit(x_train, y_train)
 y_pred = sgd.predict(x_test)
 acc_sgd = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_sgd )
df_all_temp = df_all.copy()
df_all_temp.Breed1 = df_all_temp.pred_breed.map(breed_mapper_all)
 df_all_temp = df_all_temp.merge(df_detail_breeds,on='Breed1',how='left')
 df_detail_breeds_cols = df_detail_breeds.columns[1:]<concatenate>
gbk = GradientBoostingClassifier()
gbk.fit(x_train, y_train)
 y_pred = gbk.predict(x_test)
 acc_gbk = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_gbk )
df_all = pd.concat(( df_all,df_all_temp[df_detail_breeds_cols]),axis=1)
 all_columns.extend(list(df_detail_breeds.columns.values[1:]))<load_from_csv>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron', 'Linear SVC',
'Decision Tree', 'Stochastic Gradient Descent', 'Gradient Boosting Classifier'],
'Score': [acc_svc, acc_knn, acc_logreg,
acc_randomforest, acc_gaussian, acc_perceptron,acc_linear_svc, acc_decisiontree,
acc_sgd, acc_gbk]})
 models.sort_values(by='Score', ascending=False )
df_external_3_cat = pd.read_csv('.. /input/pet-breed-characteristics/cat_breed_characteristics.csv')
 df_external_3_dog = pd.read_csv('.. /input/pet-breed-characteristics/dog_breed_characteristics.csv' )<concatenate>
PassengerId = test['PassengerId']
predictions = decisiontree.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : PassengerId, 'Survived': predictions })
 output.to_csv('submission.csv', index=False )