text_html_output_1.png
from sklearn.compose import ColumnTransformer

from sklearn.ensemble import RandomForestClassifier

from sklearn.impute import SimpleImputer

from sklearn.linear_model import SGDClassifier

from sklearn.metrics import accuracy_score

from sklearn.model_selection import GridSearchCV,train_test_split

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import StandardScaler,OneHotEncoder

import pandas as pd

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
ss = StandardScaler()
OneHot = OneHotEncoder()
Imputer = SimpleImputer()

train_data = pd.read_csv('../input/titanic/train.csv')
train_data.keys()

drop_train_data = train_data.drop(['Name', 'PassengerId', 'Cabin', 'Ticket'], axis=1).dropna(axis=0)
train_data['Family'] = train_data['SibSp'] + train_data['Parch']
train_data.corr()

y = drop_train_data.pop('Survived')

OneHot.fit_transform(drop_train_data['Embarked'])

num_pipeline = Pipeline([('imputer', Imputer), ('scaler', ss)])

num_attrib = ['Fare', 'Age']
txt_attrib = ['Sex', 'Embarked']
full_pipeline = ColumnTransformer([('num', num_pipeline, num_attrib), ('txt_trans', OneHot, txt_attrib)])
data_pred = full_pipeline.fit_transform(drop_train_data)
data = pd.DataFrame(data_pred)

from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.ensemble import RandomForestClassifier
sgd = SGDClassifier()
RandForest = RandomForestClassifier()

x_train, x_test, y_train, y_test = train_test_split(data_pred, y, test_size=0.2)
param_rand = {'n_estimators': [5, 10, 20], 'max_depth': [1, 5, 8, 10]}
grid_search_rand = GridSearchCV(RandForest, param_rand, n_jobs=-1)
grid_search_rand.fit(x_train, y_train)
best_param_tree = grid_search_rand.best_estimator_
best_param_tree

final_rand = grid_search_rand.best_estimator_
final_rand.fit(x_train, y_train)
y_pred_rand = final_rand.predict(x_test)
accuracy_score(y_test, y_pred_rand)