(In order to load the stylesheet of this notebook, execute the last code cell in this notebook)

# Clustering the data from the Mayor's 24-hour hotline in Boston

## Introduction

In this assignment, we will do a clustering analysis on the 311 calls that we used in the previous homework. We will focus on choosing a value for k, clustering based on multiple features and detecting outliers.



In [32]:

%matplotlib inline
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt




In [31]:



## Step 1. Simple clustering

The first thing that we will try is to cluster the geolocation data of the calls. The geolocation of a record consists of a pair (latitude, longitude), which represents the position of the record on the map. Since the dataset that we have spans only a small geographic location, i.e. Boston, we will use the Euclidean distance when measuring how close two points are. Techinically, we should pick a projection in order to transform the data into 2d, and measure the Euclidean distance in the projected space.

Since we are dealing with numeric data, we might consider centering and normalizing them. In order to do that, check the StandardScaler from scikit-learn.

For exampe, the result of a clustering with 10 clusters is the following:

Filter the dataset to get the latitude and longitude, transform it as required and run the k-means clustering algorithm. Argue about the number of clusters you used. (pts 50)



In [31]:



## Step 2. Using multiple features

The next step in our analysis is to use more features than just the geolocation of a record (whose clustering only gives us information about the neighborhoods, which should not be a huge revelation!) Filter the dataset to get more features, transform them as required and compute another clustering. You should argue about the quality of your clustering and use domain knowledge (i.e. the fact that you are locals) to explore the clusters you produced and present them to the reader. (pts 30)



In [ ]:



## Step 3. Detecting outliers

Now that we have a clustering of the records in the dataset, we can detect the outlying ones. These are the ones who are the farthest from the centroids of their clusters. Track them down and describe any interesting observations that you can make. (pts 20)



In [ ]:




In [21]:

# Code for setting the style of the notebook
from IPython.core.display import HTML
def css_styling():
return HTML(styles)
css_styling()




Out[21]:

@font-face {
font-family: "Computer Modern";
src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');
}
.code_cell {
width: 105ex !important ;
margin-bottom: 15px !important;
}
div.cell {
margin-left: auto;
margin-right: auto;
width: 70%;
}
div.cell.selected {
border: thin rgba(171, 171, 171, 0.5) dashed;
}
h1 {
font-family: 'Alegreya Sans', sans-serif;
}
h2 {
font-family: 'EB Garamond', serif;
}
h3 {
font-family: 'EB Garamond', serif;
margin-top:12px;
margin-bottom: 3px;
}
h4 {
font-family: 'EB Garamond', serif;
}
h5 {
font-family: 'Alegreya Sans', sans-serif;
}
div.text_cell_render {
font-family: 'EB Garamond',Computer Modern, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
line-height: 145%;
font-size: 140%;
}
div.input_area {
border-color: rgba(0,0,0,0.10) !important;
background: #fafafa;
}
.CodeMirror {
font-family: "Source Code Pro";
font-size: 90%;
}
.prompt {
display: None;
}
.output {
}
.output_wrapper {
}
div.output_scroll {
width: inherit;
}
.inner_cell {
}
.text_cell_render h1 {
font-weight: 200;
font-size: 50pt;
line-height: 100%;
color:#CD2305;
margin-bottom: 0.5em;
margin-top: 0.5em;
display: block;
}
.text_cell_render h5 {
font-weight: 300;
font-size: 16pt;
color: #CD2305;
font-style: italic;
margin-bottom: .5em;
margin-top: 0.5em;
display: block;
}
.warning {
color: rgb( 240, 20, 20 )
}

MathJax.Hub.Config({
TeX: {
extensions: ["AMSmath.js"]
},
tex2jax: {
inlineMath: [ ['$','$'], ["\$","\$"] ],
displayMath: [ ['$$','$$'], ["\$","\$"] ]
},
displayAlign: 'center', // Change this to 'center' to center equations.
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 4}}
}
});