Kaggle competition: Titanic - Machine Learning from Disaster
subKER = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 subKER['diagnosis'] = subemp.argmax(1 ).astype(int)
 subKER.to_csv('submissionKER.csv', index=False )<define_variables>
sns.set()
pd.set_option('display.max_rows',None )
score = [0.777, 0.758, 0.749, 0.783]
weight = [0.29, 0.16, 0.09,0.06, 0.40]
subData = [submission1, submission2, d, submission3, submission4]
predsData = [preds1, preds2, preds4]<save_to_csv>
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv' )
numClass = 5
subTemp = np.zeros(( subData[0].shape[0],numClass))
for i in range(len(subData)) :
subTemp[subData[i].index, subData[i].diagnosis.tolist() ] += weight[i]
print(subTemp)
 sub = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 sub['diagnosis'] = subTemp.argmax(1 ).astype(int)
 sub.to_csv('submission.csv', index=False )<define_variables>
print('Total by Sex')
 print(train.Sex.value_counts())
 print('
Total Survived by Sex')
 print(train.loc[train.Survived==1].Sex.value_counts() )
numClass = 5
subTemp = np.zeros(( subData[0].shape[0],numClass))
for i in range(len(subData)) :
subTemp[subData[i].index, subData[i].diagnosis.tolist() ] += weight[i]
print(subTemp)
<save_to_csv>
train_with_age=train.query('Age!="NaN"')
 print('Total Survived by Age under 10')
 print(train_with_age.loc[train_with_age.Age <= 10].Survived.value_counts())
 print('
Total Survived by Age between 10 and 20')
 print(train_with_age.loc[(train_with_age.Age > 10)&(train_with_age.Age <= 20)].Survived.value_counts())
 print('
Total Survived by Age between 20 and 30')
 print(train_with_age.loc[(train_with_age.Age > 20)&(train_with_age.Age <= 30)].Survived.value_counts())
 print('
Total Survived by Age between 30 and 45')
 print(train_with_age.loc[(train_with_age.Age > 30)&(train_with_age.Age <= 45)].Survived.value_counts())
 print('
Total Survived by Age above 45')
 print(train_with_age.loc[(train_with_age.Age > 45)].Survived.value_counts() )
sub = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 sub['diagnosis'] = subTemp.argmax(1 ).astype(int)
 sub.to_csv('submission.csv', index=False )<load_from_csv>
print('Total by Pclass')
 print(train.Pclass.value_counts())
 print('
Total Survived by Pclass')
 print(train.loc[train.Survived==1].Pclass.value_counts() )
<set_options>
complete=pd.concat([train,test],ignore_index=True )
%matplotlib inline<define_variables>
complete.drop(['Cabin','Ticket'],axis=1,inplace=True )
TRAINING = True<load_from_csv>
complete['Title']=complete.Name.str.extract('([A-Za-z]+)\.' )
test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 print(test_df.shape)
 if TRAINING:
train_df = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 print(train_df.shape)
 train_df.head()<load_from_csv>
q1_fare=complete.Fare.quantile(0.25)
 q3_fare=complete.Fare.quantile(0.75)
 IQR=q3_fare-q1_fare
min_val=q1_fare-(IQR*1.5)
 max_val=q3_fare+(IQR*1.5)
 print('Minimum: {}'.format(min_val))
print('Maximum: {}'.format(max_val))
<define_variables>
for row in range(len(complete)) :
if np.isnan(complete.loc[row,'Age'])==True:
complete.loc[row,'Age']=complete.loc[(complete.Title==(complete.loc[row,'Title'])) &(complete.SibSp==(complete.loc[row,'SibSp'])) ].Age.mean()
IMG_SIZE = 224
NB_CHANNELS = 3<categorify>
complete.Fare.fillna(complete.Fare.mean() ,inplace=True )
def get_pad_width(im, new_shape, is_rgb=True):
pad_diff = new_shape - im.shape[0], new_shape - im.shape[1]
t, b = math.floor(pad_diff[0]/2), math.ceil(pad_diff[0]/2)
 l, r = math.floor(pad_diff[1]/2), math.ceil(pad_diff[1]/2)
 if is_rgb:
pad_width =(( t,b),(l,r),(0, 0))
else:
pad_width =(( t,b),(l,r))
return pad_width
def standardize(x):
x = x.astype(np.float32)
 x = x / np.max(x)
 return(x - np.mean(x)) /(np.std(x))

 def normalize(img):
img =(( img - np.min(img)) /(np.max(img)- np.min(img)))* 255
return img.astype(np.uint8)

def crop_image(img, tol=10):
def crop_image_1(img):
mask = img > tol
return img[np.ix_(mask.any(1), mask.any(0)) ]
if img.ndim == 2:
return crop_image_1(img)
 elif img.ndim == 3:
try:
img_cpy = img.copy()
h, w, _ = img.shape
img1 = cv2.resize(crop_image_1(img[:, :, 0]),(w, h))
img2 = cv2.resize(crop_image_1(img[:, :, 1]),(w, h))
img3 = cv2.resize(crop_image_1(img[:, :, 2]),(w, h))
img[:,:,0] = img1
img[:,:,1] = img2
img[:,:,2] = img3
except:
return img_cpy
return img

 def preprocess_image(im):
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
 im = normalize(im)
 im = crop_image(im)
 im = cv2.resize(im,(IMG_SIZE, IMG_SIZE))
im = cv2.addWeighted(im, 4, cv2.GaussianBlur(im,(0, 0), IMG_SIZE / 10), -4, 128)
 return im.astype(np.uint8 )<prepare_x_and_y>
complete.loc[complete.Age.isna() ]
if TRAINING:
N = train_df.shape[0]
x_train = np.empty(( N, 224, 224, 3), dtype=np.uint8)
 for i, image_id in enumerate(tqdm(train_df['id_code'])) :
x_train[i, :, :, :] = preprocess_image(cv2.imread(
 f'.. /input/aptos2019-blindness-detection/train_images/{image_id}.png' 
))<prepare_x_and_y>
for row in range(len(complete)) :
if np.isnan(complete.loc[row,'Age'])==True:
complete.loc[row,'Age']=complete.loc[complete.Title==(complete.loc[row,'Title'])].Age.mean()
N = test_df.shape[0]
x_test = np.empty(( N, 224, 224, 3), dtype=np.uint8)
 for i, image_id in enumerate(tqdm(test_df['id_code'])) :
x_test[i, :, :, :] = preprocess_image(cv2.imread(
 f'.. /input/aptos2019-blindness-detection/test_images/{image_id}.png' 
))<categorify>
complete.drop(['Name','Embarked'],axis=1,inplace=True )
if TRAINING:
y_train = pd.get_dummies(train_df['diagnosis'] ).values
print(x_train.shape)
 print(y_train.shape)
 print(x_test.shape )<split>
def sex(x):
if x == 'male':
return 0
else:
return 1
complete['Sex']=complete.Sex.apply(sex )
if TRAINING:
x_train, x_val, y_train, y_val = train_test_split(
 x_train, y_train,
test_size=0.2
)<filter>
def age(x):
if x <= 10:
return 0
elif x <= 20:
return 1
elif x <= 30:
return 2
elif x <= 45:
return 3
else:
return 4
complete['Age']=complete.Age.apply(age )
<data_type_conversions>
train=complete.loc[0:890]
test=complete.loc[891:]
<categorify>
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
<prepare_x_and_y>
def tuning_random_forest(MaxLeafNodes,MaxDepth,NEstimators):
model=RandomForestClassifier(random_state=1,max_leaf_nodes=MaxLeafNodes,max_depth=MaxDepth,n_estimators=NEstimators)
 X=train[['Age','Fare','Pclass','Sex']]
y=train.Survived
X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2,random_state=1)
 model.fit(X_train,y_train)
 predicted=model.predict(X_test)
 accuracy=accuracy_score(y_test,predicted)
 if accuracy > 0.8:
print('Max Leaf Nodes:{} Max Depth:{} N Estimators:{}'.format(MaxLeafNodes,MaxDepth,NEstimators))
print('Accuracy: {}'.format(accuracy_score(y_test,predicted)))
 print('
')
<data_type_conversions>
model=RandomForestClassifier(random_state=1,max_leaf_nodes=30,max_depth=10,n_estimators=200)
 X=train[['Age','Fare','Pclass','Sex']]
y=train.Survived
X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2,random_state=1)
 model.fit(X_train,y_train)
 predicted=model.predict(X_test)
 accuracy_score(y_test,predicted )
<randomize_order>
test2=test.loc[:,['Age','Fare','Pclass','Sex']]
X=train[['Age','Fare','Pclass','Sex']]
y=train.Survived
model.fit(X,y)
 predicted=model.predict(test2 )
if TRAINING:
indexes = np.random.permutation(len(x_train))
x_train = x_train[indexes]
y_train = y_train[indexes]<categorify>
test2=test.loc[:,['PassengerId']]
submission=pd.DataFrame({'PassengerId':test2.PassengerId,'Survived':predicted})
 submission=submission.astype('int32')
 submission.to_csv('submission.csv',index=False )