Answered step by step
Verified Expert Solution
Link Copied!

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 '~/nclab-data-read/titanic-test.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 = pd.read_csv('~/nclab-data-read/titanic.csv')
# Define the training data X and Y:
X = df['Fare']
Y = df['Survived']
# Add constant to X and store the result in a new array Xc:
Xc = sm.add_constant(X)
# Fit the logistic model with Statsmodels:
model = sm.Logit(Y, Xc).fit()
# Read the testing data file into a new DataFrame df_test:
df_test = pd.read_csv('~/nclab-data-read/titanic-test.csv')
# Define the testing dataset F (column 'Fare' from df_test):
F = df_test['Fare']
# Add constant to F and store the result in a new array Fc:
Fc = sm.add_constant(F)
# Make the prediction using Fc and store the result in Prob:
Prob = model.predict(Fc)
# Create a list 'Surv' of PassengerIds of all passengers whose
# probability of survival is at least 0.5:
Surv = list(df_test['PassengerId'].loc[Prob]>=0.5)
# Sort the list Surv:
Surv.sort()
# Main program (do not change):
print("PassengerIds of predicted survivors:
"+ str(Surv))
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 5.7, 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 19c DBA By Examples Installation And Administration

Authors: Ravinder Gupta

1st Edition

B09FC7TQJ6, 979-8469226970

More Books

Students also viewed these Databases questions

Question

3. Identify cultural universals in nonverbal communication.

Answered: 1 week ago