资源简介

《贝叶斯思维:统计建模的Python学习法》(Think Bayes)的高清电子书和代码

资源截图

代码片段和文件信息

“““This file contains code for use with “Think Stats“
by Allen B. Downey available from greenteapress.com

Copyright 2010 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
“““

import math
import sys
import survey
import thinkstats


class Respondents(survey.Table):
    “““Represents the respondent table.“““

    def ReadRecords(self data_dir=‘.‘ n=None):
        filename = self.GetFilename()
        self.ReadFile(data_dir
                      filename
                      self.GetFields() 
                      survey.Respondent
                      n)
        self.Recode()

    def GetFilename(self):
        “““Get the name of the data file.

        This function can be overridden by child classes.

        The BRFSS data is available from thinkstats.com/CDBRFS08.ASC.gz

        “““
        return ‘CDBRFS08.ASC.gz‘

    def GetFields(self):
        “““Returns a tuple specifying the fields to extract.
        
        BRFSS codebook 
        http://www.cdc.gov/brfss/technical_infodata/surveydata/2008.htm

        The elements of the tuple are field start end case.

                field is the name of the variable
                start and end are the indices as specified in the NSFG docs
                case is a callable that converts the result to int float etc.
        “““
        return [
            (‘age‘ 101 102 int)
            (‘weight2‘ 119 122 int)
            (‘wtyrago‘ 127 130 int)
            (‘wtkg2‘ 1254 1258 int)
            (‘htm3‘ 1251 1253 int)
            (‘sex‘ 143 143 int)
            ]

    def Recode(self):
        “““Recode variables that need cleaning.“““

        def CleanWeight(weight):
            if weight in [7777 9999]:
                return ‘NA‘
            elif weight < 1000:
                return weight / 2.2
            elif 9000 < weight < 9999:
                return weight - 9000
            else:
                return weight

        for rec in self.records:
            # recode wtkg2
            if rec.wtkg2 in [‘NA‘ 99999]:
                rec.wtkg2 = ‘NA‘
            else:
                rec.wtkg2 /= 100.0

            # recode wtyrago
            rec.weight2 = CleanWeight(rec.weight2)
            rec.wtyrago = CleanWeight(rec.wtyrago)

            # recode htm3
            if rec.htm3 == 999:
                rec.htm3 = ‘NA‘

            # recode age
            if rec.age in [7 9]:
                rec.age = ‘NA‘


    def SummarizeHeight(self):
        “““Print summary statistics for male and female height.“““

        # make a dictionary that maps from gender code to list of heights
        d = {1:[] 2:[] ‘all‘:[]}
        [d[r.sex].append(r.htm3) for r in self.records if r.htm3 != ‘NA‘]
        [d[‘all‘].append(r.htm3) for r in self.records if r.htm3 != ‘NA‘]
        
        print ‘Height (cm):‘
        print ‘key n     mean     var    sigma     cv‘
        for key t in d.iteritems():
            mu var = thinkstats.TrimmedM

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件     2374789  2018-01-02 23:12  think bayes.pdf
     目录           0  2016-02-23 03:21  ThinkBayes-master\
     文件         303  2016-02-23 03:21  ThinkBayes-master\.gitignore
     目录           0  2016-02-23 03:21  ThinkBayes-master\book\
     文件        4440  2016-02-23 03:21  ThinkBayes-master\book\back.png
     文件      330287  2016-02-23 03:21  ThinkBayes-master\book\book.tex
     目录           0  2016-02-23 03:21  ThinkBayes-master\book\figs\
     文件       25355  2016-02-23 03:21  ThinkBayes-master\book\figs\dungeons1.eps
     文件       12370  2016-02-23 03:21  ThinkBayes-master\book\figs\dungeons1.pdf
     文件       21019  2016-02-23 03:21  ThinkBayes-master\book\figs\dungeons2.eps
     文件       11148  2016-02-23 03:21  ThinkBayes-master\book\figs\dungeons2.pdf
     文件       24262  2016-02-23 03:21  ThinkBayes-master\book\figs\dungeons3.eps
     文件       10770  2016-02-23 03:21  ThinkBayes-master\book\figs\dungeons3.pdf
     文件       20589  2016-02-23 03:21  ThinkBayes-master\book\figs\euro1.eps
     文件       10363  2016-02-23 03:21  ThinkBayes-master\book\figs\euro1.pdf
     文件       23991  2016-02-23 03:21  ThinkBayes-master\book\figs\euro2.eps
     文件       11829  2016-02-23 03:21  ThinkBayes-master\book\figs\euro2.pdf
     文件       23968  2016-02-23 03:21  ThinkBayes-master\book\figs\euro3.eps
     文件       11636  2016-02-23 03:21  ThinkBayes-master\book\figs\euro3.pdf
     文件       29326  2016-02-23 03:21  ThinkBayes-master\book\figs\hockey0.eps
     文件       13851  2016-02-23 03:21  ThinkBayes-master\book\figs\hockey0.pdf
     文件       29881  2016-02-23 03:21  ThinkBayes-master\book\figs\hockey1.eps
     文件       15267  2016-02-23 03:21  ThinkBayes-master\book\figs\hockey1.pdf
     文件       20027  2016-02-23 03:21  ThinkBayes-master\book\figs\hockey2.eps
     文件       10655  2016-02-23 03:21  ThinkBayes-master\book\figs\hockey2.pdf
     文件       23879  2016-02-23 03:21  ThinkBayes-master\book\figs\hockey3.eps
     文件       12557  2016-02-23 03:21  ThinkBayes-master\book\figs\hockey3.pdf
     文件       27643  2016-02-23 03:21  ThinkBayes-master\book\figs\jaynes1.eps
     文件       14127  2016-02-23 03:21  ThinkBayes-master\book\figs\jaynes1.pdf
     文件       23929  2016-02-23 03:21  ThinkBayes-master\book\figs\jaynes2.eps
     文件       12614  2016-02-23 03:21  ThinkBayes-master\book\figs\jaynes2.pdf
............此处省略201个文件信息

评论

共有 条评论