Kaggle competition: Titanic - Machine Learning from Disaster
print(sns.__version__)
<load_from_csv>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import sklearn.metrics as metrics
import eli5
from eli5.sklearn import PermutationImportance
path = '.. /input/tabular-playground-series-jan-2021/'
train = pd.read_csv(path + 'train.csv')
 test = pd.read_csv(path + 'test.csv')
 sample = pd.read_csv(path + 'sample_submission.csv' )<count_missing_values>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
print('number of null columns in train set :- ',np.sum(train.isnull().sum() > 0))
print('number of null columns in test set :-',np.sum(test.isnull().sum() > 0))<define_variables>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
features = [f'cont{x}'for x in range(1,15)]
target = ['target']<concatenate>
train_data.drop(['Survived'], axis=1, inplace = True )
all_data = train[features+['id']].append(test )<filter>
train1=train_data
test1=test_data
print('Ready to concatinate!' )
lesser,greater = train[train['target'] <= train['target'].median() ],train[train['target'] >= train['target'].median() ]<filter>
df = pd.concat([train1, test1], axis=0,sort=False)
 df.shape
train = train[train['target'] > 5]<compute_test_metric>
df.isnull().sum()
def rmse_score(yreal, yhat):
return sqrt(mean_squared_error(yreal, yhat))<import_modules>
df.drop(['PassengerId','Name','Ticket','Cabin'], axis=1, inplace=True)
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import lightgbm as lgb
import optuna
from math import sqrt<prepare_x_and_y>
df.fillna(df.median() , inplace=True )
train_ds = train.loc[:,features]
y_train = train.loc[:,target]
val_ds = test.loc[:,features]<normalization>
df.isnull().sum()
sc = MinMaxScaler()
train_ds = sc.fit_transform(train_ds)
 val_ds = sc.transform(val_ds )<split>
df['Embarked'].fillna(df['Embarked'].mode() [0], inplace=True )
X, X_val, y, y_val = train_test_split(train_ds, y_train, test_size=0.35,random_state=43)
 print('Train', X.shape, y.shape, 'Test', X_val.shape, y_val.shape )<create_dataframe>
df.isnull().sum()
lgb_train = lgb.Dataset(X,y)
 lgb_valid = lgb.Dataset(X_val,y_val,reference=lgb_train )<choose_model_class>
df = pd.get_dummies(df, columns=['Pclass', 'Sex', 'SibSp', 'Parch','Embarked'])
def objective(trial):
params = {
'num_leaves': trial.suggest_int('num_leaves', 32, 512),
'boosting_type': 'gbdt',
'max_bin': trial.suggest_int('max_bin', 700, 900),
'objective': 'regression',
'metric': 'RMSE',
'learning_rate': trial.suggest_float('learning_rate',0.0155,0.05),
'random_state' : 17,
'max_depth': trial.suggest_int('max_depth', 4, 16),
'min_child_weight': trial.suggest_int('min_child_weight', 1, 16),
'feature_fraction': trial.suggest_uniform('feature_fraction', 0.4, 1.0),
'bagging_fraction': trial.suggest_uniform('bagging_fraction', 0.4, 1.0),
'bagging_freq': trial.suggest_int('bagging_freq', 1, 8),
'min_child_samples': trial.suggest_int('min_child_samples', 4, 80),
'lambda_l1': trial.suggest_loguniform('lambda_l1', 1e-8, 1.0),
'lambda_l2': trial.suggest_loguniform('lambda_l2', 1e-8, 1.0),
'early_stopping_rounds': 10
}
model = lgb.train(params, lgb_train, valid_sets=[lgb_valid], verbose_eval=100, num_boost_round=800)
 y_pred = model.predict(X_val)
 score = rmse_score(y_val, y_pred)
 print(f"RMSE = {score}")
 return score<find_best_params>
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import sklearn.metrics as metrics
study = optuna.create_study(direction = 'minimize')
 study.optimize(objective,n_trials = 50 )<find_best_params>
df_train = preprocessing.StandardScaler().fit(df_train ).transform(df_train)
 df_train[0:5]
print(f"Trials : {len(study.trials)}")
 print(f"Best Trial : {study.best_trial}")
 best = study.best_trial
for i, j in best.params.items() :
print(f"{i} : {j}")
 print(f"{best.value}" )<init_hyperparams>
x_train,x_test,y_train,y_test = train_test_split(df_train,target,test_size=0.33,random_state=0 )
best_params = best.params
;<train_model>
parameter_grid = {'C': [0.0001, 0.001, 0.01, 0.1], 'gamma' : [00.1, 0.10, 1, 10]}
grid_search = GridSearchCV(SVC(kernel='rbf'), cv=10, param_grid = parameter_grid)
 grid_search.fit(x_train, y_train)
 print("Best Score: {}".format(grid_search.best_score_))
print("Best params: {}".format(grid_search.best_params_))
model = lgb.train(best_params, lgb_train, valid_sets=[lgb_train,lgb_valid],verbose_eval=50,num_boost_round=700 )<predict_on_test>
SVC_model = SVC(C=0.1, gamma=0.1, kernel='rbf')
 SVC_model.fit(x_train, y_train)
preds = model.predict(X_val,num_iteration=model.best_iteration )<compute_test_metric>
perm = PermutationImportance(SVC_model ).fit(x_test, y_test)
 eli5.show_weights(perm, feature_names = df.columns.tolist() )
print('LGBM: RMSE %.6f' %(rmse_score(y_val, preds)) )<predict_on_test>
df_test = preprocessing.StandardScaler().fit(df_test ).transform(df_test)
 df_test[0:5]
train_preds = model.predict(train_ds )<compute_test_metric>
SVC_model.fit(df_train,target)
 prediction=SVC_model.predict(df_test )
print('LGBM: Train RMSE %.6f' %(rmse_score(y_train, train_preds)) )<prepare_output>
output = pd.DataFrame({'PassengerId': PassengerId, 'Survived': prediction})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )