<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import math <EOL> import sys <EOL> import pandas <EOL> import numpy as np <EOL> import thinkstats2 <EOL> import thinkplot <EOL> def Summarize ( df , column , title ) : <EOL> """<STR_LIT>""" <EOL> items = [ <EOL> ( '<STR_LIT:all>' , df [ column ] ) , <EOL> ( '<STR_LIT>' , df [ df . sex == <NUM_LIT:1> ] [ column ] ) , <EOL> ( '<STR_LIT>' , df [ df . sex == <NUM_LIT:2> ] [ column ] ) , <EOL> ] <EOL> print ( title ) <EOL> print ( '<STR_LIT>' ) <EOL> for key , series in items : <EOL> mean , var = series . mean ( ) , series . var ( ) <EOL> std = math . sqrt ( var ) <EOL> cv = std / mean <EOL> t = key , len ( series ) , mean , var , std , cv <EOL> print ( '<STR_LIT>' % t ) <EOL> def CleanBrfssFrame ( df ) : <EOL> """<STR_LIT>""" <EOL> df . age . replace ( [ <NUM_LIT:7> , <NUM_LIT:9> ] , float ( '<STR_LIT>' ) , inplace = True ) <EOL> df . htm3 . replace ( [ <NUM_LIT> ] , float ( '<STR_LIT>' ) , inplace = True ) <EOL> df . wtkg2 . replace ( [ <NUM_LIT> ] , float ( '<STR_LIT>' ) , inplace = True ) <EOL> df . wtkg2 /= <NUM_LIT> <EOL> df . wtyrago . replace ( [ <NUM_LIT> , <NUM_LIT> ] , float ( '<STR_LIT>' ) , inplace = True ) <EOL> df [ '<STR_LIT>' ] = df . wtyrago . apply ( lambda x : x / <NUM_LIT> if x < <NUM_LIT> else x - <NUM_LIT> ) <EOL> def ReadBrfss ( filename = '<STR_LIT>' , compression = '<STR_LIT>' , nrows = None ) : <EOL> """<STR_LIT>""" <EOL> var_info = [ <EOL> ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , int ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , int ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , int ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , int ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , int ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , int ) , <EOL> ] <EOL> columns = [ '<STR_LIT:name>' , '<STR_LIT:start>' , '<STR_LIT:end>' , '<STR_LIT:type>' ] <EOL> variables = pandas . DataFrame ( var_info , columns = columns ) <EOL> variables . end += <NUM_LIT:1> <EOL> dct = thinkstats2 . FixedWidthVariables ( variables , index_base = <NUM_LIT:1> ) <EOL> df = dct . ReadFixedWidth ( filename , compression = compression , nrows = nrows ) <EOL> CleanBrfssFrame ( df ) <EOL> return df <EOL> def MakeNormalModel ( weights ) : <EOL> """<STR_LIT>""" <EOL> cdf = thinkstats2 . Cdf ( weights , label = '<STR_LIT>' ) <EOL> mean , var = thinkstats2 . TrimmedMeanVar ( weights ) <EOL> std = math . sqrt ( var ) <EOL> print ( '<STR_LIT>' , len ( weights ) , mean , std ) <EOL> xmin = mean - <NUM_LIT:4> * std <EOL> xmax = mean + <NUM_LIT:4> * std <EOL> xs , ps = thinkstats2 . RenderNormalCdf ( mean , std , xmin , xmax ) <EOL> thinkplot . Plot ( xs , ps , label = '<STR_LIT>' , linewidth = <NUM_LIT:4> , color = '<STR_LIT>' ) <EOL> thinkplot . Cdf ( cdf ) <EOL> def MakeNormalPlot ( weights ) : <EOL> """<STR_LIT>""" <EOL> mean , var = thinkstats2 . TrimmedMeanVar ( weights , p = <NUM_LIT> ) <EOL> std = math . sqrt ( var ) <EOL> xs = [ - <NUM_LIT:5> , <NUM_LIT:5> ] <EOL> xs , ys = thinkstats2 . FitLine ( xs , mean , std ) <EOL> thinkplot . Plot ( xs , ys , color = '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> xs , ys = thinkstats2 . NormalProbability ( weights ) <EOL> thinkplot . Plot ( xs , ys , label = '<STR_LIT>' ) <EOL> def MakeFigures ( df ) : <EOL> """<STR_LIT>""" <EOL> weights = df . wtkg2 . dropna ( ) <EOL> log_weights = np . log10 ( weights ) <EOL> thinkplot . PrePlot ( cols = <NUM_LIT:2> ) <EOL> MakeNormalModel ( weights ) <EOL> thinkplot . Config ( xlabel = '<STR_LIT>' , ylabel = '<STR_LIT>' ) <EOL> thinkplot . SubPlot ( <NUM_LIT:2> ) <EOL> MakeNormalModel ( log_weights ) <EOL> thinkplot . Config ( xlabel = '<STR_LIT>' ) <EOL> thinkplot . Save ( root = '<STR_LIT>' ) <EOL> thinkplot . PrePlot ( cols = <NUM_LIT:2> ) <EOL> MakeNormalPlot ( weights ) <EOL> thinkplot . Config ( xlabel = '<STR_LIT:z>' , ylabel = '<STR_LIT>' ) <EOL> thinkplot . SubPlot ( <NUM_LIT:2> ) <EOL> MakeNormalPlot ( log_weights ) <EOL> thinkplot . Config ( xlabel = '<STR_LIT:z>' , ylabel = '<STR_LIT>' ) <EOL> thinkplot . Save ( root = '<STR_LIT>' ) <EOL> def main ( script , nrows = <NUM_LIT:1000> ) : <EOL> """<STR_LIT>""" <EOL> thinkstats2 . RandomSeed ( <NUM_LIT> ) <EOL> nrows = int ( nrows ) <EOL> df = ReadBrfss ( nrows = nrows ) <EOL> MakeFigures ( df ) <EOL> Summarize ( df , '<STR_LIT>' , '<STR_LIT>' ) <EOL> Summarize ( df , '<STR_LIT>' , '<STR_LIT>' ) <EOL> Summarize ( df , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if nrows == <NUM_LIT:1000> : <EOL> assert ( df . age . value_counts ( ) [ <NUM_LIT> ] == <NUM_LIT> ) <EOL> assert ( df . sex . value_counts ( ) [ <NUM_LIT:2> ] == <NUM_LIT> ) <EOL> assert ( df . wtkg2 . value_counts ( ) [ <NUM_LIT> ] == <NUM_LIT> ) <EOL> assert ( df . wtyrago . value_counts ( ) [ <NUM_LIT> / <NUM_LIT> ] == <NUM_LIT> ) <EOL> assert ( df . htm3 . value_counts ( ) [ <NUM_LIT> ] == <NUM_LIT> ) <EOL> assert ( df . finalwt . value_counts ( ) [ <NUM_LIT> ] == <NUM_LIT> ) <EOL> print ( '<STR_LIT>' % script ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( * sys . argv ) </s>
