text_plain_output_1.png
from sklearn.compose import ColumnTransformer

from sklearn.ensemble import RandomForestClassifier

from sklearn.ensemble import VotingClassifier

from sklearn.impute import SimpleImputer

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import cross_val_score

from sklearn.neighbors import KNeighborsClassifier

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import OneHotEncoder

from sklearn.preprocessing import PolynomialFeatures

from sklearn.preprocessing import StandardScaler

import numpy as np

import pandas as pd

train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
x_train = pd.read_csv('/kaggle/input/titanic/train.csv')
x_test = pd.read_csv('/kaggle/input/titanic/test.csv')
y_train = np.array(x_train['Survived'].copy())
id_test = np.array(x_test['PassengerId'].copy())

labels = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
x_train = x_train[labels]
x_test = x_test[labels]

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
num_pipeline = Pipeline([('num_imputer', SimpleImputer(strategy='median')), ('std_scaler', StandardScaler())])

from sklearn.preprocessing import OneHotEncoder
cat_pipeline = Pipeline([('cat_imputer', SimpleImputer(strategy='most_frequent')), ('encode', OneHotEncoder())])

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import PolynomialFeatures
num_labels = ['Pclass', 'Age', 'SibSp', 'Parch', 'Fare']
cat_labels = ['Sex', 'Embarked']
full_pipeline = ColumnTransformer([('num_trans', num_pipeline, num_labels), ('cat_trans', cat_pipeline, cat_labels)])

x_train_prepared = full_pipeline.fit_transform(x_train)

def model_fit_and_print_acc(model):
    model.fit(x_train_prepared, y_train)
    pred = model.predict(x_train_prepared)
    cv_scores = cross_val_score(model, x_train_prepared, y_train, cv=3, scoring='accuracy')

poly_log_reg = Pipeline([('poly_features', PolynomialFeatures(degree=3, include_bias=False)), ('log_reg', LogisticRegression(penalty='elasticnet', l1_ratio=0.7, max_iter=100000, solver='saga'))])

from sklearn.ensemble import RandomForestClassifier
forest_clf = RandomForestClassifier()

from sklearn.model_selection import GridSearchCV
param_grid = [{'n_estimators': [150, 200, 250], 'max_features': [5, 10], 'max_depth': [5, 10, 20], 'min_samples_leaf': [5, 10, 20], 'min_samples_split': [5, 10, 20]}]
forest_clf = RandomForestClassifier()
grid_search = GridSearchCV(forest_clf, param_grid, cv=3, scoring='accuracy', return_train_score=True)
grid_search.fit(x_train_prepared, y_train)
best_search_rf = grid_search.best_estimator_
grid_search.best_params_

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(20)

from sklearn.ensemble import VotingClassifier
voting = VotingClassifier(estimators=[('poly_logres', poly_log_reg), ('grid_randomforest', best_search_rf), ('knn', knn)], voting='hard')
voting.fit(x_train_prepared, y_train)
pred = voting.predict(x_train_prepared)
print(accuracy_score(pred, y_train))
cv_scores = cross_val_score(voting, x_train_prepared, y_train, cv=3, scoring='accuracy')
cv_scores.mean()