Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Mastery Level Predict the survival of Titanic passengers from the file ' ~ / nclab - data - read / titanic - test.csv ' based
Mastery Level
Predict the survival of Titanic passengers from the file ~nclabdatareadtitanictest.csv based solely on their fare!
The program below reads the training data file and fits the model. It's up to you to make the prediction. # Import Pandas and Statsmodels:
import pandas as pd statsmodels.api as sm
# Read CSV data file:
df pdreadcsv~nclabdatareadtitaniccsv
# Define the training data X and Y:
X dfFare
Y dfSurvived
# Add constant to X and store the result in a new array Xc:
Xc smaddconstantX
# Fit the logistic model with Statsmodels:
model smLogitY Xcfit
# Read the testing data file into a new DataFrame dftest:
dftest pdreadcsv~nclabdatareadtitanictest.csv
# Define the testing dataset F column 'Fare' from dftest:
F dftestFare
# Add constant to F and store the result in a new array Fc:
Fc smaddconstantF
# Make the prediction using Fc and store the result in Prob:
Prob model.predictFc
# Create a list 'Surv' of PassengerIds of all passengers whose
# probability of survival is at least :
Surv listdftestPassengerIdlocProb
# Sort the list Surv:
Surv.sort
# Main program do not change:
printPassengerIds of predicted survivors:
strSurv
Use the comments in the code as a guide. The main program will then print the PassengerIds of the predicted survivors.
Note: As we already stated in considering the fare as the only explanatory variable is a strong simplification.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started