<jupyter_start><jupyter_text>Loading Dataset<jupyter_code>train_data=pd.read_csv('train.csv')
test_data=pd.read_csv('test.csv')
full_data=pd.concat([train_data,test_data])
full_data<jupyter_output><empty_output><jupyter_text>Data Analysis<jupyter_code>full_data.info()
#Missing values
train_data['Title'] = train_data.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
full_data['Title'] = full_data.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
newtitles={
    "Capt":       "Officer",
    "Col":        "Officer",
    "Major":      "Officer",
    "Dr":         "Officer",
    "Rev":        "Officer",
    "Jonkheer":   "Royalty",
    "Lady" :      "Royalty",
    "Don":        "Royalty",
    "Sir" :       "Royalty",
    "the Countess":"Royalty",
    "Dona":       "Royalty",
    "Mr" :        "Mr",
    "Mme":        "Mrs",
    "Ms":         "Mrs",
    "Mrs" :       "Mrs",
    "Miss" :      "Miss",
    "Mlle":       "Miss",
    "Master" :    "Master",
    }
train_data['Title']=train_data.Title.map(newtitles)
full_data['Title']=full_data.Title.map(newtitles)
train_data.groupby(['Title','Sex']).Age.mean()
def newage(cols):
    title=cols[0]
    Sex=cols[1]
    Age=cols[2]
    if pd.isnull(Age):
        if title=='Master' and Sex=="male":
            return 4.57
        elif title=='Miss' and Sex=='female':
            return 21.8
        elif title=='Mr' and Sex=='male': 
            return 32.37
        elif title=='Mrs' and Sex=='female':
            return 35.72
        elif title=='Officer' and Sex=='female':
            return 49
        elif title=='Officer' and Sex=='male':
            return 46.56
        elif title=='Royalty' and Sex=='female':
            return 40.50
        else:
            return 42.33
    else:
        return Age 
train_data.Age=train_data[['Title','Sex','Age']].apply(newage, axis=1)
full_data.Age=full_data[['Title','Sex','Age']].apply(newage, axis=1)
mean=train_data['Fare'].mean()
full_data['Fare']=full_data['Fare'].fillna(mean)
train_data['Cabin']=train_data['Cabin'].fillna("unknown")
full_data['Cabin']=full_data['Cabin'].fillna("Unknown")
mode=train_data['Embarked'].mode()[0]
print(mode)
full_data['Embarked']=full_data['Embarked'].fillna(mode)
#only the null values of Survived column in testing dataset is left
full_data.isnull().sum().sum()<jupyter_output><empty_output><jupyter_text>Feature Engineering<jupyter_code>g = sns.factorplot(x="Pclass",y="Survived",data=train_data,kind="bar", palette = "muted")
g.despine(left=True)
g = g.set_ylabels("survival probability")
print(train_data.groupby(['Sex']).Survived.value_counts())
g = sns.barplot(x="Sex",y="Survived",data=train_data)
g = g.set_ylabel("Survival Probability")
full_data['Sex']=full_data['Sex'].map({'male':0,'female':1}).astype('int64')
g = sns.factorplot(x="Embarked", y="Survived",  data=train_data,
                   size=6, kind="bar", palette="muted")
g.despine(left=True)
g = g.set_ylabels("survival probability")
print(train_data.groupby(['Embarked']).Survived.value_counts())
full_data['Embarked']=full_data['Embarked'].map({'S':1,'Q':2,'C':3}).astype('int64')
g = sns.heatmap(train_data[["Survived","SibSp","Parch","Age","Fare"]].corr(),annot=True, fmt = ".2f", cmap = "coolwarm")
full_data['FamilySize']=full_data['SibSp']+full_data['Parch']+1
full_data.Cabin = full_data.Cabin.map(lambda x: x[0])
full_data
full_data=pd.get_dummies(full_data, columns=['Pclass','Embarked','Title','Cabin'], drop_first=True)
full_data=full_data.drop(['PassengerId','Name','Ticket'],axis=1)
corr=full_data.corr()
corr.Survived.sort_values(ascending=False)
cols= ['Age', 'SibSp', 'Parch', 'Fare']
full_data[cols]=full_data[cols].clip(lower=full_data[cols].quantile(0.15), upper=full_data[cols].quantile(0.85),axis=1)
from sklearn.preprocessing import StandardScaler
ss= StandardScaler()
features=['Age','Fare','SibSp']
full_data[features]=ss.fit_transform(full_data[features])
X=full_data[:891]
test=full_data[891:]
y=X['Survived']
X=X.drop(['Survived'],axis=1)
test=test.drop(['Survived'],axis=1)
print(X.shape,y.shape)<jupyter_output>(891, 23) (891,)<jupyter_text>Modelling<jupyter_code>X_train,X_valid,y_train,y_valid=train_test_split(X,y,test_size=0.2,random_state=1)
print(X_train.shape,y_train.shape)
logistic=make_pipeline(StandardScaler(),LogisticRegression())
logistic.fit(X_train,y_train)
y_pred=logistic.predict(X_valid)
print(accuracy_score(y_pred,y_valid))
forest=RandomForestClassifier(n_estimators=400,max_depth=8, max_features=4,
                                    random_state=0)
#param=[{'n_estimators':[100,200,300,400,500],'max_depth':[2,4,6,8,10],
#                     'max_features':['auto', 'sqrt', 'log2']}]
#GSRF=GridSearchCV(estimator=forest, param_grid=param, scoring='accuracy',cv=5)
#scores_rf=cross_val_score(GSRF,X,y,scoring='accuracy',cv=5)
#GSRF.best_params_
forest.fit(X_train,y_train)
forest_score = cross_val_score(forest, X_train, y_train, cv = 10, scoring = 'accuracy').mean()
print(forest_score)
svc=make_pipeline(StandardScaler(),SVC(C = 10, gamma = 'auto' ,kernel = 'rbf', random_state = 0))
# param_grid = [{'gamma': ['scale','auto'], 
#               'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
#               'C': [0.001 ,0.01 ,0.1, 1, 10, 100]}]

#grid_search = GridSearchCV(svc, param_grid=param_grid, cv = 10)
#grid_search.fit(X_train, y_train)
#grid_search.best_params_

# best params: {'C': 10, 'gamma': 'auto', 'kernel': 'rbf'}
svc.fit(X_train,y_train)
svc_score = cross_val_score(svc, X_train, y_train, cv = 10, scoring = 'accuracy').mean()
print(svc_score)
y_pred=svc.predict(X_valid)
print(accuracy_score(y_pred,y_valid))
grdb_clf = GradientBoostingClassifier(max_depth=4, max_features= 2, n_estimators= 200)
#param= [{'n_estimators':[100,200,300],  'max_depth': [2, 4, 8, 16, 20], 
#               'max_features': [2, 4, 6, 8, 10]}]

#grid_search = GridSearchCV(grdb_clf, param_grid=param, cv = 10)
#grid_search.fit(X, y)
#grid_search.best_params_

#best param : {'max_depth': 4, 'max_features': 2, 'n_estimators': 200}
grdb_clf.fit(X, y)

grdb_clf_score = cross_val_score(grdb_clf, X, y, cv = 10, scoring = 'accuracy').mean()

print("Gradient Boosting score: ", grdb_clf_score)
print(grdb_clf.score(X, y))
model=GSRF.fit(X,y)
y_test=model.predict(test)
y_test
y_test=y_test.astype('int64')
y_test=pd.DataFrame(y_test)
y_test.to_csv('t2_sub.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19336670838548184, 'fraction_numerical': 0.027065081351689613, 'mean_word_length': 5.43158953722334, 'pattern_counts': {'":': 13, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '173886', 'license': 'MIT', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2487, 'n_words': 340, 'path': 'Titanic_model/Titanic_model.ipynb', 'repo_name': 'Sagar-Rp7/Titanic_Prediction', 'size': '6392'}