Kaggle competition: Titanic - Machine Learning from Disaster
train = pd.read_csv('.. /input/train.csv', parse_dates=['Dates'])
 test = pd.read_csv('.. /input/test.csv', parse_dates=['Dates'], index_col='Id')
 train.drop_duplicates(inplace=True)
 train.replace({'X': -120.5, 'Y': 90.0}, np.NaN, inplace=True)
 test.replace({'X': -120.5, 'Y': 90.0}, np.NaN, inplace=True)
 imp = SimpleImputer(strategy='mean')
 for district in train['PdDistrict'].unique() :
train.loc[train['PdDistrict'] == district, ['X', 'Y']] = imp.fit_transform(
 train.loc[train['PdDistrict'] == district, ['X', 'Y']])
 test.loc[test['PdDistrict'] == district, ['X', 'Y']] = imp.transform(
 test.loc[test['PdDistrict'] == district, ['X', 'Y']])
 train_data = lgb.Dataset(
 train, label=y, categorical_feature=['PdDistrict'], free_raw_data=False)
 train = feature_engineering(train)
 train.drop(columns=['Descript','Resolution'], inplace=True)
 test = feature_engineering(test)
 le1 = LabelEncoder()
train['PdDistrict'] = le1.fit_transform(train['PdDistrict'])
 test['PdDistrict'] = le1.transform(test['PdDistrict'])
 le2 = LabelEncoder()
X = train.drop(columns=['Category'])
 y= le2.fit_transform(train['Category'])
 train_data = lgb.Dataset(
 X, label=y, categorical_feature=['PdDistrict'])
 params = {'boosting':'gbdt',
'objective':'multiclass',
'num_class':39,
'max_delta_step':0.9,
'min_data_in_leaf': 21,
'learning_rate': 0.4,
'max_bin': 465,
'num_leaves': 41
}
bst = lgb.train(params, train_data, 100)
 predictions = bst.predict(test)
 submission = pd.DataFrame(
 predictions,
columns=le2.inverse_transform(np.linspace(0, 38, 39, dtype='int16')) ,
index=test.index)
 submission.to_csv(
 'LGBM_final.csv', index_label='Id' )<filter>
df = pd.read_csv("/kaggle/input/titanic/train.csv")
 df_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 df.head()
data_for_prediction = test.loc[[846262]]
data_for_prediction<import_modules>
y_train = df["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", "Honoured"]
X_train = pd.get_dummies(df[features])
 X_test = pd.get_dummies(df_test[features] )
sns.set(rc={'figure.figsize':(12,8.27)})
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<find_best_model_class>
rf = RandomForestClassifier(n_estimators=200, max_depth=3, random_state=1)
 rf.fit(X_train, y_train)
 scores = cross_val_score(rf, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores)
 print("Mean: ", scores.mean())
 print("Standard Deviation: ", scores.std())
def train_models(X,y,kfolds=12):
kfold = KFold(kfolds)
 for model in models:
cf_result = cross_val_score(model['regressor'], X, y, cv=kfold,scoring='neg_mean_squared_log_error')
 model['cv_results'] = np.sqrt(cf_result*-1)
 msg = f"Regressor: {model['name']}, rmsle:{cf_result.mean().round(11)} "
print(msg)
 class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, models):
self.models = models
def fit(self, X, y):
self.models_ = [clone(x)for x in self.models]
for model in self.models_:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([
model.predict(X)for model in self.models_
])
 return np.mean(predictions, axis=1 )<choose_model_class>
lr = LogisticRegression()
lr.fit(X_train, y_train)
 scores = cross_val_score(lr, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores)
 print("Mean: ", scores.mean())
 print("Standard Deviation: ", scores.std() )
models = []
models.append({'name':'XBR','regressor':XGBRegressor(random_state=0)})
 models.append({'name' :'Random Forest','regressor': RandomForestRegressor(criterion='mse',max_depth=35,max_features='sqrt',n_estimators=150,random_state=0)})
 models.append({'name': 'Ridge','regressor' :Ridge(alpha=2.0,copy_X=True,fit_intercept=False,max_iter=1000,normalize=True,random_state=0)})
 models.append({'name': 'AveragingModels','regressor' :AveragingModels(models =(XGBRegressor() ,Lasso() , Ridge(alpha=2.0), RandomForestRegressor(n_estimators=200,random_state=0)))} )<load_from_csv>
model_svm = svm.SVC()
model_svm.fit(X_train, y_train)
 scores_svm = cross_val_score(model_svm, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_svm)
 print("Mean: ", scores_svm.mean())
 print("Standard Deviation: ", scores_svm.std() )
original_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 original_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 y_column = 'SalePrice'
original_train.head()<drop_column>
model_gbc = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
 model_gbc.fit(X_train, y_train)
 scores_gbc = cross_val_score(model_gbc, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_gbc)
 print("Mean: ", scores_gbc.mean())
 print("Standard Deviation: ", scores_gbc.std() )
train = original_train.copy().drop(columns=['Id'])
 test = original_test.copy()<count_missing_values>
model_bagging = BaggingClassifier()
model_bagging.fit(X_train, y_train)
 scores_bagging = cross_val_score(model_bagging, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_bagging)
 print("Mean: ", scores_bagging.mean())
 print("Standard Deviation: ", scores_bagging.std() )
train.isnull().sum().sort_values(ascending=False )<data_type_conversions>
model_gnb = GaussianNB()
model_gnb.fit(X_train, y_train)
 scores_gnb = cross_val_score(model_gnb, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_gnb)
 print("Mean: ", scores_gnb.mean())
 print("Standard Deviation: ", scores_gnb.std() )
no_missing_train = train.copy().drop(columns=['PoolQC','MiscFeature','Alley','Fence'])
 no_missing_train.FireplaceQu = no_missing_train.FireplaceQu.fillna('NA')
 no_missing_train.BsmtQual = no_missing_train.BsmtQual.fillna('NA')
 no_missing_train.BsmtFinType1 = no_missing_train.BsmtFinType1.fillna('NA')
 no_missing_train.BsmtCond = no_missing_train.BsmtCond.fillna('NA')
 no_missing_train.LotFrontage = no_missing_train.LotFrontage.mean()
for column in no_missing_train.isnull().sum().sort_values(ascending=False ).index:
no_missing_train[column].fillna(no_missing_train[column].mode() [0],inplace=True)
 no_missing_test = test.copy().drop(columns=['PoolQC','MiscFeature','Alley','Fence'])
 no_missing_test.FireplaceQu = no_missing_test.FireplaceQu.fillna('NA')
 no_missing_test.BsmtQual = no_missing_test.BsmtQual.fillna('NA')
 no_missing_test.BsmtFinType1 = no_missing_test.BsmtFinType1.fillna('NA')
 no_missing_test.BsmtCond = no_missing_test.BsmtCond.fillna('NA')
 no_missing_test.LotFrontage = no_missing_test.LotFrontage.mean()
for column in no_missing_test.isnull().sum().sort_values(ascending=False ).index:
no_missing_test[column].fillna(no_missing_test[column].mode() [0],inplace=True )<sort_values>
model_xgb = XGBClassifier().fit(X_train, y_train)
 model_xgb.score(X_train, y_train)
 scores_xgb = cross_val_score(model_xgb, X_train, y_train, cv = 10, scoring = "accuracy")
 print("Scores: ",scores_gnb)
 print("Mean: ", scores_gnb.mean())
 print("Standard Deviation: ", scores_gnb.std() )
no_missing_test.isnull().sum().sort_values(ascending=False )<drop_column>
eclf1 = VotingClassifier(estimators=[('rf', rf),('lr', lr),('svm', model_svm),('gbc', model_gbc),('bagging', model_bagging),('gnb', model_gnb),('xgb', model_xgb)], voting='hard')
 eclf1 = eclf1.fit(X_train, y_train)
 y_pred = eclf1.predict(X_test)
 print(y_pred)
 output = pd.DataFrame({'PassengerId': df_test.PassengerId, 'Survived': y_pred})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )