1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| """ Created on Sun Oct 30 15:28:22 2016
@author: thinkwee """
import csv as csv import numpy as np from glue import qglue
test_file=(open(r'文件目录略', 'r')) test_file_object = csv.reader(open(r'文件目录略', 'r')) testheader = next(test_file_object) predictions_file = open(r"文件目录略", "w") predictions_file_object = csv.writer(predictions_file) p = csv.writer(predictions_file) p.writerow(["PassengerId", "Survived"]) csv_file_object = csv.reader(open(r'文件目录略', 'r')) trainheader = next(csv_file_object) data=[] for row in csv_file_object: data.append(row) print(type(data)) data = np.array(data)
number_passengers = np.size(data[0::,1].astype(np.float)) number_survived = np.sum(data[0::,1].astype(np.float)) proportion_survivors = number_survived / number_passengers
women_only_stats = data[0::,4] == "female" men_only_stats = data[0::,4] != "female"
women_onboard = data[women_only_stats,1].astype(np.float) men_onboard = data[men_only_stats,1].astype(np.float)
proportion_women_survived = \ np.sum(women_onboard) / np.size(women_onboard) proportion_men_survived = \ np.sum(men_onboard) / np.size(men_onboard)
print ('Proportion of women who survived is %s' % proportion_women_survived) print ('Proportion of men who survived is %s' % proportion_men_survived)
fare_ceiling = 40
data[ data[0::,9].astype(np.float) >= fare_ceiling, 9 ] = fare_ceiling - 1.0
fare_bracket_size = 10 number_of_price_brackets = fare_ceiling // fare_bracket_size
number_of_classes = len(np.unique(data[0::,2]))
number_of_age_brackets=8
survival_table = np.zeros((2, number_of_classes, number_of_price_brackets, number_of_age_brackets))
for i in range(number_of_classes): for j in range(number_of_price_brackets): for k in range(number_of_age_brackets): women_only_stats_plus = data[ (data[0::,4] == "female") &(data[0::,2].astype(np.float) == i+1) &(data[0:,9].astype(np.float) >= j*fare_bracket_size) &(data[0:,9].astype(np.float) < (j+1)*fare_bracket_size) &(data[0:,5].astype(np.float)>=k*10) &(data[0:,5].astype(np.float)<(k+1)*10)
, 1]
men_only_stats_plus = data[ (data[0::,4] != "female") &(data[0::,2].astype(np.float) == i+1) &(data[0:,9].astype(np.float) >= j*fare_bracket_size) &(data[0:,9].astype(np.float) < (j+1)*fare_bracket_size) &(data[0:,5].astype(np.float)>=k*10) &(data[0:,5].astype(np.float)<(k+1)*10) , 1]
survival_table[0,i,j,k] = np.mean(women_only_stats_plus.astype(np.float)) survival_table[1,i,j,k] = np.mean(men_only_stats_plus.astype(np.float))
survival_table[ survival_table != survival_table ] = 0.
for row in test_file_object: for j in range(number_of_price_brackets): try: row[8] = float(row[8]) except: bin_fare = 3 - float(row[1]) break if row[8] > fare_ceiling: bin_fare = number_of_price_brackets-1 break if row[8] >= j * fare_bracket_size\ and row[8] < \ (j+1) * fare_bracket_size: bin_fare = j break
for j in range(number_of_age_brackets):
try:
row[4] = float(row[4]) except: bin_age = -1 break
if row[4] >= j * 10\ and row[4] < \ (j+1) * 10: bin_age = j break
if row[3] == 'female': p.writerow([row[0], "%f %%" % \ (survival_table[0, int(row[1])-1, bin_fare,bin_age]*100)]) else: p.writerow([row[0], "%f %%" % \ (survival_table[1, int(row[1])-1, bin_fare,bin_age]*100)])
test_file.close() predictions_file.close()
|