Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example analysis notebook and (*very*) rudimentary notes for a customized python-novice-gapminder. http://blog.byronjsmith.com/python-lesson-balance.html
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Introduction

  • Demonstrate "complex" analysis notebook.

Pandas/Matplotlib

  • Load data
    • Talk about the optional parameter index_col=
  • Walk through the gapminder_all data.
  • Plot 1957 life-expectancy against GDP/pop
    • Note the column-as-attribute syntax
    • Note outlier
  • data.gdpPercap_1957.argmax()
  • data.drop(...)
  • Plot Africa data alone
    • Use data[data.continent == 'Africa'] syntax
    • Plot with a color
  • CHALLENGE: Select the subset of the data from the continent which contains the country with the maximum life-expectancy in 1967

For loops

  • Talk about wanting to repeat something for each continent.

  • For loop syntax

  • CHECK YOUR UNDERSTANDING:

    Fill in the blanks in the program below so that it prints “nit” (the reverse of the original character string “tin”).

    original = "tin"
    result = ____
    for char in original:
        result = ____
    print(result)
  • Plot only a subset of the continents (africa, asia, europe)

If statements

  • Talk about wanting to do something different depending on which continent we're considering.
  • If statement syntax (notice the 'block' syntax)
    • if
    • if, else
    • if, elif, else
  • Plot a different color for several of the continents.

Plot Aesthetics

  • Add x/y axis labels
  • Add a title
  • Size the points by np.sqrt(population) (so that the area is proportional)
  • Add a legend (dummy plotting)
  • Note the uglyness of the numbers

Defining Functions

  • Celsius to fahrenheit conversion
  • Kelvin to C conversion
  • Talk about function composition
  • What are the benefits of using functions?
  • Let's write a function to convert large numbers into scientific notation
  • Add an optional argument to set the precision
  • Add some documentation and show how help(scientific_notation) works

Defensive programming

  • How do we know if it's working? (unit testing, assert)

Statsmodels

  • Line of best fit
  • Statsmodels
  • formula
  • fitting the model and viewing the summary
  • Getting the coefficients out and using them
  • Plotting the line
  • CHALLENGE: extend this analysis and share with your neighbors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment