Kaggle competition: Titanic - Machine Learning from Disaster
test_df.isnull().sum().index[test_df.isnull().sum().values>0]<drop_column>
train = pd.read_csv('.. /input/train.csv' , index_col = 'PassengerId')
 label = train['Survived']
test = pd.read_csv('.. /input/test.csv', index_col = 'PassengerId')
 index = test.index
test_df.drop(["Id"],axis=1,inplace=True )<categorify>
train['Deck'] = train.Cabin.str.get(0)
 test['Deck'] = test.Cabin.str.get(0)
 train['Deck'] = train['Deck'].fillna('NOTAVL')
 test['Deck'] = test['Deck'].fillna('NOTAVL')
 train.Deck.replace('T' , 'G' , inplace = True)
 train.drop('Cabin' , axis = 1 , inplace =True)
 test.drop('Cabin' , axis = 1 , inplace =True )
test_num_transformed=qt.transform(test_df[num_train_columns] )<create_dataframe>
train.isna().sum()
df_test_num_transformed=pd.DataFrame(test_num_transformed.reshape(-1,36),columns=test_df[num_train_columns].columns )<define_search_space>
test.isna().sum()
outlier_cols=df_test_num_transformed.skew().index[df_test_num_transformed.skew().values>1]
for cols in outlier_cols:
df_test_num_transformed[cols]=winsorize(df_test_num_transformed[cols], limits=[0.05, 0.05] )<categorify>
train.loc[train.Embarked.isna() , 'Embarked'] = 'S'
X_test_num_transformed=rs.transform(df_test_num_transformed )<create_dataframe>
age_to_fill = train.groupby(['Pclass' , 'Sex' , 'Embarked'])[['Age']].median()
age_to_fill
X_test_num_transformed=pd.DataFrame(X_test_num_transformed.reshape(-1,36),columns=test_df[num_train_columns].columns )<concatenate>
for cl in range(1,4):
for sex in ['male' , 'female']:
for E in ['C' , 'Q' , 'S']:
filll = pd.to_numeric(age_to_fill.xs(cl ).xs(sex ).xs(E ).Age)
 train.loc[(train.Age.isna() &(train.Pclass == cl)&(train.Sex == sex)
 &(train.Embarked == E)) , 'Age'] =filll
test.loc[(test.Age.isna() &(test.Pclass == cl)&(test.Sex == sex)
 &(test.Embarked == E)) , 'Age'] =filll
test_transformed= pd.concat([X_test_num_transformed,test_df[cat_columns]],axis=1 )<prepare_x_and_y>
train.Ticket = pd.to_numeric(train.Ticket.str.split().str[-1] , errors='coerce')
 test.Ticket = pd.to_numeric(test.Ticket.str.split().str[-1] , errors='coerce' )
X_test_catboost_reduced=test_transformed[embeded_cat_feature]<predict_on_test>
train.isna().sum()
test_cat_predictions=tuned_cat.predict(X_test_catboost_reduced )<predict_on_test>
test.isna().sum()
test_transformed_copy=test_transformed.copy()
for col in cat_columns:
test_transformed_copy[col] = test_transformed_copy[col].astype('category')
 X_test_gbm_reduced=test_transformed_copy[embeded_gbm_feature]
test_gbm_predictions=tuned_gbm.predict(X_test_gbm_reduced)
<create_dataframe>
train['Status'] = train['Name'].str.split(',' ).str.get(1 ).str.split('.' ).str.get(0 ).str.strip()
test['Status'] = test['Name'].str.split(',' ).str.get(1 ).str.split('.' ).str.get(0 ).str.strip()
importan_person = ['Dr' , 'Rev' , 'Col' , 'Major' , 'Mlle' , 'Don' , 'Sir' , 'Ms' , 'Capt' , 'Lady' , 'Mme' , 'the Countess' , 'Jonkheer' , 'Dona']
for person in importan_person:
train.Status.replace(person, 'IMP' , inplace =True)
 test.Status.replace(person, 'IMP' , inplace =True )
pd.DataFrame(np.expm1(test_gbm_predictions))<define_variables>
test.drop(['Name' , 'Ticket' ] ,axis = 1, inplace = True)
 train.drop(['Survived','Ticket' ,'Name' ], inplace =True , axis =1 )
final_predictions=(0.7*np.expm1(test_cat_predictions)) +(0.3*np.expm1(test_gbm_predictions))<prepare_output>
cat_col = ['Pclass' , 'Sex' , 'Embarked' , 'Status' , 'Deck']
train.Pclass.replace({
1 :'A' , 2:'B' , 3:'C'
} , inplace =True)
 test.Pclass.replace({
1 :'A' , 2:'B' , 3:'C'
} , inplace =True)
 train = pd.get_dummies(train , columns=cat_col)
 test = pd.get_dummies(test , columns=cat_col)
 print(train.shape , test.shape )
sub = pd.DataFrame()
sub['Id'] = test_df_copy['Id']
sub['SalePrice'] = final_predictions
sub.head()<save_to_csv>
scaler = MinMaxScaler()
train= scaler.fit_transform(train)
 test = scaler.transform(test )
sub.to_csv('submission.csv',index=False )<load_from_csv>
model = RandomForestClassifier(bootstrap= True , min_samples_leaf= 3, n_estimators = 500 ,
min_samples_split = 10, max_features = "sqrt", max_depth= 6)
 cross_val_score(model , train , label , cv=5 )
categorical = pd.read_csv("/kaggle/input/categorical-raw/full_features.csv")
 features = pd.read_csv("/kaggle/input/housepriceregression-featurecreation/FeatureFilled.csv")
 targets = pd.read_csv("/kaggle/input/housepriceregression-featurecreation/salePrice.csv")
 with open("/kaggle/input/housepriceregression-featurecreation/new_adboost", "rb")as file:
new_adboost = pickle.load(file )<drop_column>
model = LogisticRegression()
cross_val_score(model , train , label , cv=5 )
features_sub = features.drop(['Utilities', 'Street', 'PoolQC',], axis=1 )<feature_engineering>
model = SVC(C=4)
 cross_val_score(model , train , label , cv=5 )
features_add = features.copy()
features_add['YrBltAndRemod']=features['YearBuilt']+features['YearRemodAdd']
features_add['TotalSF']=features['TotalBsmtSF'] + features['1stFlrSF'] + features['2ndFlrSF']
features_add['Total_sqr_footage'] =(features['BsmtFinSF1'] + features['BsmtFinSF2'] +
features['1stFlrSF'] + features['2ndFlrSF'])
 features_add['Total_Bathrooms'] =(features['FullBath'] +(0.5 * features['HalfBath'])+
features['BsmtFullBath'] +(0.5 * features['BsmtHalfBath']))
features_add['Total_porch_sf'] =(features['OpenPorchSF'] + features['3SsnPorch'] +
features['EnclosedPorch'] + features['ScreenPorch'] +
features['WoodDeckSF'] )<feature_engineering>
model.fit(train , label)
 pre = model.predict(test )
features_add['haspool'] = features['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 features_add['has2ndfloor'] = features['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 features_add['hasgarage'] = features['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 features_add['hasbsmt'] = features['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 features_add['hasfireplace'] = features['Fireplaces'].apply(lambda x: 1 if x > 0 else 0 )<drop_column>
ans = pd.DataFrame({'PassengerId' : index , 'Survived': pre})
 ans.to_csv('submit.csv', index = False)
 ans.head()