text_plain_output_1.png
from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import cross_val_score, StratifiedKFold

from sklearn.preprocessing import StandardScaler

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

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

test_data = pd.read_csv('/kaggle/input/titanic/test.csv')

df = train_data.drop(columns=['Name', 'Ticket', 'Cabin'])
dft = test_data.drop(columns=['Name', 'Ticket', 'Cabin'])

DF = pd.concat([df, dft])
survivors = DF[DF.Survived == 1]
women_survivors = DF[(DF.Survived == 1) & (DF.Sex == 'female')]
percent_women_survivors = len(women_survivors) / len(survivors)
men_survivors = DF[(DF.Survived == 1) & (DF.Sex == 'male')]
percent_men_survivors = len(men_survivors) / len(survivors)

df.isna().sum()

dft.isna().sum()

df2 = df.copy()
df2.Age = df.Age.fillna(df.Age.mean())
dft2 = dft.copy()
dft2.Age = dft.Age.fillna(dft.Age.mean())
dft2.Fare = dft.Fare.fillna(dft.Fare.mean())

df3 = df2.copy()
df3.Sex = df3.Sex.map({'male': 0, 'female': 1})
dft3 = dft2.copy()
dft3.Sex = dft3.Sex.map({'male': 0, 'female': 1})

df4 = pd.get_dummies(df3)
dft4 = pd.get_dummies(dft3)

X_train = df4.drop(columns=['PassengerId', 'Survived'])
X_test = dft4.drop(columns=['PassengerId'])
y_train = df4['Survived']

from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score, StratifiedKFold
sc = StandardScaler()
X_train[['Age', 'Fare']] = sc.fit_transform(X_train[['Age', 'Fare']])
X_test[['Age', 'Fare']] = sc.transform(X_test[['Age', 'Fare']])
skf = StratifiedKFold(n_splits=4, shuffle=True)
clf = LogisticRegression(max_iter=1000, C=0.1)
scores = cross_val_score(clf, X_train, y_train, cv=skf)
print(scores)
print(scores.mean())