📚 The CoCalc Library - books, templates and other resources
License: OTHER
""" This simple code is desinged to teach a basic user to read in the files in python, simply find what proportion of males and females survived and make a predictive model based on this1Author : AstroDave2Date : 18 September 20123Revised: 28 March 201445"""678import csv as csv9import numpy as np1011csv_file_object = csv.reader(open('train.csv', 'rb')) # Load in the csv file12header = csv_file_object.next() # Skip the fist line as it is a header13data=[] # Create a variable to hold the data1415for row in csv_file_object: # Skip through each row in the csv file,16data.append(row[0:]) # adding each row to the data variable17data = np.array(data) # Then convert from a list to an array.1819# Now I have an array of 12 columns and 891 rows20# I can access any element I want, so the entire first column would21# be data[0::,0].astype(np.float) -- This means all of the rows (from start to end), in column 022# I have to add the .astype() command, because23# when appending the rows, python thought it was a string - so needed to convert2425# Set some variables26number_passengers = np.size(data[0::,1].astype(np.float))27number_survived = np.sum(data[0::,1].astype(np.float))28proportion_survivors = number_survived / number_passengers2930# I can now find the stats of all the women on board,31# by making an array that lists True/False whether each row is female32women_only_stats = data[0::,4] == "female" # This finds where all the women are33men_only_stats = data[0::,4] != "female" # This finds where all the men are (note != means 'not equal')3435# I can now filter the whole data, to find statistics for just women, by just placing36# women_only_stats as a "mask" on my full data -- Use it in place of the '0::' part of the array index.37# You can test it by placing it there, and requesting column index [4], and the output should all read 'female'38# e.g. try typing this: data[women_only_stats,4]39women_onboard = data[women_only_stats,1].astype(np.float)40men_onboard = data[men_only_stats,1].astype(np.float)4142# and derive some statistics about them43proportion_women_survived = np.sum(women_onboard) / np.size(women_onboard)44proportion_men_survived = np.sum(men_onboard) / np.size(men_onboard)4546print 'Proportion of women who survived is %s' % proportion_women_survived47print 'Proportion of men who survived is %s' % proportion_men_survived4849# Now that I have my indicator that women were much more likely to survive,50# I am done with the training set.51# Now I will read in the test file and write out my simplistic prediction:52# if female, then model that she survived (1)53# if male, then model that he did not survive (0)5455# First, read in test.csv56test_file = open('test.csv', 'rb')57test_file_object = csv.reader(test_file)58header = test_file_object.next()5960# Also open the a new file so I can write to it. Call it something descriptive61# Finally, loop through each row in the train file, and look in column index [3] (which is 'Sex')62# Write out the PassengerId, and my prediction.6364predictions_file = open("gendermodel.csv", "wb")65predictions_file_object = csv.writer(predictions_file)66predictions_file_object.writerow(["PassengerId", "Survived"]) # write the column headers67for row in test_file_object: # For each row in test file,68if row[3] == 'female': # is it a female, if yes then69predictions_file_object.writerow([row[0], "1"]) # write the PassengerId, and predict 170else: # or else if male,71predictions_file_object.writerow([row[0], "0"]) # write the PassengerId, and predict 0.72test_file.close() # Close out the files.73predictions_file.close()74757677