Kaggle competition: Titanic - Machine Learning from Disaster
train_out = train_rm.copy()
test_out = test_rm.copy()
train_out = train_out.drop(train_out[train_out.LotArea > 50000].index)
 train_out = train_out.drop(train_out[train_out.GrLivArea > 4000].index )<train_model>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 print(train.shape)
 train.head(5)
X_train_out = train_out.drop(columns=[y_column] ).select_dtypes([int,float,bool])
 y_train_out = train_out[y_column]
X_test_out = test_out.drop(columns=['Id'] ).select_dtypes([int,float,bool])
 print("Training model for outliers removal technique")
 train_models(X_train_out,y_train_out )<categorify>
y = train['Survived'].to_frame()
train.drop('Survived',axis=1,inplace=True)
 train['FamilySize'] = train['SibSp'] + train['Parch']
test['FamilySize'] = test['SibSp'] + test['Parch']
train_encoded = train_out.copy()
test_encoded = test_out.copy()
train_encoded[train_encoded.select_dtypes('object' ).columns] = train_encoded[train_encoded.select_dtypes('object' ).columns].astype('category')
 test_encoded[test_encoded.select_dtypes('object' ).columns] = test_encoded[test_encoded.select_dtypes('object' ).columns].astype('category')
 category_columns = train_encoded.select_dtypes('category' ).columns
encoder = ce.CatBoostEncoder(cols=category_columns)
 encoder.fit(train_encoded[category_columns],train_encoded['SalePrice'])
 train_encoded[category_columns] = encoder.transform(train_encoded[category_columns])
 test_encoded[category_columns] = encoder.transform(test_encoded[category_columns])
<train_model>
col_with_missing_values = [col for col in train.columns if(train[col].isnull().any()
or test[col].isnull().any())]
numeric_col_with_missing_val = [col for col in col_with_missing_values if
np.issubdtype(train[col].dtype, np.number)]
non_numeric_col_with_missing_val = list(set(col_with_missing_values)- set(numeric_col_with_missing_val))
print('Column with missing values :',col_with_missing_values)
 print('Numeric Column with missing values :', numeric_col_with_missing_val)
 print('Non Numeric Column with missing values :', non_numeric_col_with_missing_val )
X_train_encoded = train_encoded.drop(columns=[y_column] ).select_dtypes([int,float,bool])
 y_train_encoded = train_encoded[y_column]
X_test_encoded = test_encoded.drop(columns=['Id'] ).select_dtypes([int,float,bool])
 print("Training model for encoded features technique")
 train_models(X_train_encoded,y_train_encoded)
<train_model>
train['Cabin'].fillna('0',inplace=True)
 test['Cabin'].fillna('0', inplace=True)
 train['Cabin'] = train['Cabin'].str[0]
test['Cabin'] = test['Cabin'].str[0]
di = {'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'T':8}
train['Cabin'].replace(di,inplace=True)
 test['Cabin'].replace(di,inplace=True)
 train['Cabin'].unique()
estimator = XGBRegressor(random_state=0)
 selector = RFE(estimator, n_features_to_select=25, step=1)
 selector = selector.fit(X_train_encoded, y_train_encoded)
 best_columns = X_train_encoded.columns[selector.support_]
train_models(X_train_encoded[best_columns],y_train_encoded )<train_on_grid>
for col in numeric_col_with_missing_val:
my_dict = train.groupby(['Pclass','Sex'])[col].agg(['median'] ).to_dict()
for(key,value)in my_dict.items() :
for(k1,v1)in value.items() :
train.loc[(( np.isnan(train[col])) |(train[col]==0)) &(train['Pclass']==k1[0])&(train['Sex']==k1[1]),col] = v1
test.loc[(( np.isnan(test[col])) |(test[col]==0)) &(test['Pclass']==k1[0])&(test['Sex']==k1[1]),col] = v1
m = train['Embarked'].mode() [0]
train['Embarked'].fillna(m,inplace=True)
 test['Embarked'].fillna(m,inplace=True )
parameters ={"alpha" : [0.05, 0.10, 0.50, 0.75, 1, 2,4,5 ] }
rdg = Ridge()
clf = GridSearchCV(rdg, parameters)
 clf.fit(X_train_encoded,y_train_encoded )<find_best_params>
def get_outlier_limits(df,col):
q1 = df[col].quantile(0.25)
 q3 = df[col].quantile(0.75)
 iqr = q3 - q1
ul = q3 + 1.5 * iqr
ll = q1 - 1.5 * iqr
return(ul,ll)
 def get_outlier_index(df,col,ul,ll):
return df[(df[col]>ul)|(df[col]<ll)].index
outlier_indices = []
outlier_columns = ['Age','FamilySize','Fare']
for col in outlier_columns:
ul, ll = get_outlier_limits(train,col)
 outlier_indices.extend(get_outlier_index(train,col,ul,ll))
drop_index = [k for(k,v)in Counter(outlier_indices ).items() if v > 1]
print('Number of outliers found :',len(drop_index))
print('Dropping outliers')
 train.drop(drop_index,axis=0,inplace=True)
 y.drop(drop_index,axis=0,inplace=True )
clf.best_params_<train_model>
columns_to_drop = ['Name','PassengerId','Ticket','SibSp','Parch']
train.drop(columns_to_drop, axis=1,inplace=True)
 test_passengerId = test['PassengerId']
test.drop(columns_to_drop, axis=1,inplace=True)
 train.head()
best_model = AveragingModels(models =(Ridge(alpha=2.0,copy_X=True,fit_intercept=False,max_iter=1000,normalize=True,random_state=0), RandomForestRegressor(criterion='mse',max_depth=35,max_features='sqrt',n_estimators=150,random_state=0)))
 best_model.fit(X_train_encoded,y_train_encoded )<predict_on_test>
t = [('labelencoder',OrdinalEncoder() ,['Sex','IsAlone','Embarked'])]
ct = ColumnTransformer(transformers = t, remainder='passthrough')
 train = ct.fit_transform(train)
 test = ct.transform(test)
predicted = test[['Id']].copy()
predicted['SalePrice'] = np.expm1(best_model.predict(X_test_encoded)) 
<save_to_csv>
X_train, X_test,y_train, y_test = train_test_split(train,y['Survived'],test_size=0.2,random_state=42)
 clf = LogisticRegression(max_iter=1000)
 for i in range(1,train.shape[1]+1):
model = SelectKBest(chi2,i)
 X_X_train = model.fit_transform(X_train,y_train)
 X_X_test = model.transform(X_test)
 clf.fit(X_X_train,y_train)
 print('Accuracy score for ', i, ' features is ',accuracy_score(y_test,clf.predict(X_X_test)))
predicted.to_csv('HousePricingEduardoRenz.csv',index=False )<set_options>
logreg_clf = LogisticRegression(max_iter=1000)
 rf_clf = RandomForestClassifier()
dt_clf = DecisionTreeClassifier()
xgb = XGBClassifier()
svm = SVC()
%matplotlib inline 
<load_from_csv>
logreg_clf.fit(X_train,y_train)
 rf_clf.fit(X_train,y_train)
 dt_clf.fit(X_train,y_train)
 xgb.fit(X_train,y_train)
 svm.fit(X_train,y_train)
 logreg_pred = logreg_clf.predict(X_test)
 rf_pred = rf_clf.predict(X_test)
 dt_pred = dt_clf.predict(X_test)
 xgb_pred = xgb.predict(X_test)
 svm_pred = svm.predict(X_test)
 average_pred =(logreg_pred + rf_pred + dt_pred + xgb_pred + svm_pred)//5
acc = accuracy_score(y_test, average_pred)
 print('Accuracy score simple averaging ', acc*100 )
train_raw=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test_raw=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<drop_column>
voting_clf = VotingClassifier(estimators=[('LogReg',logreg_clf), 
('RandomForest',rf_clf), 
('DecisionTree',dt_clf), 
('XGBoost',xgb), 
('SVM',svm)], voting='hard')
 voting_clf.fit(X_train,y_train)
 voting_pred = voting_clf.predict(X_test)
 acc = accuracy_score(y_test,voting_pred)
 print('Accuracy of voting classifier(hard)',acc*100)
train_raw = train_raw.drop(train_raw[(train_raw['GrLivArea']>4000)&(train_raw['SalePrice']<300000)].index )<concatenate>
voting_clf2 = VotingClassifier(estimators=[('LogReg',logreg_clf), 
('RandomForest',rf_clf), 
('DecisionTree',dt_clf), 
('XGBoost',xgb)], voting='soft')
 voting_clf2.fit(X_train,y_train)
 voting_pred = voting_clf2.predict(X_test)
 acc = accuracy_score(y_test,voting_pred)
 print('Accuracy of voting classifier(soft)',acc*100 )
<set_options><EOS> 
voting_clf2.fit(train,y['Survived'])
 y_pred=voting_clf.predict(test)
 submit = pd.concat([pd.DataFrame(data=test_passengerId,columns=['PassengerId']),pd.DataFrame(data=y_pred, columns=['Survived'])],axis=1)
 submit.to_csv('gender_submission.csv',index=False)