The Analytic Edge Lecture code in Python Week8 Crime

VIDEO 3 - A Basic Line Plot

Load our data:

Convert the Date variable to time format

Extract the hour and the day of the week:

Let's take a look at the structure of our data again:

Create a simple line plot - need the total number of crimes on each day of the week.

We can get this information by creating a table:

Save this table as a data frame:

Create our plot

png

 

Make the "Weekdays" variable an ORDERED

Try again:

png

 

make the x values as ordered categorical data type won't help, it is

probably due to the difference in implementation ggplot in Python.

png

 

Change our x and y labels:

png

 

VIDEO 4 - Adding the Hour of the Day

Create a counts table for the weekday and hour:

Save this to a data frame:

Get variables Hour and Weekdays

Create out plot/Change the colors

ggplot in python does not support aes(group=Var) yet

Also the ggplot version I got using pip does not seem to plot legend correctly.

png

Redo our plot, this time coloring by Type:

png

Make the lines a little transparent:

png

Make a heatmap:

Struggled to plot heatmap using ggplot in Python without success. Turning to the old friend matplotlib

 

png

Change the color scheme and legend label

png

VIDEO 5 - Maps

Given up on it...

2 thoughts on “The Analytic Edge Lecture code in Python Week8 Crime

  1. 叶飘潇雨

    又看到前辈的大作了,之前看到关于Ng的机器学习课程的笔记,就膜拜得不行。今天在MOOC学院上上MIT的分析前沿这门课,还看到把R翻译成了Python(但是今年这门课怎么感觉和你上的那一次内容有些不一样呢)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *