Mapping Housing Prices in Brooklyn
By: Sam Bell
Tags: Brooklyn, Python, Data Visualization, Flatiron School, Real Estate

Mapping Housing Prices in Brooklyn

A GeoMapping Tutorial with Bokeh

Photo by NASA on Unsplash

In his novel How To Kill A City, P.E. Moskowitz writes about the effects of gentrification in 4 American cities: New Orleans, Detroit, San Francisco, and New York. He credits geographer Neil Smith for developing the most influential academic theory on gentrification since 1979 — the rent gap. Gentrification is predictable. It follows the basic principle of capitalist economics, “investors will go where the rate of potential return is the highest.” By examining tax data, one could pinpoint where buildings could be bought cheap and made more expensive in a short amount of time. The jackpot would be finding a dilapidated building within a few blocks of a gentrified, or gentrifying, neighborhood. The key is the disparity between how much a property could be bought for and how much it could be worth once gentrified. The larger the rent gap, the higher the chance a building would be targeted. Real estate developers looking for the next neighborhood to gentrify rely on the fact that the sellers do not realize the potential worth.

Kaggle is home to many great data sets and I was lucky enough to find a great data set of Brooklyn Home Sales from 2003–2017. This data came with shapefiles from NYC Pluto and lots of null values. Each entry came with an address and X and Y coordinates that plotted the points in the shape of Brooklyn.

I really wanted to plot all of the points over a map of Brooklyn, and make some interactive visualizations, so I decided to work with Bokeh for this project.

My first step was to convert all of my X and Y coordinates to latitude and longitude, so I could then convert those to their proper mercator coordinates. Bokeh will only accept mercator coordinates when plotting a map since your computer screen is flat, and Earth is not!

It’s a banana.

Since I was unable to find a free and quick resource for geocoding, I worked with what I had. I took 20 data points and looked up their latitude and longitude coordinates one at a time. With these coordinates I created a training dataset in order to predict the latitude and longitude of the other 25,000 points with a simple linear regression.

Naturally, I repeated this for the longitudinal coordinates as well and placed those values into their own column. The outcome was pretty accurate. When I compared a random datapoint’s estimated coordinates and the actual coordinates, the difference was only a few blocks. Moreover, when I plotted the final set of coordinates there weren’t any houses in the water, or in the parks.

Here is a function to convert your latitude and longitude coordinates to mercator coordinates.

Bokeh plots have a lot of potential for interaction and user manipulation. You will need to install bokeh with the conda command. To read up on the documentation and more explicit instructions, click here. The following are the important packages to import in order to get started with your plotting.

Bokeh has their own tile sets for maps, which allows for mapping without shapefiles or APIs. This is a huge plus for me. Their tile sets are listed here. Please note that there are some confusing directions in the documentation here. The correct way to import a tile set is to simply import it directly. In the code above, you call the package bokeh.tile_providers and import whichever style of map you want to use. Using get_provider or Vendors will return an error. Some color palettes are lists and some are dictionaries. Make sure to check the datatype of your chosen palette like so.

If the type is a list, then you’re fine to just define your palette and leave it at that. If the type is a dictionary, you must indicate how many colors to use like in the second line of code below.

I am going to break everything down in this chunk line by line. I defined my color palette and indicated that I wanted ten different colors, which would correspond with 10 different price ranges for the sold homes. Click here to check out all the palettes Bokeh offers. The source is defined as my data frame called TwoThree. This is all the relevant housing data from 2003.

I not only wanted to plot my data on a map to show location, but I wanted color to distinguish more expensive homes from less expensive homes. The mapper is defined for my color bar attributes. It requires a field name, and a range of low and high values. Bokeh has a great customizable hover function. When hovering over any point on your visualization, you can determine what information will pop up in the corresponding window. I used the variable tooltips to define what I wanted to show up. Each attribute is a tuple of the label and where the data is coming from. See the documentation here. If the data is coming from your data set, the second value must begin with a “@”, if the value is coming from the visualization itself, like the index or in-chart X and Y coordinates, you will precede the value with a “$”. I added a widget slider in order for the visualization to change and present new data based on the year selected. I will go into more detail on this in another tutorial. The slider will be purely decorative for now.

Pretty!

I define my figure’s X and Y axis-type as mercator. This will convert the mercator coordinates to latitude and longitude coordinates, so it will be like a real map! I call my tooltips, which are the values that will appear as you hover over each point, and I give my figure a title. Since this is a map, I must add my map tile as well.

I am creating a scatterplot, so I add circles to my figure by simply referring to the corresponding column names in quotes. The colors will be determined by my mapper variable, and the values from my data source. I create a color bar as a key for my points, and below that I indicate that I want to place my color bar to the right of my chart.

I can define my layout as a row or a column. A column will place everything on top of each other (up to down), but a row will place everything side by side of each other (left to right). I am placing my figure on top of my slider for now.

It looks like there were a lot of homes selling for under $2million in Brooklyn in 2003. In fact, a lot my data indicated a lot of homes were selling for under $1,000 most likely due to foreclosures.

With the zoom feature I found a cluster of more expensive homes in Park Slope. In 2003 I’m sure some real estate investors were looking to buy in the neighborhood of Prospect Heights just north of Park Slope.

Yes, just a few blocks away from the $3 million home is a house selling for $165 thousand.

I plan to work more on this project and will create at least one more tutorial for visual interactions with Bokeh. Let me know which mapping visualization tools you like to use and why. I will be sure to check them out!