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


《贝叶斯思维:统计建模的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-masterook
     文件        4440  2016-02-23 03:21  ThinkBayes-masterookack.png
     文件      330287  2016-02-23 03:21  ThinkBayes-masterookook.tex
     目录           0  2016-02-23 03:21  ThinkBayes-masterookfigs
     文件       25355  2016-02-23 03:21  ThinkBayes-masterookfigsdungeons1.eps
     文件       12370  2016-02-23 03:21  ThinkBayes-masterookfigsdungeons1.pdf
     文件       21019  2016-02-23 03:21  ThinkBayes-masterookfigsdungeons2.eps
     文件       11148  2016-02-23 03:21  ThinkBayes-masterookfigsdungeons2.pdf
     文件       24262  2016-02-23 03:21  ThinkBayes-masterookfigsdungeons3.eps
     文件       10770  2016-02-23 03:21  ThinkBayes-masterookfigsdungeons3.pdf
     文件       20589  2016-02-23 03:21  ThinkBayes-masterookfigseuro1.eps
     文件       10363  2016-02-23 03:21  ThinkBayes-masterookfigseuro1.pdf
     文件       23991  2016-02-23 03:21  ThinkBayes-masterookfigseuro2.eps
     文件       11829  2016-02-23 03:21  ThinkBayes-masterookfigseuro2.pdf
     文件       23968  2016-02-23 03:21  ThinkBayes-masterookfigseuro3.eps
     文件       11636  2016-02-23 03:21  ThinkBayes-masterookfigseuro3.pdf
     文件       29326  2016-02-23 03:21  ThinkBayes-masterookfigshockey0.eps
     文件       13851  2016-02-23 03:21  ThinkBayes-masterookfigshockey0.pdf
     文件       29881  2016-02-23 03:21  ThinkBayes-masterookfigshockey1.eps
     文件       15267  2016-02-23 03:21  ThinkBayes-masterookfigshockey1.pdf
     文件       20027  2016-02-23 03:21  ThinkBayes-masterookfigshockey2.eps
     文件       10655  2016-02-23 03:21  ThinkBayes-masterookfigshockey2.pdf
     文件       23879  2016-02-23 03:21  ThinkBayes-masterookfigshockey3.eps
     文件       12557  2016-02-23 03:21  ThinkBayes-masterookfigshockey3.pdf
     文件       27643  2016-02-23 03:21  ThinkBayes-masterookfigsjaynes1.eps
     文件       14127  2016-02-23 03:21  ThinkBayes-masterookfigsjaynes1.pdf
     文件       23929  2016-02-23 03:21  ThinkBayes-masterookfigsjaynes2.eps
     文件       12614  2016-02-23 03:21  ThinkBayes-masterookfigsjaynes2.pdf
............此处省略201个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)