titanic survival 2

#########hte idea is to create an table which contains just 1's and 0's.The array will be a surbibal reference table.whereby you read in the 

##########tet data,find out passenger attributes,look them opn in the survival table,and determine if they should be predicted to survive or not.

######In hte case of a model that use gender,calss,and ticktet price,you will need an array of 2X3X4([female/male],[1st/2nd/3rd class],[4bins 

############of bprices]).The script will systematically will loop through each combination and use the "where" function  in python to search 

######passengers that fit that combination of variables.Just like befor,you can ask what indices in your data equals female,1st class,and paid

#########more than $30.For the sake of binning let's say everything equal to and abouve 40 "equals" 39 so it falls in this bin.So then you can 

######set the bins:


#######so we add  a ceiling 

fare_ceiling=40

######then modify the data in the fare column to=39,if it is greater or equal to the ciling 


data[data[0::,9].astype(np.float)>=fare_ceiling,9]=fare_ceiling-1.0


####I know there were 1st,2nd adn 3rd classes on board 

number_of_classes=3


#####but it is better practice to calculate this from the data directly

####take the length of an array of unique valuese in column index2

number_of_classes=len(np.unique(data[0::,2]))


######initialize the survival table with all zeros

survival_table=np.zeros((2,number_of_classes,number_of_price_brackets))

######now that these are set up,you can loop throuhg each variable and find all those passengers that agree with the statements:

for i in xrange(number_of_classes):   ##########loop through each class 

    for j in xrange(number_of_price_brackets):   ########loop through each price bin


        women_only_stats=data[                                                \######which element

                                                    (data[0::,4]=="femalse")        \######is a female

                                                 &(data[0::,2].astype(np.float) \######and wa ith class

                                                          ==i+1)\

                                                 &(data[0:,9].astype(np.float)) \######## was greater

                                                        >=j*fare_bracket_size     \#######than this bin

                                                 &(data[0:,9].astype(np.float) \#######and less than 

                                                        <=(j+1)*fare_bracket_size)  \#####the next bin 

                                                        ,1]                                              \#####in the 2nde col



men_only_stats=data[(data[0::,4]!="female")     \#####is a male

                                       &(data[0::,2].astype(np.float))   \#########and was ith class 

                                           ==i+1

                                        &(data[0:,9].astype(np.float)    \#############was greater

                                         >=j*fare_bracket_size)               \############than this bin

                                      & (data[0:,9].astype(np.float)           \############and less than 

                                         <(j+1)*fare_bracket_size)          \#########the next bin

                                         ,1]




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值