Rails into your Machine Learning

Ruby on Machine Learning

Software Development is all about getting different technologies to talk to one another. You talk to a website by putting the URL in your browser’s address bar and then click around. The Front-End page that loads is a Symphony of independent languages and frameworks working together to create the overall experience you enjoy. You click on a button and a JavaScript event is triggered. This event responds by both talking back to you by making the HTML page look differently and also talking to the next internal program. This could be in the form of an AJAX fetch request to the Back-End server. The Back-End then talks to the database and extracts data. These each would be less than helpful to users by standing alone. However together there is an emergent property of something living and interactive. Machine Learning as we have discussed is built on data. These datasets have thus far have been premade for us, typically by researchers. Today will will go back to the beginning of this software development blog. We will use Ruby on Rails and create a dataset from our rails database . We can then use this dataset from our rails database and create a csv file dataset for Machine Learning.

Much like JavaScript and Python, Ruby has modules or specific blocks of code that have been put together to achieve a single task. Ruby has a CSV module for comma separated values as they are ubiquitous and what i’ll term a pseudo-data structure. There are a few different ways we could have the end csv product work. For example we could literally just server up csv from our server and read it live from the database in our browser. From there we could actually just put the URL in the jupyter notebook while developing our Machine Learning Model. Today we will take a different approach. We will automate our Rails application to download our CVS file onto our desktop. In production a user would be able to download this file from the URL.

Let's start from the top and I’ll break this down into numbered steps so you can follow along.

  1. Gem and require

You will need to install the csv gem in your terminal. You also need to locate the file ‘application.rb’ in the rails app and add the line requre ‘csv’ at the top of the file.

bundle i csv -v 3.0.0 #=> aslo add requre ‘csv’ to application.rb

application.rb

2. Controller Action:

We will access our database as we normally would by requesting endpoints in the URL. HOWVER we are going to add ‘.csv’ to whatever resource we want to grab. Today I used my GlobalWarming App that I have live online. I will modify this app to respond to .csv formatting. Currently if you type [myapp.com]/cities.json you will get JSON in the browser of all of the cities I am Tracking

json from myapp.com/cities.json

I will use this functionality and replace .json with .csv. Effectively adding a new format to respond to. NOTE: My routs are already set up to respond to /cities.json to render JSON in the browser.

Add a new respond to format in the controller.

in the controller action add a .csv format

There is a lot going on in this picture. Just focus on the respond_to block and the format.csv. note: open weather map is free and the hidden key was effectively to show I could protect data if needed(don’t worry it works and hides the key). As you can see I have format.csv {send_data @alll.to_csv }

Let's break that down. within the respond_to block rails will look at what format the user is asking for. Once it sees ‘.csv’ will will know to trigger the format.csv block. This block has send_data attribute. This will render a file from the browser and actually download it. The following @alll is just the instance variable from the controller action for City.all(all of the cities in my database). After @alll you will see ‘.to_csv’. This will be step 3.

3. class method to_csv to create our comma separated values file

Go to your Model in question and create a class to_csv method. (NOTE: to_csv could be to_bananas this is not inherent to the ruby or the CSV module).

For my example I went to the City model and created a class method

class method to create csv file

This class method will use the built in CSV.generate method which will be in a block. As you can see the attributes is equal to my columns. So I have name, region and fetchURL in my postgresql database table. The %w will create the csv for us and pack them to be used. You must add your column names in your file.

That's It. With those three steps you will have a CSV file downloaded from your Rails app and then ready for Exploratory Data Analysis and Machine Learning.

cities from my rails db