Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<set_options>
warnings.filterwarnings("ignore")
 %matplotlib inline
del raw_train
gc.collect()<load_from_csv>
def drop_col_not_req(df, cols):
df.drop(cols, axis = 1, inplace = True )
raw_train = pd.read_csv(basePath+'train.csv' )<merge>
def pclass_fare_category(df, Pclass_1_mean_fare, Pclass_2_mean_fare, Pclass_3_mean_fare):
if(df['Pclass'] == 1):
if(df['Fare'] <= Pclass_1_mean_fare):
return 'Pclass_1_Low_Fare'
else:
return 'Pclass_1_High_Fare'
elif(df['Pclass'] == 2):
if(df['Fare'] <= Pclass_2_mean_fare):
return 'Pclass_2_Low_Fare'
else:
return 'Pclass_2_High_Fare'
elif(df['Pclass'] == 3):
if(df['Fare'] <= Pclass_3_mean_fare):
return 'Pclass_3_Low_Fare'
else:
return 'Pclass_3_High_Fare'
raw_train_building_info = pd.merge(raw_train, building_metadata,left_on = "building_id", right_on = "building_id", how = "left")
 groupedYearBuiltTrainData = raw_train_building_info.groupby(['year_built'] ).mean().reset_index()
ax = sns.lineplot(x="year_built", y="meter_reading", data=groupedDOWTrainData_buildingInfo[['year_built','meter_reading']])
 ax.set_title('Average Meter reading by year')
 del groupedYearBuiltTrainData<drop_column>
def fill_missing_age(missing_age_train, missing_age_test):
missing_age_X_train = missing_age_train.drop(['Age'], axis = 1)
 missing_age_y_train = missing_age_train['Age']
missing_age_X_test = missing_age_test.drop(['Age'], axis = 1)
 gbm_reg = ensemble.GradientBoostingRegressor(random_state = 42)
 gbm_reg_param_grid = {'n_estimators': [2000], 'max_depth': [3], 'learning_rate': [0.01], 'max_features': [3]}
gbm_reg_grid = model_selection.GridSearchCV(gbm_reg, gbm_reg_param_grid, cv = 5, n_jobs = 25, verbose = 1, scoring = 'neg_mean_squared_error')
 gbm_reg_grid.fit(missing_age_X_train, missing_age_y_train)
 print("Age feature Best GB Params: " + str(gbm_reg_grid.best_params_))
print("Age feature Best GB Score: " + str(gbm_reg_grid.best_score_))
print("GB Train Error for 'Age' Feature Regressor: " + str(gbm_reg_grid.score(missing_age_X_train, missing_age_y_train)))
 missing_age_test['Age_GB'] = gbm_reg_grid.predict(missing_age_X_test)
 print(missing_age_test['Age_GB'][:4])
 lrf_reg = LinearRegression()
lrf_reg_param_grid = {'fit_intercept': [True], 'normalize': [True]}
lrf_reg_grid = model_selection.GridSearchCV(lrf_reg, lrf_reg_param_grid, cv = 5, n_jobs = 25, verbose = 1, scoring = 'neg_mean_squared_error')
 lrf_reg_grid.fit(missing_age_X_train, missing_age_y_train)
 print("Age feature Best LR Params: " + str(lrf_reg_grid.best_params_))
print("Age feature Best LR Score: " + str(lrf_reg_grid.best_score_))
print("LR Train Error for 'Age' Feature Regressor: " + str(lrf_reg_grid.score(missing_age_X_train, missing_age_y_train)))
 missing_age_test['Age_LRF'] = lrf_reg_grid.predict(missing_age_X_test)
 print(missing_age_test['Age_LRF'][:4])
 missing_age_test['Age'] = missing_age_test[['Age_GB', 'Age_LRF']].mean(axis = 1)
 print(missing_age_test['Age'][:4])
 drop_col_not_req(missing_age_test, ['Age_GB', 'Age_LRF'])
 return missing_age_test
del raw_train_building_info<filter>
def get_top_n_features(titanic_train_data_X, titanic_train_data_y, top_n_features):
rf_est = RandomForestClassifier(random_state = 42)
 rf_param_grid = {'n_estimators' : [500], 'min_samples_split':[2, 3], 'max_depth':[20]}
rf_grid = model_selection.GridSearchCV(rf_est, rf_param_grid, n_jobs = 25, cv = 10, verbose = 1)
 rf_grid.fit(titanic_train_data_X, titanic_train_data_y)
 print("Top N Features Best RF Params: " + str(rf_grid.best_params_))
print("Top N Features Best RF Score: " + str(rf_grid.best_score_))
print("Top N Features RF Train Error: " + str(rf_grid.score(titanic_train_data_X, titanic_train_data_y)))
 feature_imp_sorted_rf = pd.DataFrame({'feature': list(titanic_train_data_X), 'importance': rf_grid.best_estimator_.feature_importances_} ).sort_values('importance', ascending = False)
 features_top_n_rf = feature_imp_sorted_rf.head(top_n_features)['feature']
print("Sample 25 Features from RF Classifier:")
 print(str(features_top_n_rf[:25]))
ada_est = ensemble.AdaBoostClassifier(random_state = 42)
 ada_param_grid = {'n_estimators' : [500], 'learning_rate': [0.5, 0.6]}
ada_grid = model_selection.GridSearchCV(ada_est, ada_param_grid, n_jobs = 25, cv = 10, verbose = 1)
 ada_grid.fit(titanic_train_data_X, titanic_train_data_y)
 print("Top N Features Best Ada Params: " + str(ada_grid.best_params_))
print("Top N Features Best Ada Score: " + str(ada_grid.best_score_))
print("Top N Features Ada Train Error: " + str(ada_grid.score(titanic_train_data_X, titanic_train_data_y)))
 feature_imp_sorted_ada = pd.DataFrame({'feature': list(titanic_train_data_X), 'importance': ada_grid.best_estimator_.feature_importances_} ).sort_values('importance', ascending = False)
 features_top_n_ada = feature_imp_sorted_ada.head(top_n_features)['feature']
print("Sample 25 Features from Ada Classifier:")
 print(str(features_top_n_ada[:25]))
et_est = ensemble.ExtraTreesClassifier(random_state = 42)
 et_param_grid = {'n_estimators' : [500], 'min_samples_split':[3, 4], 'max_depth':[15]}
et_grid = model_selection.GridSearchCV(et_est, et_param_grid, n_jobs = 25, cv = 10, verbose = 1)
 et_grid.fit(titanic_train_data_X, titanic_train_data_y)
 print("Top N Features Best ET Params: " + str(et_grid.best_params_))
print("Top N Features Best ET Score: " + str(et_grid.best_score_))
print("Top N Features ET Train Error: " + str(et_grid.score(titanic_train_data_X, titanic_train_data_y)))
 feature_imp_sorted_et = pd.DataFrame({'feature': list(titanic_train_data_X), 'importance': et_grid.best_estimator_.feature_importances_} ).sort_values('importance', ascending = False)
 features_top_n_et = feature_imp_sorted_et.head(top_n_features)['feature']
print("Sample 25 Features from ET Classifier:")
 print(str(features_top_n_et[:25]))
features_top_n = pd.concat([features_top_n_rf, features_top_n_ada, features_top_n_et], ignore_index = True ).drop_duplicates()
return features_top_n
raw_train = raw_train [ raw_train['building_id'] != 1099 ]
raw_train = raw_train.query('not(building_id <= 104 & meter == 0 & timestamp <= "2016-05-20")' )<data_type_conversions>
train_data_orig = pd.read_csv('.. /input/train.csv')
 test_data_orig = pd.read_csv('.. /input/test.csv' )
raw_train['timestamp'] = pd.to_datetime(raw_train['timestamp'])
 raw_train['is_holiday'] =(raw_train['timestamp'].dt.date.isin(us_holidays)).astype(np.int8 )<merge>
print(combined_train_test.groupby(['Survived', 'Embarked'])['Survived'].count())
 print(combined_train_test['PassengerId'].groupby(by = combined_train_test['Embarked'] ).count().sort_values(ascending = False))
print(combined_train_test['Fare'].groupby(by = combined_train_test['Embarked'] ).mean().sort_values(ascending = False))
if(combined_train_test['Embarked'].isnull().sum() != 0):
combined_train_test['Embarked'].fillna(combined_train_test['Embarked'].mode().iloc[0], inplace=True)
 combined_train_test.info()
emb_dummies_df = pd.get_dummies(combined_train_test['Embarked'],
prefix = combined_train_test[['Embarked']].columns[0])
 combined_train_test = pd.concat([combined_train_test, emb_dummies_df], axis = 1 )
building_traindata = pd.merge(raw_train,building_metadata,left_on = "building_id", right_on = "building_id", how = "left")
 raw_train_data = pd.merge(building_traindata,weather_train,left_on = ["site_id", "timestamp"], right_on = ["site_id", "timestamp"])
 na_columns = raw_train_data.isna().sum()
print(na_columns/raw_train_data.shape[0] )<data_type_conversions>
print(combined_train_test['Sex'].groupby(by = combined_train_test['Sex'] ).count().sort_values(ascending = False))
print(combined_train_test.groupby(['Survived', 'Sex'])['Survived'].count())

sex_dummies_df = pd.get_dummies(combined_train_test['Sex'],
prefix = combined_train_test[['Sex']].columns[0])
 combined_train_test = pd.concat([combined_train_test, sex_dummies_df], axis = 1 )
raw_train_data['dayofweek'] = raw_train_data['timestamp'].dt.dayofweek.astype(np.uint8)
 raw_train_data['weekday'] = raw_train_data['timestamp'].dt.weekday.astype(np.uint8)
 raw_train_data['hour'] = raw_train_data['timestamp'].dt.hour.astype(np.uint8)
 raw_train_data['day'] = raw_train_data['timestamp'].dt.day.astype(np.uint8)
 raw_train_data['month'] = raw_train_data['timestamp'].dt.month.astype(np.uint8 )<define_search_space>
title_Dict = {}
title_Dict.update(dict.fromkeys(["Capt", "Col", "Major", "Dr", "Rev"], "Officer"))
title_Dict.update(dict.fromkeys(["Jonkheer", "Don", "Sir", "the Countess", "Dona", "Lady"], "Royalty"))
title_Dict.update(dict.fromkeys(["Mme", "Ms", "Mrs"], "Mrs"))
title_Dict.update(dict.fromkeys(["Mlle", "Miss"], "Miss"))
title_Dict.update(dict.fromkeys(["Mr", "Ms"], "Mr"))
title_Dict.update(dict.fromkeys(["Master"], "Master"))
beaufort_vals = [(0,0,0.4),(1,0.5,1.5),(2,1.6,3.3),(3,3.4,5.5),(4,5.6,7.9),(5,8.0,10.7),(6,10.8,13.8),(7,13.9,17.1), 
(8,17.2,20.7),(9,20.8,24.4),(10,24.5,28.4),(11,28.5,32.6),(12,32.7,400)]<concatenate>
combined_train_test['Title'] = combined_train_test['Title'].map(title_Dict)
 print(combined_train_test['Title'].groupby(by = combined_train_test['Title'] ).count().sort_values(ascending = False))
title_dummies_df = pd.get_dummies(combined_train_test['Title'],
prefix = combined_train_test[['Title']].columns[0])
 combined_train_test = pd.concat([combined_train_test, title_dummies_df], axis = 1 )
def degToCompass(num):
val=int(( num/22.5)+.5)
 arr=[i for i in range(0,16)]
return arr[(val % 16)]<feature_engineering>
combined_train_test['Name_Length'] = combined_train_test['Name'].str.len()
print(combined_train_test['Name_Length'].groupby(by = combined_train_test['Name_Length'] ).count().sort_values(ascending = False)[:5] )
raw_train_data['wind_direction'] = raw_train_data['wind_direction'].apply(degToCompass )<feature_engineering>
combined_train_test['Name_Length_Category'] = combined_train_test['Name_Length'].map(name_len_category)
 print(combined_train_test['Name_Length_Category'].groupby(by = combined_train_test['Name_Length_Category'] ).count().sort_values(ascending = False))
le_fare = LabelEncoder()
le_fare.fit(np.array(['Very_Short_Name', 'Short_Name', 'Medium_Name', 'Long_Name', 'Very_High_Fare']))
combined_train_test['Name_Length_Category'] = le_fare.transform(combined_train_test['Name_Length_Category'])
 print(combined_train_test[['Name_Length_Category', 'Survived']].corr())
 first_name_dummies_df = pd.get_dummies(combined_train_test['Name_Length_Category'],
prefix = combined_train_test[['Name_Length_Category']].columns[0])
 combined_train_test = pd.concat([combined_train_test, first_name_dummies_df], axis = 1 )
for i_scale in beaufort_vals:
raw_train_data.loc[(raw_train_data['wind_speed']>=i_scale[1])&(raw_train_data['wind_speed']<=i_scale[2]),'beaufort_scale'] = i_scale[0]<define_variables>
combined_train_test['First_Name'] = combined_train_test['Name'].str.extract('^ (.+?),' ).str.strip()
print(combined_train_test['First_Name'].groupby(by = combined_train_test['First_Name'] ).count().sort_values(ascending = False)[:5])
 first_name_dummies_df = pd.get_dummies(combined_train_test['First_Name'],
prefix = combined_train_test[['First_Name']].columns[0])
 combined_train_test = pd.concat([combined_train_test, first_name_dummies_df], axis = 1 )
categoricalCols = ["site_id", "building_id", "primary_use", "hour", "weekday", "meter", "wind_direction","is_holiday"]
numericCols = ["square_feet", "year_built", "air_temperature", "cloud_coverage",
"dew_temperature", 'precip_depth_1_hr', 'floor_count','beaufort_scale']<categorify>
combined_train_test['Last_Name'] = combined_train_test['Name'].str.split("\." ).str[1].str.strip()
combined_train_test['Last_Name'] = combined_train_test['Last_Name'].str.strip("\([^)]*\)")
 combined_train_test['Last_Name'].fillna(combined_train_test['Name'].str.split("\." ).str[1].str.strip())
 print(combined_train_test['Last_Name'].groupby(by = combined_train_test['Last_Name'] ).count().sort_values(ascending = False)[:5])
 last_name_dummies_df = pd.get_dummies(combined_train_test['Last_Name'],
prefix = combined_train_test[['Last_Name']].columns[0])
 combined_train_test = pd.concat([combined_train_test, last_name_dummies_df], axis = 1 )
le = LabelEncoder()
le.fit(raw_train_data['primary_use'])
 raw_train_data['primary_use'] = le.transform(raw_train_data['primary_use'] )<feature_engineering>
combined_train_test['Original_Name'] = combined_train_test['Name'].str.split("\((.*?)\)" ).str[1].str.strip (
featuresToUse = numericCols+categoricalCols
train_data = raw_train_data.loc[:,featuresToUse]
train_data['square_feet'] = np.log(train_data['square_feet'])
 train_data['year_built'] = train_data['year_built']-1900
train_data = reduce_mem_usage(train_data)
 target_data = raw_train_data['meter_reading']<set_options>
if(combined_train_test['Fare'].isnull().sum() != 0):
combined_train_test['Fare'] = combined_train_test[['Fare']].fillna(combined_train_test.groupby('Pclass' ).transform('mean'))
combined_train_test.info()
del weather_train,raw_train_data
gc.collect()<compute_train_metric>
combined_train_test['Group_Ticket'] = combined_train_test['Fare'].groupby(by = combined_train_test['Ticket'] ).transform('count')
 combined_train_test['Fare'] = combined_train_test['Fare']/combined_train_test['Group_Ticket']
combined_train_test.drop(['Group_Ticket'], axis = 1, inplace = True )
n_folds=2
cv = KFold(n_splits=n_folds, random_state=666, shuffle=False)
 def computeRMSE(clf,index):
y_predict = clf.predict(train_data.iloc[index])
 y_predict[y_predict<0]=0
return np.sqrt(mean_squared_error(target_data.iloc[index], y_predict))<init_hyperparams>
if(sum(n == 0 for n in combined_train_test.Fare.values.flatten())> 0):
combined_train_test.loc[combined_train_test.Fare == 0, 'Fare'] = np.nan
combined_train_test['Fare'] = combined_train_test[['Fare']].fillna(combined_train_test.groupby('Pclass' ).transform('mean'))
combined_train_test['Fare'].describe()
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': {'rmse'},
'subsample': 0.25,
'subsample_freq': 1,
'learning_rate': 0.4,
'num_leaves': 20,
'feature_fraction': 0.9,
'lambda_l1': 1,
'lambda_l2': 1,
'max_bin':64,
'early_stopping_rounds':200
}<split>
combined_train_test['Fare_Category'] = combined_train_test['Fare'].map(fare_category)
 le_fare = LabelEncoder()
le_fare.fit(np.array(['Very_Low_Fare', 'Low_Fare', 'Med_Fare', 'High_Fare', 'Very_High_Fare']))
combined_train_test['Fare_Category'] = le_fare.transform(combined_train_test['Fare_Category'])
 fare_cat_dummies_df = pd.get_dummies(combined_train_test['Fare_Category'],
prefix = combined_train_test[['Fare_Category']].columns[0])
 combined_train_test = pd.concat([combined_train_test, fare_cat_dummies_df], axis = 1)
 print(combined_train_test['Fare_Category'].groupby(by = combined_train_test['Fare_Category'] ).count().sort_values(ascending = False))
scores_lgb = []
feature_importances = pd.DataFrame()
feature_importances['feature'] = train_data.columns
models = []
for(train, val), i in zip(cv.split(train_data, train_data['building_id']), range(n_folds)) :
lgb_train = lgbm.Dataset(train_data.iloc[train,:], target_data.iloc[train],categorical_feature=categoricalCols)
 lgb_val = lgbm.Dataset(train_data.iloc[val,:], target_data.iloc[val],categorical_feature=categoricalCols)
 lgb_best = lgbm.train(
 params,
lgb_train,
num_boost_round=2000,
valid_sets=(lgb_train, lgb_val),
verbose_eval = 100 
)
 feature_importances['fold_{}'.format(i + 1)] = lgb_best.feature_importance()
rmse_train = computeRMSE(lgb_best,train)
 rmse_val = computeRMSE(lgb_best,val)
 scores_lgb.append(( rmse_train, rmse_val))
models.append(lgb_best )<drop_column>
print(combined_train_test['Fare'].groupby(by = combined_train_test['Pclass'] ).mean())
 Pclass_1_mean_fare = combined_train_test['Fare'].groupby(by = combined_train_test['Pclass'] ).mean().get([1] ).values[0]
Pclass_2_mean_fare = combined_train_test['Fare'].groupby(by = combined_train_test['Pclass'] ).mean().get([2] ).values[0]
Pclass_3_mean_fare = combined_train_test['Fare'].groupby(by = combined_train_test['Pclass'] ).mean().get([3] ).values[0]
del train_data, target_data<drop_column>
combined_train_test['Pclass_Fare_Category'] = combined_train_test.apply(pclass_fare_category, args=(Pclass_1_mean_fare, Pclass_2_mean_fare, Pclass_3_mean_fare), axis = 1)
 print(combined_train_test['Pclass_Fare_Category'].groupby(by = combined_train_test['Pclass_Fare_Category'] ).count().sort_values(ascending = False))
le_fare = LabelEncoder()
le_fare.fit(np.array(['Pclass_1_Low_Fare', 'Pclass_1_High_Fare', 'Pclass_2_Low_Fare', 'Pclass_2_High_Fare', 'Pclass_3_Low_Fare', 'Pclass_3_High_Fare']))
combined_train_test['Pclass_Fare_Category'] = le_fare.transform(combined_train_test['Pclass_Fare_Category'] )
del lgb_train,lgb_val,train, val<load_from_csv>
print(combined_train_test['Fare'].groupby(by = combined_train_test['Pclass'] ).mean().sort_values(ascending = True))
combined_train_test['Pclass'].replace([1, 2, 3],[Pclass_1_mean_fare, Pclass_2_mean_fare, Pclass_3_mean_fare], inplace = True )
raw_test = pd.read_csv(basePath+'test.csv')
 raw_test['timestamp'] = pd.to_datetime(raw_test['timestamp'])
 raw_test['is_holiday'] =(raw_test['timestamp'].dt.date.isin(us_holidays)).astype(np.int8)
 building_testdata = pd.merge(raw_test,building_metadata,left_on = ["building_id"], right_on = ["building_id"],how='left' )<groupby>
combined_train_test['Family_Size'] = combined_train_test['Parch'] + combined_train_test['SibSp'] + 1
print(combined_train_test['Family_Size'].groupby(by = combined_train_test['Family_Size'] ).count().sort_values(ascending = False))
combined_train_test['Family_Size_Category'] = combined_train_test['Family_Size'].map(family_size_category)
 print(combined_train_test['Family_Size_Category'].groupby(by = combined_train_test['Family_Size_Category'] ).count().sort_values(ascending = False))
print(combined_train_test.groupby(['Survived', 'Family_Size_Category'])['Survived'].count())
 le_family = LabelEncoder()
le_family.fit(np.array(['Single', 'Small_Family', 'Large_Family']))
combined_train_test['Family_Size_Category'] = le_family.transform(combined_train_test['Family_Size_Category'])
 fam_size_cat_dummies_df = pd.get_dummies(combined_train_test['Family_Size_Category'],
prefix = combined_train_test[['Family_Size_Category']].columns[0])
 combined_train_test = pd.concat([combined_train_test, fam_size_cat_dummies_df], axis = 1 )
del building_metadata,raw_test
gc.collect()
building_testdata = reduce_mem_usage(building_testdata )<merge>
print(combined_train_test['Age'].groupby(by = combined_train_test['Title'] ).mean().sort_values(ascending = True))
weather_test = pd.read_csv(basePath+'weather_test.csv')
 weather_test['timestamp'] = pd.to_datetime(weather_test['timestamp'])
 weather_test = alignTimestamp(weather_test)
 for i,i_col in enumerate(weather_na_cols):
weather_test[i_col].fillna(weather_test[i_col].mean() ,inplace=True)
 raw_test_data = pd.merge(building_testdata,weather_test,left_on = ["site_id", "timestamp"], right_on = ["site_id", "timestamp"],how = 'left')
 del weather_test,building_testdata
gc.collect()<count_missing_values>
combined_train_test['Age_Null'] = combined_train_test['Age'].apply(lambda x: 1 if(pd.notnull(x)) else 0 )
na_columns = raw_test_data.isna().sum()
print(na_columns/raw_test_data.shape[0])
 for i,i_col in enumerate(weather_na_cols):
raw_test_data[i_col].fillna(raw_test_data[i_col].mean() ,inplace=True )<feature_engineering>
missing_age_df = pd.DataFrame(combined_train_test[['Age', 'Parch', 'Sex', 'SibSp', 'Family_Size', 'Family_Size_Category', 'Title', 'Fare']])
 missing_age_df = pd.get_dummies(missing_age_df, columns = ['Title', 'Family_Size_Category', 'Sex'])
 missing_age_df.shape
missing_age_df.info()
missing_age_train = missing_age_df[missing_age_df['Age'].notnull() ]
missing_age_test = missing_age_df[missing_age_df['Age'].isnull() ]
raw_test_data['wind_direction'] = raw_test_data['wind_direction'].apply(degToCompass)
 for i_scale in beaufort_vals:
raw_test_data.loc[(raw_test_data['wind_speed']>=i_scale[1])&(raw_test_data['wind_speed']<=i_scale[2]),'beaufort_scale'] = i_scale[0]<categorify>
combined_train_test.loc[(combined_train_test.Age.isnull()), 'Age'] = fill_missing_age(missing_age_train, missing_age_test )
raw_test_data['primary_use'] = le.transform(raw_test_data['primary_use'])
 raw_test_data = reduce_mem_usage(raw_test_data )<data_type_conversions>
if(sum(n < 0 for n in combined_train_test.Age.values.flatten())> 0):
combined_train_test.loc[combined_train_test.Age < 0, 'Age'] = np.nan
combined_train_test['Age'] = combined_train_test[['Age']].fillna(combined_train_test.groupby('Title' ).transform('mean'))
raw_test_data['dayofweek'] = raw_test_data['timestamp'].dt.dayofweek.astype(np.uint8)
 raw_test_data['weekday'] = raw_test_data['timestamp'].dt.weekday.astype(np.uint8)
 raw_test_data['hour'] = raw_test_data['timestamp'].dt.hour.astype(np.uint8)
 raw_test_data['day'] = raw_test_data['timestamp'].dt.day.astype(np.uint8)
 raw_test_data['month'] = raw_test_data['timestamp'].dt.month.astype(np.uint8)
 raw_test_data = raw_test_data.drop(['timestamp'],axis=1 )<feature_engineering>
print(combined_train_test['Age'].groupby(by = combined_train_test['Title'] ).mean().sort_values(ascending = True))
raw_test_data['square_feet'] = np.log(raw_test_data['square_feet'])
 raw_test_data['year_built'] = raw_test_data['year_built']-1900
raw_test_data = raw_test_data[featuresToUse]
raw_test_data,NAList = reduce_mem_usage(raw_test_data)
 gc.collect()<predict_on_test>
combined_train_test['Age_Category'] = combined_train_test['Age'].map(age_group_cat)
 le_age = LabelEncoder()
le_age.fit(np.array(['Baby', 'Toddler', 'Child', 'Teenager', 'Adult', 'Middle_Aged', 'Senior_Citizen', 'Old']))
combined_train_test['Age_Category'] = le_age.transform(combined_train_test['Age_Category'])
 age_cat_dummies_df = pd.get_dummies(combined_train_test['Age_Category'],
prefix = combined_train_test[['Age_Category']].columns[0])
 combined_train_test = pd.concat([combined_train_test, age_cat_dummies_df], axis = 1 )
i = 0
stepSize = 50000
predictions = []
for j in tqdm(range(int(np.ceil(raw_test_data.shape[0]/stepSize)))) :
predictions.append(np.expm1(sum([model.predict(raw_test_data.iloc[i:i+stepSize])for model in models])/n_folds))
i+=stepSize
predictions = np.concatenate(predictions )<save_to_csv>
combined_train_test['Ticket_Letter'] = combined_train_test['Ticket'].str.split().str[0]
combined_train_test['Ticket_Letter'] = combined_train_test['Ticket_Letter'].apply(lambda x: np.NaN if x.isnumeric() else x)
 combined_train_test['Ticket_Number'] = combined_train_test['Ticket'].apply(lambda x: pd.to_numeric(x, errors='coerce'))
combined_train_test['Ticket_Number'].fillna(0, inplace = True)
 combined_train_test = pd.get_dummies(combined_train_test, columns = ['Ticket', 'Ticket_Letter'])
 combined_train_test.shape
submission = pd.read_csv('/kaggle/input/ashrae-energy-prediction/sample_submission.csv')
 submission['meter_reading'] = predictions
submission.loc[submission['meter_reading']<0, 'meter_reading'] = 0
submission.to_csv('submission.csv', index=False)
 submission<import_modules>
combined_train_test['Cabin_Letter'] = combined_train_test['Cabin'].apply(lambda x: str(x)[0] if(pd.notnull(x)) else x)
 combined_train_test = pd.get_dummies(combined_train_test, columns = ['Cabin', 'Cabin_Letter'])
 combined_train_test.shape
import numpy as np
import pandas as pd
import os 
<set_options>
scale_age_fare = preprocessing.StandardScaler().fit(combined_train_test[['Age', 'Fare']])
 combined_train_test[['Age', 'Fare']] = scale_age_fare.transform(combined_train_test[['Age', 'Fare']] )
warnings.simplefilter('ignore')
 warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', 1000)
 pd.set_option('display.max_rows', 500)
<define_variables>
combined_train_test.drop(['Name', 'PassengerId', 'Embarked', 'Sex', 'Title', 'Fare_Category', 'Family_Size_Category', 'Age_Category', 'First_Name', 'Last_Name', 'Original_Name', 'Name_Length_Category'], axis = 1, inplace = True )
EPOCHS = 150
NNBATCHSIZE = 32
GROUP_BATCH_SIZE = 4000
SEED = 123
LR = 0.001
SPLITS = 5
outdir = 'wavenet_models'
flip = False
noise = False
if not os.path.exists(outdir):
os.makedirs(outdir)
 def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed )<load_from_csv>
train_data = combined_train_test[:891]
test_data = combined_train_test[891:]
titanic_train_data_X = train_data.drop(['Survived'], axis = 1)
 titanic_train_data_y = train_data['Survived']
titanic_test_data_X = test_data.drop(['Survived'], axis = 1 )
def read_data() :
train = pd.read_csv('.. /input/data-without-drift/train_clean.csv', dtype={'time': np.float32, 'signal': np.float32, 'open_channels':np.int32})
 test = pd.read_csv('/kaggle/input/data-without-drift/test_clean.csv', dtype={'time': np.float32, 'signal': np.float32})
 Y_train_proba = np.load("/kaggle/input/ion-shifted-rfc-proba/Y_train_proba.npy")
 Y_test_proba = np.load("/kaggle/input/ion-shifted-rfc-proba/Y_test_proba.npy")
 print(train.shape,Y_train_proba.shape)
 for i in range(11):
train[f"proba_{i}"] = Y_train_proba[:, i]
test[f"proba_{i}"] = Y_test_proba[:, i]
sub = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv', dtype={'time': np.float32})
 return train, test, sub
def batching(df, batch_size):
df['group'] = df.groupby(df.index//batch_size, sort=False)['signal'].agg(['ngroup'] ).values
df['group'] = df['group'].astype(np.uint16)
 return df
def normalize(train, test):
train_input_mean = train.signal.mean()
train_input_sigma = train.signal.std()
train['signal'] =(train.signal - train_input_mean)/ train_input_sigma
test['signal'] =(test.signal - train_input_mean)/ train_input_sigma
return train, test
def lag_with_pct_change(df, windows):
for window in windows:
df['signal_shift_pos_' + str(window)] = df.groupby('group')['signal'].shift(window ).fillna(0)
 df['signal_shift_neg_' + str(window)] = df.groupby('group')['signal'].shift(-1 * window ).fillna(0)
 return df
def run_feat_engineering(df, batch_size):
df = batching(df, batch_size = batch_size)
 df = lag_with_pct_change(df, [1, 2, 3])
 df['signal_2'] = df['signal'] ** 2
return df
def feature_selection(train, test):
features = [col for col in train.columns if col not in ['index', 'group', 'open_channels', 'time']]
train = train.replace([np.inf, -np.inf], np.nan)
 test = test.replace([np.inf, -np.inf], np.nan)
 for feature in features:
feature_mean = pd.concat([train[feature], test[feature]], axis = 0 ).mean()
train[feature] = train[feature].fillna(feature_mean)
 test[feature] = test[feature].fillna(feature_mean)
 return train, test, features
def split(GROUP_BATCH_SIZE=4000, SPLITS=5):
print('Reading Data Started...')
 train, test, sample_submission = read_data()
train, test = normalize(train, test)
 print('Reading and Normalizing Data Completed')
 print('Creating Features')
 print('Feature Engineering Started...')
 train = run_feat_engineering(train, batch_size=GROUP_BATCH_SIZE)
 test = run_feat_engineering(test, batch_size=GROUP_BATCH_SIZE)
 train, test, features = feature_selection(train, test)
 print(train.head())
 print('Feature Engineering Completed...')
 target = ['open_channels']
group = train['group']
kf = GroupKFold(n_splits=SPLITS)
 splits = [x for x in kf.split(train, train[target], group)]
new_splits = []
for sp in splits:
new_split = []
new_split.append(np.unique(group[sp[0]]))
new_split.append(np.unique(group[sp[1]]))
new_split.append(sp[1])
 new_splits.append(new_split)
 target_cols = ['open_channels']
print(train.head() , train.shape)
 train_tr = np.array(list(train.groupby('group' ).apply(lambda x: x[target_cols].values)) ).astype(np.float32)
 train = np.array(list(train.groupby('group' ).apply(lambda x: x[features].values)))
 test = np.array(list(test.groupby('group' ).apply(lambda x: x[features].values)))
 print(train.shape, test.shape, train_tr.shape)
 return train, test, train_tr, new_splits<import_modules>
rf_est = ensemble.RandomForestClassifier(n_estimators = 750, criterion = 'gini', max_features = 'sqrt', max_depth = 3, min_samples_split = 4, min_samples_leaf = 2, n_jobs = 50, random_state = 42, verbose = 1)
 gbm_est = ensemble.GradientBoostingClassifier(n_estimators = 900, learning_rate = 0.0008, loss = 'exponential', min_samples_split = 3, min_samples_leaf = 2, max_features ='sqrt', max_depth = 3, random_state = 42, verbose = 1)
 et_est = ensemble.ExtraTreesClassifier(n_estimators = 750, max_features = 'sqrt', max_depth = 35, n_jobs = 50, criterion = 'entropy', random_state = 42, verbose = 1)
 voting_est = ensemble.VotingClassifier(estimators = [('rf', rf_est),('gbm', gbm_est),('et', et_est)],
voting = 'soft', weights = [3,5,2],
n_jobs = 50)
 voting_est.fit(titanic_train_data_X, titanic_train_data_y)
 print("VotingClassifier Score: " + str(voting_est.score(titanic_train_data_X, titanic_train_data_y)))
 print("VotingClassifier Estimators: " + str(voting_est.estimators_))
import torch.nn.functional as F 
<choose_model_class>
titanic_test_data_X['Survived'] = voting_est.predict(titanic_test_data_X )
<define_search_model><EOS> 
submission = pd.DataFrame({'PassengerId': test_data_orig.loc[:, 'PassengerId'],
'Survived': titanic_test_data_X.loc[:, 'Survived']})
 submission.to_csv(".. /working/submission.csv", index = False )