Kaggle competition: Titanic - Machine Learning from Disaster
%%time
test_data = pd.read_csv(INPUT_DIR+'test_V2.csv')
 print("fix winPlacePerc")
 for i in range(len(test_data)) :
winPlacePerc = predicted_lasso[i]
maxPlace = int(test_data.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
predicted_lasso[i] = winPlacePerc<save_to_csv>
import pandas as pd
import numpy as np
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
f4=open(INPUT_DIR+'sample_submission_V2.csv')
 submit_lasso=pd.read_csv(f4)
 sample_result_lasso = pd.DataFrame(predicted_lasso,columns = ['winPlacePerc'])
 submit_lasso['winPlacePerc'] = sample_result_lasso
submit_lasso.to_csv(r'sample_submission_lasso.csv', index=False)
 del f4,submit_lasso,sample_result_lasso
gc.collect()<train_model>
from sklearn.model_selection import GridSearchCV
%%time
model_ridge = Ridge(alpha=0.5994842503189409)
 model_ridge.fit(X_train, y_train)
 print(model_ridge.intercept_)
 print(model_ridge.coef_ )<predict_on_test>
from sklearn.model_selection import GridSearchCV
%%time
predicted_ridge = model_ridge.predict(x_prediction )<load_from_csv>
train_df = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 print(train_df.shape)
 print(test_df.shape)
 train_df.head()
%%time
test_data = pd.read_csv(INPUT_DIR+'test_V2.csv')
 print("fix winPlacePerc")
 for i in range(len(test_data)) :
winPlacePerc = predicted_ridge[i]
maxPlace = int(test_data.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
predicted_ridge[i] = winPlacePerc<save_to_csv>
train_df[['Pclass','Survived']].groupby('Pclass' ).mean().sort_values(by = 'Survived', ascending = False )
f5=open(INPUT_DIR+'sample_submission_V2.csv')
 submit_ridge=pd.read_csv(f5)
 sample_result_ridge = pd.DataFrame(predicted_ridge,columns = ['winPlacePerc'])
 submit_ridge['winPlacePerc'] = sample_result_ridge
submit_ridge.to_csv(r'sample_submission_ridge.csv', index=False)
 del f5,submit_ridge,sample_result_ridge
gc.collect()<train_model>
train_df[['Sex','Survived']].groupby('Sex' ).mean().sort_values(by = 'Survived', ascending = False )
%%time
model_elasticnet = ElasticNet(alpha=1.6152516038498196e-06, copy_X=True, fit_intercept=True, l1_ratio=0.5,
max_iter=1000, normalize=False, positive=False, precompute=False,
random_state=0, selection='cyclic', tol=0.0001, warm_start=False)
 model_elasticnet.fit(X_train, y_train)
 print(model_elasticnet.intercept_)
 print(model_elasticnet.coef_ )<predict_on_test>
train_df[['SibSp','Survived']].groupby('SibSp' ).mean().sort_values(by = 'Survived', ascending = False )
%%time
predicted_elasticNet = model_elasticnet.predict(x_prediction )<load_from_csv>
train_df[['Parch','Survived']].groupby('Parch' ).mean().sort_values(by = 'Survived', ascending = False )
%%time
test_data = pd.read_csv(INPUT_DIR+'test_V2.csv')
 print("fix winPlacePerc")
 for i in range(len(test_data)) :
winPlacePerc = predicted_elasticNet[i]
maxPlace = int(test_data.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
predicted_elasticNet[i] = winPlacePerc<save_to_csv>
labelencoder = LabelEncoder()
train_df['Sex'] = labelencoder.fit_transform(train_df['Sex'])
 test_df['Sex'] = labelencoder.fit_transform(test_df['Sex'])
 test_df.head()
f6=open(INPUT_DIR+'sample_submission_V2.csv')
 submit_elasticNet=pd.read_csv(f6)
 sample_result_elasticNet = pd.DataFrame(predicted_elasticNet,columns = ['winPlacePerc'])
 submit_elasticNet['winPlacePerc'] = sample_result_elasticNet
submit_elasticNet.to_csv(r'sample_submission_elasticNet.csv', index=False)
 del f6,submit_elasticNet,sample_result_elasticNet
gc.collect()<import_modules>
train_df['Family size'] = train_df['SibSp'] + train_df['Parch'] + 1
test_df['Family size'] = test_df['SibSp'] + test_df['Parch'] + 1
train_df[['Family size','Survived']].groupby('Family size' ).mean().sort_values(by = 'Survived', ascending = False )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso
from catboost import CatBoostRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import StandardScaler,MinMaxScaler<load_from_csv>
train_df['Fam_type'] = pd.cut(train_df['Family size'], [0,1,4,7,11], labels=['Solo', 'Small', 'Big', 'Very big'])
 test_df['Fam_type'] = pd.cut(test_df['Family size'], [0,1,4,7,11], labels=['Solo', 'Small', 'Big', 'Very big'] )
train=pd.read_csv('.. /input/train_V2.csv')
 test=pd.read_csv('.. /input/test_V2.csv')
 ID=test['Id']<count_missing_values>
combine = [train_df, test_df]
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_df['Title'], train_df['Sex'] )
train.isna().sum()<correct_missing_values>
for dataset in combine:
dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Don', 'Sir', 'Jonkheer', 'Dona'],'Royalty')
 dataset['Title'] = dataset['Title'].replace(['Capt', 'Col','Dr','Major','Rev'],'Special')
 train_df[['Title','Survived']].groupby(['Title'], as_index=False ).mean()
train=train.dropna(axis=0 )<prepare_x_and_y>
y = train_df['Survived']
features = ['Pclass','Sex','Fam_type','Fare','Age Bin','Embarked']
X = train_df[features]
X.head()
y_train=train['winPlacePerc']
train=train.drop(['winPlacePerc'],axis=1 )<categorify>
numerical_col = ['Fare']
categorical_col = ['Pclass','Sex','Fam_type','Age Bin','Embarked']
num_trans = SimpleImputer(strategy = 'median')
 cat_trans = Pipeline(steps = [ 
('imputer',SimpleImputer(strategy = 'most_frequent')) , 
('onehot',OneHotEncoder())
 ])
 preprocessor = ColumnTransformer(
 transformers = [ 
('num',num_trans,numerical_col), 
('cat',cat_trans,categorical_col)
 ])
 titanic_pipeline = Pipeline(steps=[('preprocessor', preprocessor), 
('model', RandomForestClassifier(random_state=0 
))
])
 param_grid = {
'model__max_depth': [2, 3, 4, 5],
'model__min_samples_leaf': [3, 4, 5],
'model__min_samples_split': [6, 8, 10, 12],
'model__n_estimators': [100, 200, 300, 500]
}
search = GridSearchCV(titanic_pipeline, param_grid, n_jobs=-1)
 search.fit(X, y)
 print(search.best_params_)
train["playersInMatch"] = train.groupby("matchId")["Id"].transform("count")
 train["playersInGroup"] = train.groupby("groupId")["Id"].transform("count")
 test["playersInMatch"] = test.groupby("matchId")["Id"].transform("count")
 test["playersInGroup"] = test.groupby("groupId")["Id"].transform("count" )<categorify>
numerical_col = ['Fare']
categorical_col = ['Pclass','Sex','Fam_type','Age Bin','Embarked']
num_trans = SimpleImputer(strategy = 'median')
 cat_trans = Pipeline(steps = [ 
('imputer',SimpleImputer(strategy = 'most_frequent')) , 
('onehot',OneHotEncoder())
 ])
 preprocessor = ColumnTransformer(
 transformers = [ 
('num',num_trans,numerical_col), 
('cat',cat_trans,categorical_col)
 ])
 titanic_pipeline = Pipeline(steps=[('preprocessor', preprocessor), 
('model', RandomForestClassifier(random_state=0,
max_depth = 5,
n_estimators = 500 
))
])
 titanic_pipeline.fit(X,y)
 print('Cross validation score: {:.3f}'.format(cross_val_score(titanic_pipeline, X, y, cv=10 ).mean()))
<categorify><EOS> 
predictions = titanic_pipeline.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission2.csv', index=False)
 print("Your submission was successfully saved!" )