Kaggle competition: Titanic - Machine Learning from Disaster
logTransformer = FunctionTransformer(func=np.log1p, inverse_func=np.expm1)
 featureTransformer = ColumnTransformer([ 
('log_scaling', logTransformer, ['GrLivArea', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'LotArea', 'AvgRoomSF', 'Shed', 'TotRmsAbvGrd']), 
('neighborhood_onehot', OneHotEncoder(categories=[neighborhoodCategories]), ['Neighborhood']), 
('neighborhood_grp_onehot', OneHotEncoder() , ['NeighborhoodGroups']), 
('lot_shape_onehot', OneHotEncoder(categories=[lotShapeCategories]), ['LotShape']), 
('land_slope_onehot', OneHotEncoder(categories=[landSlopeCategories]), ['LandSlope']), 
('sale_condition_onehot', OneHotEncoder(categories=[saleCondCategories]), ['SaleCondition']), 
('land_contour_onehot', OneHotEncoder() , ['LandContour']), 
('zoning_onehot', OneHotEncoder() , ['MSZoning']), 
('bldg_type_onehot', OneHotEncoder() , ['BldgType']), 
('masvrn_type_onehot', OneHotEncoder() , ['MasVnrType']), 
('house_style_onehot', OneHotEncoder() , ['HouseStyle']), 
('season_onehot', OneHotEncoder() , ['Season']),
],
remainder='passthrough'
)<choose_model_class>
train= pd.read_csv(".. /input/titanic/train.csv")
 y=train['Survived']
train.head()
%%time
xgb_model = XGBRegressor(
 max_depth=6,
n_estimators=8000,
learning_rate=0.01,
min_child_weight=1.5,
subsample=0.2,
gamma=0.01,
reg_alpha=1,
reg_lambda=0.325,
objective='reg:gamma',
booster='gbtree' 
)
 xgb_pipeline = Pipeline([ 
('preprocessing', featureTransformer), 
('xgb_regressor', xgb_model),
])
 print('XGB Regressor:')
 score_model(xgb_pipeline, X, Y )<train_model>
print(train.shape)
 print(train.columns)
 print(train.isnull().sum())
 print(train.dtypes)
 print(train.columns )
xgb_pipeline.fit(X, Y)
 X_columns = get_columns_from_transformer(xgb_pipeline.named_steps['preprocessing'], list(X.columns))<load_pretrained>
test = pd.read_csv(".. /input/titanic/test.csv")
 test.head()
transformed_X = xgb_pipeline.named_steps['preprocessing'].transform(X)
 permutation_importance = PermutationImportance(
 xgb_model,
scoring=make_scorer(neg_rmsle),
cv=2,
random_state=42,
).fit(transformed_X, Y)
 eli5.show_weights(permutation_importance, feature_names=X_columns, top=125 )<define_search_space>
print(test.shape)
 print(test.columns)
 print(test.isnull().sum())
 print(test.dtypes)
 passenger_id= test['PassengerId']
data_combined=train.append(test )
%%time
parameters = {
'xgb_regressor__objective': ['reg:gamma'],
'xgb_regressor__learning_rate': [0.01],
'xgb_regressor__n_estimators': [7900, 8000, 8100],
'xgb_regressor__max_depth': [11, 12, 13],
'xgb_regressor__booster': ['gbtree'],
'xgb_regressor__min_child_weight': [1.5],
'xgb_regressor__gamma': [0],
'xgb_regressor__subsample': [0.2],
'xgb_regressor__reg_alpha': [0, 0.9, 1],
'xgb_regressor__reg_lambda': [1, 0.3],
}
paramSearch = GridSearchCV(
 estimator=xgb_pipeline,
scoring=make_scorer(neg_rmsle),
param_grid=parameters,
cv=2,
n_jobs=-1,
verbose=3 
)
<save_to_csv>
train.set_index(['PassengerId'] , inplace=True)
 test.set_index(['PassengerId'] , inplace=True)
 train.head()
xgb_pipeline.fit(X, Y)
 y_test_predicted = xgb_pipeline.predict(x_test)
 y_test_predicted = np.rint(y_test_predicted ).astype(int)
 submission_df = pd.DataFrame({
'Id': test_df['Id'],
'SalePrice': y_test_predicted,
})
 submission_df.to_csv('./submission_xgb.csv', index=False )<import_modules>
women = train.loc[train.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
from scipy.stats import skew,norm
from scipy.stats.stats import pearsonr<load_from_csv>
men = train.loc[train.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men)
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 train_size = train.shape[0]
submission = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.csv")
 warnings.filterwarnings(action="ignore" )<feature_engineering>
imp= SimpleImputer(strategy='mean')
 imp_train=imp.fit_transform(train[['Age']])
 train['Age2']=imp_train
train.head()
all_data = pd.concat(( train.loc[:,'MSSubClass':'SaleCondition'],
test.loc[:,'MSSubClass':'SaleCondition']))
train["SalePrice"] = np.log1p(train["SalePrice"])
 numeric_features = all_data.dtypes[all_data.dtypes != "object"].index
skewed_features = train[numeric_features].apply(lambda x: skew(x.dropna()))
skewed_features = skewed_features[skewed_features > 0.75]
skewed_features = skewed_features.index
all_data[skewed_features] = np.log1p(all_data[skewed_features])
 all_data = pd.get_dummies(all_data)
 all_data = all_data.fillna(all_data.mean() )<prepare_x_and_y>
imp_test=imp.fit_transform(test[['Age']])
 test['Age2']=imp_test
test.head()
X_train = all_data[:train_size]
X_test = all_data[train_size:]
y_train = train.SalePrice<compute_test_metric>
train.isnull().sum()
def mean_absolute_percentage_error(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
 return np.mean(np.abs(( y_true - y_pred)/ y_true)) * 100<find_best_params>
train.isnull().sum()
alphas = [0.05, 0.1, 0.5, 1, 5, 10, 20, 40, 80]
cv_ridge = [np.sqrt(-cross_val_score(Ridge(alpha = alpha), X_train, y_train, scoring="neg_mean_squared_error", cv = 5)).mean()
for alpha in alphas]
best_alpha = alphas[cv_ridge.index(min(cv_ridge)) ]
cv_ridge = pd.Series(cv_ridge, index = alphas)
 min_rmse = cv_ridge.min()
print("best alpha : ", best_alpha, " and minimal rmse : ", min_rmse )<compute_train_metric>
test.isnull().sum()
model_Ridge = Ridge(10 ).fit(X_train, y_train)
 ridge_predict = np.exp(model_Ridge.predict(X_test))
solution = pd.DataFrame({"id":test.Id, "SalePrice": ridge_predict})
 percentage_error = mean_absolute_percentage_error(submission.SalePrice, solution.SalePrice)
 print(percentage_error )<save_to_csv>
test.isnull().sum()
solution.to_csv("ridge_sol.csv", index = False )<set_options>
train.Embarked.value_counts()
%matplotlib inline
color = sns.color_palette()
sns.set_style('darkgrid')
 def ignore_warn(*args, **kwargs):
pass
warnings.warn = ignore_warn
pd.set_option('display.float_format', lambda x: '{:.3f}'.format(x))
print(check_output(["ls", ".. /input"] ).decode("utf8"))
warnings.filterwarnings('ignore')
<import_modules>
train['Embarked'].fillna('S',inplace=True)
 train.isnull().sum()
from numpy import sqrt<load_from_csv>
test['Fare'].fillna(test['Fare'].median() , inplace=True)
 test.isnull().sum()
train_data = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test_data = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv" )<drop_column>
train.drop(columns=['Age','Cabin'],inplace=True)
 test.drop(columns=['Age','Cabin'],inplace=True)
 print(train.columns)
 print(test.columns )
idsUnique = len(set(train_data.Id))
idsTotal = train_data.shape[0]
idsDupli = idsTotal - idsUnique
print("Train data: there are " + str(idsDupli)+ " duplicate IDs for " + str(idsTotal)+ " total entries")
 train_data.drop("Id", axis = 1, inplace = True )<drop_column>
OH_sex=pd.get_dummies(train['Sex'])
 train.head()
idsUnique = len(set(test_data.Id))
idsTotal = test_data.shape[0]
idsDupli = idsTotal - idsUnique
print("Test data: there are " + str(idsDupli)+ " duplicate IDs for " + str(idsTotal)+ " total entries")
 test_data.drop("Id", axis = 1, inplace = True )<filter>
OH_train=pd.concat([train,OH_sex],axis=1)
 OH_train.head()
quantitative = list(train_data.dtypes[train_data.dtypes != "object"].index)
 len(quantitative )<define_variables>
OH_sex_test=pd.get_dummies(test['Sex'])
 OH_test=pd.concat([test,OH_sex_test],axis=1)
 OH_test.head()
qualitative = list(train_data.dtypes[train_data.dtypes == "object"].index)
 len(qualitative )<categorify>
OH_embarked= pd.get_dummies(OH_train['Embarked'])
 OH_embarked.head()
def encode(frame_train, frame_test,feature):
ordering = pd.DataFrame()
ordering['val'] = frame_train[feature].unique()
ordering.index = ordering.val
ordering['spmean'] = frame_train[[feature, 'SalePrice']].groupby(feature ).mean() ['SalePrice']
ordering = ordering.sort_values('spmean')
 ordering['ordering'] = range(1, ordering.shape[0]+1)
 ordering = ordering['ordering'].to_dict()
for cat, o in ordering.items() :
frame_train.loc[frame_train[feature] == cat, feature+'_E'] = o
frame_test.loc[frame_test[feature] == cat, feature+'_E'] = o
qual_encoded = []
for q in qualitative:
encode(train_data, test_data,q)
 qual_encoded.append(q+'_E')
 print(qual_encoded )<create_dataframe>
OH_train.drop(columns=['Sex','Ticket','Embarked','Name'], inplace=True)
 final_OH_train=pd.concat([OH_train,OH_embarked],axis=1)
 final_OH_train.columns
print(final_OH_train.shape )
ordering = pd.DataFrame()
ordering['val'] = train_data['MSZoning'].unique()
ordering.index = ordering.val
ordering['spmean'] = train_data[['MSZoning', 'SalePrice']].groupby('MSZoning' ).mean() ['SalePrice']
ordering = ordering.sort_values('spmean')
 ordering['ordering'] = range(1, ordering.shape[0]+1)
 ordering = ordering['ordering'].to_dict()<define_variables>
OH_embarked_test= pd.get_dummies(OH_test['Embarked'])
 OH_embarked_test.head()
print(OH_embarked_test.shape)
q = list(qualitative )<filter>
OH_test.drop(columns=['Sex','Ticket','Embarked','Name'], inplace=True)
 final_OH_test=pd.concat([OH_test,OH_embarked_test],axis=1)
 final_OH_test.columns
train_data = train_data[train_data.GrLivArea < 4500]<prepare_x_and_y>
print(final_OH_train.shape)
 print(final_OH_train.shape)
 print(final_OH_train.isnull().sum() )
ntrain = train_data.shape[0]
ntest = test_data.shape[0]
y_train = train_data.SalePrice.values
all_data = pd.concat(( train_data, test_data),ignore_index = True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))<count_missing_values>
final_OH_train.groupby('Survived' ).mean()
print(all_data.isnull().sum() )<drop_column>
print(final_OH_train.columns)
 final_OH_train.drop(columns=['Survived'],inplace=True)
 X_train, X_valid, y_train, y_valid = train_test_split(final_OH_train, y, train_size=0.8, test_size=0.2,random_state=0 )
qx = list(set(qualitative ).difference(['Neighborhood']))
all_data.drop(qx,axis = 1,inplace=True )<sort_values>
def get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y):
model = RandomForestClassifier(n_estimators=max_leaf_nodes, max_depth=11, random_state=1)
 model.fit(train_X, train_y)
 preds_val = model.predict(val_X)
 mae = mean_absolute_error(val_y, preds_val)
 return(mae)
 leaf_nodes=[50,100,300,500,1000,2000]
for i in leaf_nodes:
mae=get_mae(i,X_train,X_valid,y_train,y_valid)
 print(" current value of node : %d\t\t mae: %f"%(i,mae))
all_data_na = pd.DataFrame(( all_data.isnull().sum() / len(all_data)) * 100)
 all_data_na = all_data_na[all_data_na[0] > 0]
all_data_na.sort_values(ascending=False,by = [0],inplace = True)
 all_data_na.head(20 )<drop_column>
model = RandomForestClassifier(n_estimators=300, max_depth=11, random_state=1)
 model.fit(X_train, y_train)
 preds=model.predict(X_valid)
 matrix=classification_report(y_valid,preds)
 print(matrix)
 predictions=model.predict(final_OH_test)
all_data.drop(['PoolQC_E','MiscFeature_E','Alley_E'],axis = 1,inplace=True )<groupby>
model2=LogisticRegression(random_state=0)
 model2.fit(X_train, y_train)
 preds2=model2.predict(X_valid)
 matrix2=classification_report(y_valid,preds2)
 print(matrix2 )
fea = list(all_data_na[all_data_na[0]<20].index)
 for f in fea:
all_data[f] = all_data.groupby('Neighborhood')[f].apply(lambda x: x.fillna(x.median()))<drop_column>
output = pd.DataFrame({'PassengerId': passenger_id, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )