<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Titanic
a Kaggle competition
Step1:  Training
Step2:  The most common embarkation port is S, so let's assume everyone got on there.
Step3:  From the Kaggle competition description, the error metric is percentage of correct predictions.
Step4:  One good way to think of logistic regression is that it takes the output of a linear regression, and maps it to a probability value between 0 and 1. The mapping is done using the logit function. Passing any value through the logit function will map it to a value between 0 and 1 by "squeezing" the extreme values. This is perfect for us, because we only care about two outcomes.
Step5:  Testing
Step6:  The most common embarkation port is S, so let's assume everyone got on there.
Step7:  We'll also need to replace a missing value in the Fare column.
Use .fillna with the median of the column in the test set to replace this.
There are no missing values in the Fare column of the training set, but test sets can sometimes be different.
Step8:  generate a submission for the competition!
First, we have to train an algorithm on the training data.
Then, we make predictions on the test set.
Finally, we'll generate a csv file with the predictions and passenger ids.
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import numpy as np
from matplotlib import pyplot
%matplotlib inline
titanic = pd.read_csv("train.csv")
titanic_test = pd.read_csv("test.csv")
titanic.shape
titanic.describe()
titanic.info()
titanic.head(3)
Explanation: Titanic
a Kaggle competition
End of explanation
titanic["Age"] = titanic["Age"].fillna(titanic["Age"].median())
titanic.loc[titanic["Sex"] == "male", "Sex"] = 0
titanic.loc[titanic["Sex"] == "female", "Sex"] = 1
titanic.head(3)
titanic["Embarked"].unique()
Explanation: Training
End of explanation
titanic["Embarked"] = titanic["Embarked"].fillna('S')
x = titanic["Embarked"]
x = pd.get_dummies(titanic["Embarked"]).astype('int')
t = pd.concat([titanic, x], axis=1)
t.head(3)
# Import the linear regression class
from sklearn.linear_model import LinearRegression
# Sklearn also has a helper that makes it easy to do cross validation
from sklearn.cross_validation import KFold
# The columns we'll use to predict the target
predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "C", "Q", "S"]
t.shape[0]
# Initialize our algorithm class
alg = LinearRegression()
nfolds = 9
# Generate cross validation folds for the titanic dataset.  It return the row indices corresponding to train and test.
# We set random_state to ensure we get the same splits every time we run this.
kf = KFold(t.shape[0], n_folds=nfolds, random_state=1)
predictions = []
for train, test in kf:
    # The predictors we're using the train the algorithm.  Note how we only take the rows in the train folds.
    train_predictors = (t[predictors].iloc[train,:])
    # The target we're using to train the algorithm.
    train_target = t["Survived"].iloc[train]
    # Training the algorithm using the predictors and target.
    alg.fit(train_predictors, train_target)
    # We can now make predictions on the test fold
    test_predictions = alg.predict(t[predictors].iloc[test,:])
    predictions.append(test_predictions)
len(predictions)
predictions[2]
# The predictions are in three separate numpy arrays.  Concatenate them into one.  
# We concatenate them on axis 0, as they only have one axis.
predictions = np.concatenate(predictions, axis=0)
# Map predictions to outcomes (only possible outcomes are 1 and 0)
predictions[predictions > .5] = 1
predictions[predictions <=.5] = 0
accuracy = sum(predictions[predictions == titanic["Survived"]]) / len(predictions)
Explanation: The most common embarkation port is S, so let's assume everyone got on there.
End of explanation
accuracy
Explanation: From the Kaggle competition description, the error metric is percentage of correct predictions.
End of explanation
from sklearn import cross_validation
from sklearn.linear_model import LogisticRegression
# Initialize our algorithm
alg = LogisticRegression(random_state=1)
# Compute the accuracy score for all the cross validation folds.  (much simpler than what we did before!)
scores = cross_validation.cross_val_score(alg, t[predictors], t["Survived"], cv=nfolds)
# Take the mean of the scores (because we have one for each fold)
print(scores.mean())
Explanation: One good way to think of logistic regression is that it takes the output of a linear regression, and maps it to a probability value between 0 and 1. The mapping is done using the logit function. Passing any value through the logit function will map it to a value between 0 and 1 by "squeezing" the extreme values. This is perfect for us, because we only care about two outcomes.
End of explanation
titanic_test["Age"] = titanic_test["Age"].fillna(titanic["Age"].median())
titanic_test.loc[titanic_test["Sex"] == "male", "Sex"] = 0
titanic_test.loc[titanic_test["Sex"] == "female", "Sex"] = 1
titanic_test.head(3)
titanic_test["Embarked"].unique()
Explanation: Testing
End of explanation
titanic_test["Embarked"] = titanic_test["Embarked"].fillna('S')
x = titanic_test["Embarked"]
x = pd.get_dummies(titanic_test["Embarked"]).astype('int')
t_test = pd.concat([titanic_test, x], axis=1)
t_test.head(3)
Explanation: The most common embarkation port is S, so let's assume everyone got on there.
End of explanation
t_test["Fare"] = t_test["Fare"].fillna(np.mean(t_test["Fare"]))
t_test.head(3)
Explanation: We'll also need to replace a missing value in the Fare column.
Use .fillna with the median of the column in the test set to replace this.
There are no missing values in the Fare column of the training set, but test sets can sometimes be different.
End of explanation
# Import the linear regression class
from sklearn.linear_model import LinearRegression
# Sklearn also has a helper that makes it easy to do cross validation
from sklearn.cross_validation import KFold
# The columns we'll use to predict the target
predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "C", "Q", "S"]
t.shape[0]
from sklearn.linear_model import LogisticRegression
# Initialize the algorithm class
alg = LogisticRegression(random_state=1)
# Train the algorithm using all the training data
alg.fit(t[predictors], t["Survived"])
# Make predictions using the test set.
predictions = alg.predict(t_test[predictors])
# Create a new dataframe with only the columns Kaggle wants from the dataset.
submission = pd.DataFrame({
        "PassengerId": titanic_test["PassengerId"],
        "Survived": predictions
    })
submission.to_csv("submission.csv", index=False)
Explanation: generate a submission for the competition!
First, we have to train an algorithm on the training data.
Then, we make predictions on the test set.
Finally, we'll generate a csv file with the predictions and passenger ids.
End of explanation
<END_TASK>