习题1-3 求怀孕周期平均值

__author__ = 'dell'

import survey

table = survey.Pregnancies()
table.ReadRecords()
print "Number of pregnancies ", len(table.records)

运行结果:
D:\Python27\python.exe F:/sync_code/python/first.py
Number of pregnancies  13593
Process finished with exit code 0

 

计算活婴的数量:
__author__ = 'dell'

import survey

table = survey.Pregnancies()
table.ReadRecords()
print "Number of pregnancies ", len(table.records)

livebirth = 0
for record in table.records:
    if record.outcome == 1:
        livebirth += 1
print 'live birth num is :', livebirth

运行结果:
D:\Python27\python.exe F:/sync_code/python/first.py
Number of pregnancies  13593
live birth num is : 9148



核对网址:
http://www.icpsr.umich.edu/nsfg6/Controller?displayPage=labelDetails&fileCode=PREG&section=&subSec=8016&srtLabel=611932
活婴分为两组,第一胎和其他。计算他们的数量:
__author__ = 'dell'

import survey

table = survey.Pregnancies()
table.ReadRecords()
print "Number of pregnancies ", len(table.records)

firsts = survey.Pregnancies()
others = survey.Pregnancies()

for p in table.records:
    if p.outcome != 1:
        continue
    if p.birthord == 1:
        firsts.AddRecord(p)
    else:
        others.AddRecord(p)

print 'Num of the first babies :', len(firsts)
print 'Num of others :', len(others )

运行结果:
D:\Python27\python.exe F:/sync_code/python/first.py
Number of pregnancies  13593
Num of the first babies : 4413
Num of others : 4735


核对网址:
http://www.icpsr.umich.edu/nsfg6/Controller?displayPage=labelDetails&fileCode=PREG&section=&subSec=8016&srtLabel=611933
第一胎婴儿和其他婴儿的平均怀孕周期(单位 周)

__author__ = 'dell'

import survey

table = survey.Pregnancies()
table.ReadRecords()
print "Number of pregnancies ", len(table.records)

firsts = survey.Pregnancies()
others = survey.Pregnancies()

for p in table.records:
    if p.outcome != 1:
        continue
    if p.birthord == 1:
        firsts.AddRecord(p)
    else:
        others.AddRecord(p)

print 'Num of the first babies :', len(firsts)
print 'Num of others :', len(others )

a = [r.prglength for r in firsts.records]
b = [r.prglength for r in others.records]


def Mean(v):
    return float(sum(v)) / len(v)

print 'the mean of prglength for first babies :', Mean(a)
print 'the mean of prglength for other babies :', Mean(b)


First babies 38.6009517335
Others 38.5229144667

the mean of prglength for first babies : 38.6009517335
the mean of prglength for other babies : 38.5229144667
Difference in days 0.546260867443 day
Difference in hours  13.1102608186

第一胎的平均时间 比 其他时间 大 13 小时

 

posted on 2013-07-31 18:34  雨渐渐  阅读(333)  评论(0编辑  收藏  举报

导航