<s> import os <EOL> import csv as csv <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> path = os . getcwd ( ) <EOL> csv_file_object = csv . reader ( open ( path + '<STR_LIT>' , '<STR_LIT:rb>' ) ) <EOL> header = csv_file_object . next ( ) <EOL> data = [ ] <EOL> for row in csv_file_object : <EOL> data . append ( row ) <EOL> data = np . array ( data ) <EOL> number_passengers = np . size ( data [ <NUM_LIT:0> : : , <NUM_LIT:0> ] . astype ( np . float ) ) <EOL> number_survived = np . sum ( data [ <NUM_LIT:0> : : , <NUM_LIT:0> ] . astype ( np . float ) ) <EOL> prop_surv = number_survived / number_passengers <EOL> print '<STR_LIT>' + str ( number_passengers ) <EOL> print '<STR_LIT>' + str ( number_survived ) <EOL> print '<STR_LIT>' + str ( prop_surv ) <EOL> print <EOL> women_only_stats = data [ <NUM_LIT:0> : : , <NUM_LIT:3> ] == '<STR_LIT>' <EOL> men_only_stats = data [ <NUM_LIT:0> : : , <NUM_LIT:3> ] != '<STR_LIT>' <EOL> women_onboard = data [ women_only_stats , <NUM_LIT:0> ] . astype ( np . float ) <EOL> men_onboard = data [ men_only_stats , <NUM_LIT:0> ] . astype ( np . float ) <EOL> print '<STR_LIT>' + str ( len ( women_only_stats ) ) <EOL> proportion_women_survived = np . sum ( women_onboard ) / np . size ( women_onboard ) <EOL> proportion_men_survived = np . sum ( men_onboard ) / np . size ( men_onboard ) <EOL> print '<STR_LIT>' % proportion_women_survived <EOL> print '<STR_LIT>' % proportion_men_survived <EOL> test_file_object = csv . reader ( open ( path + '<STR_LIT>' , '<STR_LIT:rb>' ) ) <EOL> header = test_file_object . next ( ) <EOL> open_file_object = csv . writer ( open ( path + '<STR_LIT>' , '<STR_LIT:wb>' ) ) <EOL> for row in test_file_object : <EOL> if row [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> row . insert ( <NUM_LIT:0> , '<STR_LIT:1>' ) <EOL> open_file_object . writerow ( row ) <EOL> else : <EOL> row . insert ( <NUM_LIT:0> , '<STR_LIT:0>' ) <EOL> open_file_object . writerow ( row ) <EOL> fare_ceiling = <NUM_LIT> <EOL> data [ data [ <NUM_LIT:0> : : , <NUM_LIT:8> ] . astype ( np . float ) >= fare_ceiling , <NUM_LIT:8> ] = fare_ceiling - <NUM_LIT:1.0> <EOL> fare_bracket_size = <NUM_LIT:10> <EOL> num_price_brackets = fare_ceiling / fare_bracket_size <EOL> num_classes = <NUM_LIT:3> <EOL> survival_table = np . zeros ( ( <NUM_LIT:2> , num_classes , num_price_brackets ) ) <EOL> for i in xrange ( num_classes ) : <EOL> for j in xrange ( num_price_brackets ) : <EOL> women_only_stats = data [ ( data [ <NUM_LIT:0> : : , <NUM_LIT:3> ] == '<STR_LIT>' ) & ( data [ <NUM_LIT:0> : : , <NUM_LIT:1> ] . astype ( np . float ) == i + <NUM_LIT:1> ) & ( data [ <NUM_LIT:0> : , <NUM_LIT:8> ] . astype ( np . float ) >= j * fare_bracket_size ) & ( data [ <NUM_LIT:0> : , <NUM_LIT:8> ] . astype ( np . float ) < ( j + <NUM_LIT:1> ) * fare_bracket_size ) , <NUM_LIT:0> ] <EOL> men_only_stats = data [ ( data [ <NUM_LIT:0> : : , <NUM_LIT:3> ] != '<STR_LIT>' ) & ( data [ <NUM_LIT:0> : : , <NUM_LIT:1> ] . astype ( np . float ) == i + <NUM_LIT:1> ) & ( data [ <NUM_LIT:0> : , <NUM_LIT:8> ] . astype ( np . float ) >= j * fare_bracket_size ) & ( data [ <NUM_LIT:0> : , <NUM_LIT:8> ] . astype ( np . float ) < ( j + <NUM_LIT:1> ) * fare_bracket_size ) , <NUM_LIT:0> ] <EOL> survival_table [ <NUM_LIT:0> , i , j ] = np . mean ( women_only_stats . astype ( np . float ) ) <EOL> survival_table [ <NUM_LIT:1> , i , j ] = np . mean ( men_only_stats . astype ( np . float ) ) <EOL> survival_table [ survival_table != survival_table ] = <NUM_LIT:0> <EOL> survival_table [ survival_table < <NUM_LIT:0.5> ] = <NUM_LIT:0> <EOL> survival_table [ survival_table >= <NUM_LIT:0.5> ] = <NUM_LIT:1> <EOL> print survival_table <EOL> test_file_object = csv . reader ( open ( path + '<STR_LIT>' , '<STR_LIT:rb>' ) ) <EOL> new_open_file_object = csv . writer ( open ( path + '<STR_LIT>' , '<STR_LIT:wb>' ) ) <EOL> header = test_file_object . next ( ) <EOL> for row in test_file_object : <EOL> for j in xrange ( num_price_brackets ) : <EOL> try : <EOL> row [ <NUM_LIT:7> ] = float ( row ( <NUM_LIT:7> ) ) <EOL> except : <EOL> bin_fare = <NUM_LIT:3> - float ( row [ <NUM_LIT:0> ] ) <EOL> break <EOL> if row [ <NUM_LIT:7> ] > fare_ceiling : <EOL> bin_fare = num_price_brackets - <NUM_LIT:1> <EOL> break <EOL> if row [ <NUM_LIT:7> ] >= j * fare_bracket_size and row [ <NUM_LIT:7> ] < ( j + <NUM_LIT:1> ) * fare_bracket_size : <EOL> bin_fare = j <EOL> break <EOL> if row [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> row . insert ( <NUM_LIT:0> , int ( survival_table [ <NUM_LIT:0> , float ( row [ <NUM_LIT:0> ] ) - <NUM_LIT:1> , bin_fare ] ) ) <EOL> new_open_file_object . writerow ( row ) <EOL> else : <EOL> row . insert ( <NUM_LIT:0> , int ( survival_table [ <NUM_LIT:1> , float ( row [ <NUM_LIT:0> ] ) - <NUM_LIT:1> , bin_fare ] ) ) <EOL> new_open_file_object . writerow ( row ) </s>
