text_plain_output_1.png image_output_1.png
from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import train_test_split

from sklearn.utils import shuffle

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

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

df.drop(['Pclass', 'Name', 'Ticket', 'Fare', 'Cabin', 'Embarked'], axis=1, inplace=True)

df.isna().sum()

round(df.isna().sum() / len(df.index) * 100, 2)

round(df.isna().sum() / len(df.index) * 100, 2)

categrocal_col = df.select_dtypes(exclude=np.number)
categrocal_col

labels_ = ['child', 'young', 'teenage', 'adult', 'old']
bins_ = [0, 10, 18, 28, 45, 80]

df['Age'] = pd.cut(df['Age'], bins=bins_, labels=labels_)

age = pd.get_dummies(df['Age'])

data = pd.concat([df, age], axis=1)
data

data.drop(['Age'], axis=1, inplace=True)

from sklearn.utils import shuffle
shuffle_data = shuffle(data, random_state=42)
shuffle_data

x = shuffle_data.drop('Survived', axis=1)
y = shuffle_data['Survived']

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=42)
(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

lo = LogisticRegression()
lo.fit(x_train, y_train)

y_pred = lo.predict(x_test)

metrics.accuracy_score(y_test, y_pred)