<jupyter_start><jupyter_text><jupyter_code># upload csv to colab
from google.colab import files
uploaded = files.upload()<jupyter_output><empty_output><jupyter_text>Kaggle [Titanic Challenge](https://www.kaggle.com/c/titanic/) using Logistic Regression1. Importing neccesary packages2. Read csv file and check if there is any NaN values3. Impute NaN values4. Scale the data5. Create Logistic Regression Model and fit data to the model6. Score the model using Cross Validation7. Predict the data and save it to Comma-Seperated Values Format<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import MinMaxScaler
df = pd.read_csv('train.csv')
test_df  = pd.read_csv('test.csv')
df.head()
df['Embarked'],label = pd.factorize(df['Embarked'])
test_df['Embarked'], label1 = pd.factorize(test_df['Embarked'])
PassengerId = test_df['PassengerId']
test_df.isnull().any()
train = pd.get_dummies(df[['Sex','Age','Pclass','Embarked']])
test_df = pd.get_dummies(test_df[['Sex','Age','Pclass','Embarked']])
y = df['Survived']
X_train, X_test, y_train, y_test = train_test_split(train,y, test_size=0.2, random_state=42)
test_df
# train.isnull().any()
cols_with_missing = [col for col in X_train.columns
                     if X_train[col].isnull().any()]
X_train_plus = X_train.copy()
X_test_plus = X_test.copy()

# Make new columns indicating what will be imputed
for col in cols_with_missing:
    X_train_plus[col + '_was_missing'] = X_train_plus[col].isnull()
    X_test_plus[col + '_was_missing'] = X_test_plus[col].isnull()

# Imputation
my_imputer = SimpleImputer()
imputed_X_train_plus = pd.DataFrame(my_imputer.fit_transform(X_train_plus))
imputed_X_test_plus = pd.DataFrame(my_imputer.transform(X_test_plus))

# Imputation removed column names; put them back
imputed_X_train_plus.columns = X_train_plus.columns
imputed_X_test_plus.columns = X_test_plus.columns
print(imputed_X_train_plus)
imputed_X_train_plus = np.asarray(imputed_X_train_plus)
imputed_X_test_plus = np.asarray(imputed_X_test_plus)
imputed_X_train_plus
cols_with_missing = [col for col in test_df.columns
                     if test_df[col].isnull().any()]


X_test_plus1 = test_df.copy()
X_test_plus1
# # Make new columns indicating what will be imputed

X_test_plus1['Age' + '_was_missing'] = X_test_plus1['Age'].isnull()

# # Imputation
my_imputer1 = SimpleImputer()
imputed_X_test_submit_plus = pd.DataFrame(my_imputer1.fit_transform(X_test_plus1))
imputed_X_test_submit_plus
# # Imputation removed column names; put them back
imputed_X_test_submit_plus.columns = X_test_plus1.columns
imputed_X_test_submit_plus
imputed_X_test_submit_plus = np.asarray(imputed_X_test_submit_plus)
min_max_scaler = MinMaxScaler()
imputed_X_train_plus = min_max_scaler.fit_transform(imputed_X_train_plus)
imputed_X_test_plus = min_max_scaler.fit_transform(imputed_X_test_plus)
imputed_X_test_submit_plus = min_max_scaler.fit_transform(imputed_X_test_submit_plus)
lr = LogisticRegression(C=1)
lr.fit(imputed_X_train_plus, y_train)
lr.score(imputed_X_test_plus, y_test)
X = np.concatenate((imputed_X_train_plus, imputed_X_test_plus))
y = np.concatenate((y_train, y_test))
scores = cross_val_score(lr, X, y, cv=5)
print(scores.mean())

imputed_X_test_submit_plus
pred = lr.predict(imputed_X_test_submit_plus)
output = pd.DataFrame({'PassengerId': PassengerId, 'Survived': pred})
output.to_csv('my_submission.csv', index=False)
print("Your submission was successfully saved!")<jupyter_output>Your submission was successfully saved!
{'fraction_non_alphanumeric': 0.10356949702541915, 'fraction_numerical': 0.005949161709031909, 'mean_word_length': 7.349887133182844, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '474929', 'license': 'MIT', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1357, 'n_words': 257, 'path': 'PredictPassangerSurviveTitanicWithLogisticRegression.ipynb', 'repo_name': 'LeeSinLiang/Kaggle-Titanic-Challenge-using-Logistic-Regression-77.90-', 'size': '3698'}