Kaggle competition: Titanic - Machine Learning from Disaster
<merge>
%matplotlib inline
np.random.seed(0)
 sns.set_palette('pastel' )
group = matrix.groupby(['date_block_num','item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id'], how='left')
 matrix['date_item_avg_item_cnt'] = matrix['date_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_item_avg_item_cnt')
 matrix.head(10 )<merge>
train = pd.read_csv(r'/kaggle/input/titanic/train.csv')
 test = pd.read_csv(r'/kaggle/input/titanic/test.csv')
 train.tail()
<merge>
train.isnull().sum()
<merge>
train.isnull().sum()
<merge>
test.isnull().sum()
<merge>
test.isnull().sum()
<merge>
avg_age_train =(train.groupby("Sex")['Age'] ).mean()
print(avg_age_train)
 avg_age_test =(test.groupby("Sex")['Age'] ).mean()
print(avg_age_test )
<merge>
for i in range(len(train['Age'])) :
if train['Age'].isnull() [i] == True and train['Sex'][i] == 'male':
train['Age'][i] = np.round(avg_age_train['male'],decimals=1)
 elif train['Age'].isnull() [i] == True and train['Sex'][i] == 'female':
train['Age'][i] = np.round(avg_age_train['female'],decimals=1)
 for i in range(len(test['Age'])) :
if test['Age'].isnull() [i] == True and test['Sex'][i] == 'male':
test['Age'][i] = np.round(avg_age_test['male'],decimals=1)
 elif test['Age'].isnull() [i] == True and test['Sex'][i] == 'female':
test['Age'][i] = np.round(avg_age_test['female'],decimals=1 )
<merge>
train = train.reset_index(drop=True)
 test = test.reset_index(drop=True )
<merge>
train.isnull().sum()
<merge>
train.isnull().sum()
<merge>
test.isnull().sum()
<prepare_x_and_y>
test.isnull().sum()
X_train = matrix[matrix.date_block_num <= 33].drop(['item_cnt_month'], axis=1)
 Y_train = matrix[matrix.date_block_num <= 33]['item_cnt_month']
X_valid = matrix[matrix.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = matrix[matrix.date_block_num == 33]['item_cnt_month']
X_test = matrix[matrix.date_block_num == 34].drop(['item_cnt_month'], axis=1 )<train_model>
Y = train['Survived']
train = train.drop('Survived',axis=1)
 data = pd.concat([train,test],axis=0)
 data.head()
model = XGBRegressor(
 max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10 )<save_to_csv>
avg_fare = data.groupby("Sex")['Fare'].mean()
avg_fare
Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('submission.csv', index=False )<import_modules>
print("Index of the null value is: ",test[test['Fare'].isnull() ].index.tolist())
 print(test['Sex'][152] )
def get_imports() :
for name, val in globals().items() :
if isinstance(val, types.ModuleType):
name = val.__name__.split(".")[0]
elif isinstance(val, type):
name = val.__module__.split(".")[0]
if name == "PIL":
name = "Pillow"
elif name == "sklearn":
name = "scikit-learn"
yield name
imports = list(set(get_imports()))
requirements = []
for m in pkg_resources.working_set:
if m.project_name in imports and m.project_name!="pip":
requirements.append(( m.project_name, m.version))
for r in requirements:
print("{}=={}".format(*r))<load_from_csv>
data['Fare'][152] = avg_fare['male']
data = pd.read_pickle('.. /input/eda-preprocessing-feature-engineering/all_data.pkl')
 data = data[data.date_block_num > 5]
test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv' ).set_index('ID')
 dropcols = [
"item_cnt_month_lag_12",
"item_cnt_month_lag_12_adv",
"date_item_target_enc_lag_12",
"date_shop_target_enc_lag_12",
"date_city_target_enc_lag_1",
"date_city_target_enc_lag_2",
"date_city_target_enc_lag_3",
"date_type_target_enc_lag_1",
"date_subtype_target_enc_lag_1",
"new_item_cat_avg_lag_1",
"new_item_cat_avg_lag_2",
"new_item_cat_avg_lag_3",
"new_item_shop_cat_avg_lag_1",
"new_item_shop_cat_avg_lag_2",
"new_item_shop_cat_avg_lag_3",
]
X_train = data[data.date_block_num < 33].drop(['item_cnt_month']+dropcols, axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month']+dropcols, axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month']+dropcols, axis=1)
 del data
gc.collect()<train_model>
data.isnull().sum()
start_time = time.time()
model = XGBRegressor(
 max_depth=10,
n_estimators=1500,
min_child_weight=0.5,
colsample_bytree=0.8,
subsample=0.7,
eta=0.01,
tree_method='gpu_hist',
seed=0)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 100)
 print(f"training took {time.time() - start_time}s" )<save_to_csv>
data.isnull().sum()
start_time = time.time()
Y_test = model.predict(X_test ).clip(0, 20)
 print(f"predicting on test set took {time.time() - start_time}s")
 start_time = time.time()
Y_train_pred = model.predict(X_train ).clip(0, 20)
 print(f"Predicting on train set took {time.time() - start_time} s")
 start_time = time.time()
Y_valid_pred = model.predict(X_valid ).clip(0, 20)
 print(f"Predicting on valid set took {time.time() - start_time} s")
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('xgb_submission.csv', index=False)
 train_preds = pd.DataFrame({
"ID": X_train.index,
"item_cnt_month": Y_train_pred
})
 train_preds.to_csv('xgb_y_train.csv', index=False)
 valid_preds = pd.DataFrame({
"ID": X_valid.index,
"item_cnt_month": Y_valid_pred
})
 valid_preds.to_csv('xgb_y_valid.csv', index=False )<train_model>
print("Number of duplicate rows in the train dataset :",train.duplicated().sum())
 print("Number of duplicate rows in the test dataset :",test.duplicated().sum() )
dump(model, 'xgb_model.joblib' )<import_modules>
Name_title_data = data['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 print(Name_title_data)
 data['Name_title'] = Name_title_data
data = data.reset_index(drop=True)
 data.head()
import matplotlib.pyplot as plt
import tensorflow as tf<import_modules>
age_group_data = [None] * len(data['Age'])
 for i in range(len(data['Age'])) :
if data['Age'][i] <= 3:
age_group_data[i] = 'Baby'
elif data['Age'][i] >3 and data['Age'][i] <= 13:
age_group_data[i] = 'Child'
elif data['Age'][i] >13 and data['Age'][i] <= 19:
age_group_data[i] = 'Teenager'
elif data['Age'][i] >19 and data['Age'][i] <= 30:
age_group_data[i] = 'Young Adult'
elif data['Age'][i] >30 and data['Age'][i] <= 45:
age_group_data[i] = 'Middle Aged Adult'
elif data['Age'][i] >45 and data['Age'][i] <65:
age_group_data[i] = 'Adult'
else:
age_group_data[i] = 'Old'
data['age_group'] = age_group_data
tf.__version__<load_from_csv>
data['Is_Married'] = 0
data['Is_Married'].loc[data['Name_title'] == 'Mrs'] = 1
data['FamSize'] = data['SibSp'] + data['Parch'] + 1
data['Single'] = data['FamSize'].map(lambda s: 1 if s == 1 else 0 )
dataset_training = pd.read_csv("/kaggle/input/sf-crime/train.csv.zip")
 dataset_test = pd.read_csv("/kaggle/input/sf-crime/test.csv.zip" )<data_type_conversions>
np.unique(data['Ticket'])
 tic = data.groupby('Ticket',sort=True,group_keys=True)
 groups = list(tic.groups)
 togther = [None] * len(data['Ticket'])
 k=0
for i in range(len(groups)) :
for j in range(len(data['Ticket'])) :
if data['Ticket'][j] == groups[i]:
togther[j] = i
data['Togther'] = togther
dataset_training_new = dataset_training.drop(['Category', 'Descript', 'Resolution'], axis=1)
 dataset_test_new = dataset_test.drop('Id', axis=1)
 dataset_training_new['Dates'] = pd.to_datetime(dataset_training_new['Dates'])
 dataset_test_new['Dates'] = pd.to_datetime(dataset_test_new['Dates'] )<feature_engineering>
rates = [None]*len(data['Fare'])
 for i in range(len(data['Fare'])) :
if data['Fare'][i]<=10:
rates[i] = 1
elif data['Fare'][i] >10 and data['Fare'][i]<=30:
rates[i] = 2
elif data['Fare'][i] >30 and data['Fare'][i]<=70:
rates[i] = 3
elif data['Fare'][i] >70 and data['Fare'][i]<=100:
rates[i] = 4
else:
rates[i] = 5
data['Rates'] = rates
dataset_training_new['Year'] = dataset_training_new['Dates'].dt.year
dataset_test_new['Year'] = dataset_test_new['Dates'].dt.year
dataset_training_new['Month'] = dataset_training_new['Dates'].dt.month
dataset_test_new['Month'] = dataset_test_new['Dates'].dt.month
dataset_training_new['Day'] = dataset_training_new['Dates'].dt.day
dataset_test_new['Day'] = dataset_test_new['Dates'].dt.day
dataset_training_new['Hour'] = dataset_training_new['Dates'].dt.hour
dataset_test_new['Hour'] = dataset_test_new['Dates'].dt.hour
dataset_training_new['Minute'] = dataset_training_new['Dates'].dt.minute
dataset_test_new['Minute'] = dataset_test_new['Dates'].dt.minute
dataset_training_new['WeekDay'] = dataset_training_new['Dates'].dt.weekday
dataset_test_new['WeekDay'] = dataset_test_new['Dates'].dt.weekday
dataset_training_new.drop(['Dates', 'DayOfWeek', 'Address'], axis=1, inplace=True)
 dataset_test_new.drop(['Dates', 'DayOfWeek', 'Address'], axis=1, inplace=True)
<categorify>
data['Cabin_present'] = 1
data['Cabin_present'].loc[data['Cabin'].isnull() ] = 0
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder() , [0])], remainder='passthrough')
 X = np.array(ct.fit_transform(dataset_training_new))
X_submission = np.array(ct.transform(dataset_test_new))<categorify>
data = data.drop('Cabin',axis=1)
 data = data.drop('Ticket',axis=1)
 data = data.drop('Name',axis=1)
 data = data.drop('PassengerId',axis=1 )
y = dataset_training['Category'].values
ohe = OneHotEncoder(sparse=False)
 y = ohe.fit_transform(y.reshape(-1, 1))
y.shape<split>
data_ohe = pd.get_dummies(data,drop_first=True)
 data_ohe.head()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 1 )<normalization>
train['Survived'] = Y
sc = MinMaxScaler()
X_train = sc.fit_transform(X_train)
 X_test = sc.transform(X_test )<train_model>
train = train.drop('Survived',axis=1 )
ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units=200, activation='relu'))
ann.add(tf.keras.layers.Dense(units=200, activation='relu'))
ann.add(tf.keras.layers.Dense(units=200, activation='relu'))
ann.add(tf.keras.layers.Dense(units=200, activation='relu'))
ann.add(tf.keras.layers.Dense(units=39, activation='softmax'))
ann.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 ann.fit(X_train, y_train, batch_size=32, epochs=25, validation_data=(X_test, y_test))<prepare_output>
train_ohe = data_ohe[:train.shape[0]]
test_ohe = data_ohe[train.shape[0]:]
X_submission = sc.transform(X_submission)
 y_pred = ann.predict(X_submission)
 m = np.max(y_pred, axis=1 ).reshape(-1, 1)
 predicted = np.array(( y_pred == m), dtype='int32')
 col_names = ohe.categories_[0]
df_submission = pd.DataFrame()
for i, entry in enumerate(col_names):
df_submission[entry] = predicted[:,i]
df_submission['Id'] = dataset_test['Id']
df_submission.set_index('Id', inplace=True)
 df_submission<save_to_csv>
X_train,X_test,Y_train,Y_test = train_test_split(train_ohe,Y,test_size=0.2 )
df_submission.to_csv('Submission.csv' )<set_options>
predictions = gbdt.predict(test_ohe)
 predictions.shape
%matplotlib inline
sns.set()<load_from_csv>
submit = pd.DataFrame(test['PassengerId'],columns=['PassengerId'])
 submit['Survived'] = predictions
submit.head()
train = pd.read_csv(".. /input/sf-crime/train.csv.zip")
 test = pd.read_csv(".. /input/sf-crime/test.csv.zip" )<count_missing_values>
submit.to_csv("Submissions.csv",index=False)
 print("Finished saving the file" )