Easy-breezy REST API in Python

A Simple Tutorial Example of a REST API with Flask

Photo by Brendan Church on Unsplash

For a recent two day hackathon, I worked with a software developer (Roman Turner) to make a recommendation app. I’m a Data Scientist, so my superpowers are Python, Data Science and Flask. His superpowers are JavaScript and React (amongst others). We decided that he would work on the front-end web app, and I would run a recommendation model on the backend in Python.

To get the two to talk to each other, I built a Flask API which would allow my partner to send JSON requests, and receive a JSON formatted responses from my model.

I have made my share…

How to effectively use Python’s built in sorting algorithm

Sorting with Keys and Lambda Function in Python

Photo by Florian Berger on Unsplash

As a high school Computer Science teacher, I found that sorting lists using keys was often challenging for students. Sorting is a routine coding task essential for almost any Python project, so mastering its use is essential to your programming future.

This article will walk you through the most common and useful ways to sort lists (and other objects) in Python using the builtin functions and methods. This article is written for novice programmers using Python3.

What’s Happening Under the Hood?

Python uses an internal algorithm written by Tim Peters (the algorithm is fondly referred to as TimSort). It is fast and efficient. …

Learn list comprehensions with examples

The Pythonic Way to Create Lists

the rough syntax

This article is intended to introduce novice Pythonistas to the art of List Comprehension. It is a vital building block for advanced coding in Python, and should be embraced soon after you become comfortable creating loops and lists in Python.

I have spent 7 years teaching Python to high school students. For most topics, most students intuitively picked up the structure and syntax. However, there were a handful of topics and structures that were always met with some resistance. One of those is the list comprehension.

The List Comprehension

Creating and populating a list is a common Python task. …

Pick a simple baseline accuracy to demonstrate that your classification model has skill on a problem

Photo by Robert Anasch on Unsplash

When you evaluate a new machine learning model and end up with an accuracy number or other metric, you need to know if it is meaningful. Particularly in imbalanced classification models, it can appear that your model isn’t really doing much better than guessing. What accuracy is enough to call your model useful?

This article is just to show the simplistic baseline accuracy for your model that I find useful.

The Problem

I created an election model that predicted the voting habits of all 3200+ US counties (Democrat or Republican) using economic metrics. The model had an accuracy of 86%. That sounds…

An example NLP classification model using a Recurrent Neural Network with the Keras Python library

Photo by Mick Haupt on Unsplash

This will be a minimal working example of Natural Language Processing (NLP) using deep learning with a Recurrent Neural Network (RNN) in Python. For this project, you should have a solid grasp of Python and a working knowledge of Neural Networks (NN) with Keras.

The goal here to build a NLP deep learning model to analyze Twitter sentiment about Apple and Google products. The dataset I used comes from CrowdFlower via data.world. In this dataset, human raters were asked to rate the sentiment of over 9,000 Tweets as positive, negative, or neither. I am seeking to use this labled dataset…

Python Geolocation

Photo by Waldemar Brandt on Unsplash

Nominatim is the Latin for (‘by name’). It is also a tool to search OpenStreetMap data by address or location (geocoding). Nominatim is included in the GeoPy in the GeoPy Python library. It has saved me on a couple of recent projects where I had datasets with addresses, but none of the latitude/longitude data required for map plots and pretty much any other location based task.

Lookup a Single Address

We will start with the base code, similar to what is provided in the excellent documentation at https://geopy.readthedocs.io/en/stable/.

This code creates the Nominatim object

The code above instantiated a Nominatim object called geolocator on…

Do red light cameras reduce traffic accidents?

Photo by Gaby on Unsplash

I recently received a $100 red light ticket from the city of Chicago. Along with the ticket was a link to a picture and video of my vehicle not coming to a full stop when turning right at a red light. I fell victim to the dreaded Chicago red light camera. A friend told me about a study showing red light cameras made intersections more dangerous. As a data scientist, I was intrigued. This sounded like an answerable question in Chicago, so I built a project to attempt to answer it.

I also created a web app to go along…

Make and deploy a simple Flask app for your ML project

The purpose of this article is to show you a very simple ‘productionization’ of a machine learning model using Flask, Heroku, and GitHub. This article assumes a solid understanding of Python code and that you have already trained a Machine Learning model in Python but have not made a Flask app previously for this purpose. You will need access to GitHub and a Heroku account later; we will be creating a repository for our app and deploying to Heroku via the repo. Experience with HTML and Jinja is also nice to have, but not necessary.

Flask is a lightweight framework…

Photo by Markus Winkler on Unsplash

This is a quick start example of how to create a basic PySpark session in Google Colab. This is not a tutorial for the use PySpark or Colab, simply a quick example to get you up and running.

Why I use Google Colab for PySpark projects:

  • PySpark can be challenging to setup on some computers. I found Colab far easier to get ‘up and running’ than other options like Docker.
  • All the major libraries are already installed and ready to be imported. (Scikit-learn, Matplotlib, TensorFlow etc.)
  • You can use bash commands (just add ! before your command)
  • If you are already familiar with using ipynb files in Jupyter…

Modeling US elections using chain stores

If your county has more Dollar Tree stores than Starbucks, you’re likely a Republican. If you have even a single Trader Joe’s store in your county, you’re probably a Democrat.

Photo by Elliott Stallion on Unsplash

The chain stores found in a community can tell us a lot about the people that live there. Think about Target and Walmart; what political party do you associate with each? I wanted to investigate that perception, so I built an election model based solely on the number of chain stores in your area.

My hypothesis is that the number of total chain stores will correlate with an increase in…

Aaron Lee

Data Scientist, Computer Science Teacher, and Veteran. www.linkedin.com/in/aaron-lee-data/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store