Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<save_to_csv>
warnings.filterwarnings(action='ignore', category=DataConversionWarning)
 warnings.filterwarnings(action='ignore', category=DeprecationWarning)
 warnings.filterwarnings(action='ignore', category=FutureWarning )
assert X[feature_subset].shape[1] == test[feature_subset].shape[1]
sample_submission['isFraud'] = y_preds
sample_submission.to_csv('{}_feats_{}fold_lgbm_gpu.csv'.format(len(feature_subset),EPOCHS))<train_model>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 data = train.append(test, sort=False )
print("Notebook Runtime: %0.2f Hours"%(( time.time() - notebookstart)/60/60))<import_modules>
types = pd.DataFrame(data.dtypes ).rename(columns={0: 'type'} ).sort_values(by=['type'],ascending=False)
 types
from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *<set_options>
def check_missing(df):
null_val = df.isnull().sum()
percent = 100 * df.isnull().sum() /len(df)
 missing_table = pd.concat([null_val, percent], axis=1)
 col = missing_table.rename(columns = {0 : 'Num', 1 : 'Rate'})
 return col
print("Data
cols = check_missing(data)
 types.join(cols ).sort_values(by="Rate", ascending=False )
torch.cuda.is_available()<set_options>
data.drop(['Cabin'], axis=1, inplace = True)
 data["Embarked"] = data["Embarked"].fillna(data["Embarked"].mode() [0])
 data["Embarked"] = data["Embarked"].map({"S": 0, "C" : 1, "Q" : 2})
 title_mapping = {
' (.+)Mr\.(.+)': 1, ' (.+)Master\.(.+)': 1,
' (.+)Dr\.(.+)': 2, ' (.+)Don\.(.+)': 2, ' (.+)Major\.(.+)': 2,
' (.+)Sir\.(.+)':2, ' (.+)Col\.(.+)': 2, ' (.+)Jonkheer\.(.+)': 2,
' (.+)Capt\.(.+)': 2,' (.+)Countess\.(.+)': 2, ' (.+)Dona\.(.+)': 2,
' (.+)Rev\.(.+)': 3,
' (.+)Ms\.(.+)': 4, ' (.*)Miss\.(.+)': 4, ' (.+)Mrs\.(.+)': 4,
' (.+)Mme\.(.+)': 4,' (.+)Lady\.(.+)': 4, ' (.+)Mlle\.(.+)': 4
}
data["Title"] = data["Name"].replace(title_mapping, regex=True ).astype('int')
 data["Sex"] = data["Sex"].map({"male": 0, "female": 1} )
torch.backends.cudnn.enabled<define_variables>
data['FamilySize'] = data["Parch"] + data["SibSp"]
data['IsFamily'] = data["Parch"] + data["SibSp"]
data.loc[data['IsFamily'] > 1, 'IsFamily'] = 2
data.loc[data['IsFamily'] == 1, 'IsFamily'] = 1
data.loc[data['IsFamily'] == 0, 'IsFamily'] = 0
DEFAULT_SURVIVAL_VALUE = 0.5
data['FamilySurvival'] = DEFAULT_SURVIVAL_VALUE
data['LastName'] = data['Name'].apply(lambda x: str.split(x, ",")[0])
 for grp, grp_df in data.groupby(['LastName', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 1
elif(smin==0.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 0
for _, grp_df in data.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['FamilySurvival'] == 0)|(row['FamilySurvival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 1
elif(smin==0.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 0
dog_indexes = []
cat_indexes = []
count = 0
cat_count = 1
dog_count = 1<define_variables>
train_target = data[:891]["Survived"].values
data.drop(['Name', 'PassengerId', 'Age', 'Fare', 'Ticket', 'LastName'], axis = 1, inplace = True)
 data = pd.get_dummies(data, columns=["Embarked", "Title", "Sex", "IsFamily", "FamilySurvival"], drop_first=True)
 train = data[:891]
test = data[891:]
data.dtypes
for name in os.listdir('.. /input/dogs-vs-cats-redux-kernels-edition/train/'):
if 'cat' in name and cat_count <= 8000:
cat_indexes.append(name.split('.')[1])
 cat_count += 1
if 'dog' in name and dog_count <= 8000:
dog_indexes.append(name.split('.')[1])
 dog_count += 1
if dog_count > 8000 and cat_count > 8000:
break<define_variables>
possible_features = train.columns.copy().drop('Survived')
 selector = SelectKBest(f_classif, len(possible_features))
selector.fit(train[possible_features], train_target)
 scores = -np.log10(selector.pvalues_)
 indices = np.argsort(scores)[::-1]
print('Feature importances:')
 for i in range(len(scores)) :
print('%.2f %s' %(scores[indices[i]], possible_features[indices[i]]))
train_dir = ".. /input/dogs-vs-cats-redux-kernels-edition/train/"
test_dir = ".. /input/dogs-vs-cats-redux-kernels-edition/test/"
cat_train_dir = ".. /working/dogcats/train/cats/"
cat_valid_dir = ".. /working/dogcats/valid/cats/"
dog_train_dir = ".. /working/dogcats/train/dogs/"
dog_valid_dir = ".. /working/dogcats/valid/dogs/"
dogcats_test = ".. /working/dogcats/test/"<define_variables>
fparams = \
['Sex_1', 'Title_4', 'FamilySurvival_1.0', 'Pclass', 'FareBin',
'FamilySurvival_0.5', 'Embarked_1', 'IsFamily_1', 'IsFamily_2',
'Parch', 'Title_3', 'AgeBin', 'SibSp']
train_features = train[fparams].values
test_features = test[fparams].values
CV_SPLIT_NUM = 6
rfgs_parameters = {
'n_estimators': [300],
'max_depth' : [2,3,4],
'max_features': [2,3,4],
"min_samples_split": [2,3,4],
"min_samples_leaf": [2,3,4]
}
rfc_cv = GridSearchCV(RandomForestClassifier() , rfgs_parameters, cv=CV_SPLIT_NUM)
 rfc_cv.fit(train_features, train_target)
 print("RFC GridSearch score: "+str(rfc_cv.best_score_))
print("RFC GridSearch params: ")
 print(rfc_cv.best_params_ )
<train_model><EOS> 
survived = rfc_cv.best_estimator_.predict(test_features)
 pred = pd.DataFrame(pd.read_csv(".. /input/titanic/test.csv")['PassengerId'])
 pred['Survived'] = survived.astype(int)
 pred.to_csv(".. /working/submission.csv", index = False )