CS 111: Program Desig I Lecture #26: Heat maps, Nothig, Predictive Policig Robert H. Sloa & Richard Warer Uiversity of Illiois at Chicago November 29, 2018
Some Logistics Extra credit: Sample Fial Exam Questios Post oe good sample fial exam uestio to Piazza by the lat day of classes uder the topic sample_exam_uestio for oe poit of extra credit o the course. You will kow your uestio has bee accepted whe a professor marks it with "Good uestio" Predictive policig assigmet has bee posted to Piazza. Datasets are at lik (usig Box.com). Prof. Sloa office hours today: 3:30 5:00
HEAT MAPS
Heat map (or Heatmap) Heat map: Graphical represetatio of data where idividual values i matrix represeted as colors (Word Heatmap was coied ad trademarked i 1991)
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5, 6] y = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0] itesity = [ [5, 10, 15, 20, 25], [30, 35, 40, 45, 50], [55, 60, 65, 70, 75], [80, 85, 90, 95, 100], [105, 110, 115, 120, 125], [130, 135, 140, 145, 150] ] plt.pcolor(x, y, itesity) # Creates heatmap, origi lower left corer plt.summer() # Color scheme: Gree (low) to Yellow (hi) plt.colorbar() # Adds colorbar so we kow what values mea plt.title("simple Heat Map Example")
Heat maps i Pytho Everythig we eed is i matplotlib.pyplot, so import matplotlib.plot as plt plt.pcolor(c) creates heat map from array C of color values Row 0 displayed at bottom of figure Each row displayed left to right So C[0][0] i lower left corer plt.pcolor(x, Y, C) has lists of umbers for X- ad Y-axes
Fecepost issue If you wat to specify borders of a ru of rectagles placed side-by-side, eed +1 specificatios left ed of every rectagle, ad right ed of last plt.pcolor(x, Y, C) should have le(x) = 1 + umber of colums of C le(y) = 1 + umber of rows of C I plt.pcolor(c) form labels are supplied startig at 0 for both X ad Y
Heat maps i Pytho Everythig we eed is i matplotlib.pyplot, so import matplotlib.plot as plt plt.pcolor ca take sigle array-like list of lists C as argumet, or optioally also lists X & Y givig boudaries of the rectagles plt.pcolor(c) creates heat map from array C of color values Row 0 displayed at bottom of figure Each row displayed left to right So C[0][0] i lower left corer plt.pcolor(x, Y, C) has lists of umbers for X- ad Y-axes
Fecepost issue If you wat to specify borders of a ru of rectagles placed side-by-side, eed +1 specificatios left ed of every rectagle, ad right ed of last plt.pcolor(x, Y, C) should have le(x) = 1 + umber of colums of C le(y) = 1 + umber of rows of C I plt.pcolor(c) form labels are supplied startig at 0 for both X ad Y
What do the colors mea? Colorbar plt.colorbar() places colorbar at right of plot Gives key for what colors correspod to what matrix values
Which colors i the heat map? Wat colors where huma brai uderstads idea of directio of colors. Seuetial ot raibow Bad Good Good (better?)
To set the colors ( colormap ) plt.viridis()# viridis colormap plt.summer() # summer colormap etc. Use matplotlib colormap from the Perceptually Uiform Seuetial*, Seuetial, or Seuetial(2) group *The favorite of the politically correct visualizatio police
Reasoable Colormap choices From http://matplotlib.org/examples/col or/colormaps_referece.html
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5, 6] y = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0] itesity = [ [5, 10, 15, 20, 25], [30, 35, 40, 45, 50], [55, 60, 65, 70, 75], [80, 85, 90, 95, 100], [105, 110, 115, 120, 125], [130, 135, 140, 145, 150] ] plt.pcolor(x, y, itesity) plt.summer() plt.colorbar() plt.title("simple Heat Map Example")
REVIEW (?): FUNCTIONS, RETURN VALUES, NONE
Which of these is a fuctio that returs True? def A(s): if s == s: prit(true) def B(s): if s == s: retur True C: 2 i [1, 2, 3] A. A B. B C. C D. A ad B E. All of them
How cofidet are you of your aswer? A. Very Highly cofidet: I've got this B. Very cofidet C. Somewhat cofidet D. Not so cofidet: educated guess E. Not cofidet at all: radom guess ad/or bullied ito by the rest of my small group
Commo Pytho begier mistake def times3(x): y = 3 * x prit(y) # Bad! should use retur istead Caot use it as we wat to, i, e.g., to_triple = 14 tripled = times3(to_triple) prit("the result of", to_triple, " tripled is", tripled)
Why a mistake: what's coceptually wrog def times3(x): y = 3 * x prit(y) # Bad! should use retur istead Problem: It does prit value of triplig its iput but that value ot retured to place where fuctio is called!
Why mistake: what happes def times3(x): y = 3 * x prit(y) # Bad! should use retur istead >>> to_triple = 14 >>> tripled = times3(to_triple) >>> prit("the result of ", to_triple, " tripled is", tripled) The result of 14 tripled is Noe
Pytho fuctios: Nofruitful = Noe All Pytho fuctios that do ot ot have a explicit retur statemet retur Noe Noe is special Pytho value (of type NoeType) whose mai use is that it is retured automagically by fuctios that do ot execute a explicitly retur statemet with a retur value Thik of Noe as "o value" Legal to write fuctio that explicitly returs oe: retur Noe
What does the fuctio retur def mult(a, b, c): prit (a*b*c) A. Noe (o value) B. The value of a*b*c C. The strig 'a*b*c'
Nothigess cotiued a (aka NaN) is a float type missig/ull values Reuired to exist i all laguages implemetig curret stadard for float umbers (IEEE floatig poit) Ca get it explicitly as math.a ad umpy.a Comes up i padas, which uses umpy "uder the hood" Queer beasty for ay purposes but pritig/seeig math.a == math.a False
PREDICTIVE POLICING
What Is Predictive Policig? The applicatio of aalytical uatitative techiues makig statistical predictios to idetify targets for crime prevetio solve past crimes. Perry et al., Predictive Policig: The Role of Crime Forecastig i Law Eforcemet Operatios
You Ca Idetify Targets for Crime Prevetio Ward map of Chicago divided at Roosevelt Ave. Is there more crime orth or south of Roosevelt Ave.?
From The Crimes Database You Are Usig A assault is a itetioal act creatig a apprehesio immiet harmful or offesive cotact. Where would sed more police? North of Roosevelt, or South of Roosevelt? What assumptios uderlie the graph?
Crime Is Predictable There is a strog body of evidece to support the theory that crime is predictable (i the statistical sese) maily because crimials ted to operate i their comfort zoe. That is, they ted to commit the type of crimes that they have committed successfully i the past, geerally close to the same time ad locatio. Perry et al., Predictive Policig.
PredPol Usig oly three data poits crime type, crime locatio ad crime date/time PredPol s powerful software provides each law eforcemet agecy with customized crime predictios for the places ad times that crimes are most likely to occur. PredPol pipoits small areas, depicted i 500 feet by 500 feet boxes o maps that are automatically geerated for each shift of each day. The algorithms used by PredPol... are based o the observatio that certai crime types ted to cluster i time ad space. http://www.predpol.com/how-predpol-works/
Types of Predictios Predict crimes: predict places ad times with a icreased risk of crime. This is PrePol. Predict offeders: predict people at high risk for committig a future crime. Predict victims: idetify groups ad idividuals likely to become victims. Predict perpetrators: Match people with past crimes.