Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<find_best_params>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
model_ridge = RandomForestRegressor()
cv_rf = [rmse_cv(RandomForestRegressor(n_estimators = estimators)).mean()
for estimators in [1,10,20,40,100]]
cf_rf = pd.Series(cv_ridge, index = [1,10,20,40,100])
 print('random forest score ',cf_rf.min())
 print("number of estimators: ",cf_rf.sort_values().index[0] )<train_model>
train_original = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_original = pd.read_csv('/kaggle/input/titanic/test.csv')
 train = train_original.copy()
test = test_original.copy()
dfs = [train, test]
dfs_names = ['Train','Test']
rf = RandomForestRegressor(n_estimators=100)
 rf.fit(X_train,y )<train_on_grid>
for df in [train,test]:
df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col',
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 for df in [train,test]:
df['Title'] = df['Title'].replace(['Mlle','Mme','Ms'], 'Miss')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
param_grid = {
'max_depth': [80, 90,],
'max_features': [2, 3],
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [8, 10, 12],
'n_estimators': [100, 200]
}
grid_search = GridSearchCV(estimator = GradientBoostingRegressor() , scoring="neg_mean_squared_error", param_grid = param_grid,
cv = 3, n_jobs = -1, verbose = 2)
 grid_search.fit(X_train, y )<prepare_output>
for df in [train,test]:
df['Ticket Extracted'] = df['Ticket'].str.extract(r'([a-zA-Z]+)' )
pd.DataFrame(grid_search.cv_results_)['mean_test_score'].mean() *(-1 )<predict_on_test>
for i,j in enumerate(dfs):
print(dfs_names[i]+':')
 for col in j.select_dtypes(include='object' ).columns:
print(f'Unique number of {col}: {j[col].nunique() }' )
gradient_boost_preds = np.expm1(grid_search.best_estimator_.predict(X_test))
rf_preds = np.expm1(rf.predict(X_test))
lasso_preds = np.expm1(model_lasso.predict(X_test))<save_to_csv>
label_enc = LabelEncoder()
to_encode = ['Sex','Embarked','Ticket Extracted','Title']
for df in [train, test]:
for i in to_encode:
df[i] = df[i].astype(str)
 df[i] = label_enc.fit_transform(df[i])
 for df in [train,test]:
df.drop(['Name','Ticket','Cabin'],axis=1,inplace=True )
preds = 0.45*gradient_boost_preds +0.1*rf_preds + 0.45*lasso_preds
kaggle_solution = pd.DataFrame({"id":test.Id, "SalePrice":preds})
 kaggle_solution.to_csv("test_pred.csv", index = False )<set_options>
def get_dummies_and_drop(df,dummy_cols,drop_cols):

 df_ = pd.get_dummies(df,columns=dummy_cols)
 for i in to_drop:
try:
df_.drop(i,inplace=True,axis=1)
 except:
pass
return df_
to_dummy = ['Sex','Embarked','Ticket Extracted','Title']
to_drop = ['Sex','Embarked','Name','Ticket','Cabin','Title']
np.random.seed(10)
 %matplotlib inline
%reload_ext autoreload
%autoreload 2<load_from_csv>
def percent_na_values(df,df_name,impute_thres):

 cols_with_na = df.columns[df.isna().any() ].to_list()
df_len = len(df)
 for i in cols_with_na:
count_missing = df[i].isna().sum()
percent_missing = round(count_missing/df_len*100,1)
 print(f'Percent of {df_name} {i} missing = {percent_missing}')
 if percent_missing <= impute_thres:
print(f'Ok to impute {i}(below chosen {impute_thres}% impute threshold)')
 else:
print(f'Don't impute {i}')
 percent_na_values(train,'Train',impute_thres=25)
 print('')
 percent_na_values(test,'Test',impute_thres=25 )
data_folder = Path(".. /input")
 train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )<choose_model_class>
def multivariate_imputer(df):

 imp = IterativeImputer(max_iter=20, random_state=0,min_value=0)
 imp.fit(df)
 df_imputed = imp.transform(df)
 df_imputed = pd.DataFrame(df_imputed,columns=df.columns)
 return df_imputed
train = multivariate_imputer(train)
 test = multivariate_imputer(test )
learn_densnet = cnn_learner(train_img, models.densenet201, metrics=[error_rate, accuracy], model_dir="/tmp/model/" )<find_best_params>
imputer = SimpleImputer(strategy='most_frequent')
 def impute_df(df):
df_imputed = pd.DataFrame(imputer.fit_transform(df))
df_imputed.columns = df.columns
df = df_imputed.copy()
return df
print(f'Total NA in train: {train.isna().sum().sum() }')
 print(f'Total NA in test: {test.isna().sum().sum() }' )
learn_densnet.lr_find()
learn_densnet.recorder.plot()<train_model>
for df in [train, test]:
df['Family'] = df['Parch'] + df['SibSp']
lr = 1e-02
learn_densnet.fit_one_cycle(5 , slice(lr))<predict_on_test>
for df in [train,test]:
df.drop(['Age','Fare'],inplace=True,axis=1 )
preds,_ = learn_densnet.get_preds(ds_type=DatasetType.Test )<save_to_csv>
X = train.copy()
X.drop('Survived',inplace=True,axis=1)
 y = train['Survived'].copy()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2 )
test_df.to_csv('submission.csv', index=False )<import_modules>
def gridsearch(X,y,model,grid,cv):

 CV = GridSearchCV(estimator=model,param_grid=grid,cv=cv)
 CV.fit(X, y)
 print(CV.best_params_)
 print('Best parameters returned for use')
 return(CV.best_params_ )
import os
import time
import pandas as pd
import numpy as np
import cv2
from tqdm import tqdm
import matplotlib.pyplot as plt
from keras.models import Sequential,load_model
from keras.layers import Dense,Conv2D,Dropout,MaxPooling2D,Flatten,BatchNormalization
from keras.callbacks import EarlyStopping
from keras import optimizers
from sklearn.metrics import confusion_matrix
import seaborn as sns 
<define_variables>
xgb = XGBClassifier(learning_rate=xgb_params['learning_rate'],max_depth=xgb_params['max_depth'],
n_estimators=xgb_params['n_estimators'],subsample=xgb_params['subsample'])
 forest = RandomForestClassifier(max_depth=forest_params['max_depth'],max_leaf_nodes=forest_params['max_leaf_nodes'],
n_estimators=forest_params['n_estimators'] )
train_path='.. /input/train/train'
test_path='.. /input/test/test'<prepare_x_and_y>
def cross_val(model_name,model,X,y,cv):

 scores = cross_val_score(model, X, y, cv=cv)
 print(f'{model_name} Scores:')
 for i in scores:
print(round(i,2))
print(f'Average {model_name} score: {round(scores.mean() ,2)}' )
label_train=pd.read_csv(".. /input/train.csv")
 label_train=label_train.sort_values(by=['id'])
 id=label_train['id'].values
l=label_train['has_cactus'].values
train=[]
X=[]
Y=[]
a=0
for i in tqdm(sorted(os.listdir(train_path))):
path=os.path.join(train_path,i)
 i=cv2.imread(path,cv2.IMREAD_COLOR)
 X.append(i)
 train.append([np.array(i),l[a]])
 a=a+1
train=np.array(train)
 Y=train[:,1]
train=train[:,0]
X=np.array(X)
 X.shape
X=X/255
train=train/255<train_model>
xgb.fit(X,y)
 preds = xgb.predict(test)
 test.PassengerId.astype(int )
<choose_model_class><EOS> 
output = pd.DataFrame({'PassengerId': test.PassengerId.astype(int), 'Survived': preds.astype(int)})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )