

API Guide for Data Scientists
source link: https://towardsdatascience.com/api-guide-for-data-scientists-e373f997ed61?gi=9ef227624456
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to use APIs and REST + JSON.
Mar 17 ·4min read
APIs are the backbone of software everywhere. APIs allow us to package up and expose code for other users or software to consume. And at Spawner we use JSON for everything from delivering data to accessing NLP, Financial ML, and Computer Vision in 3–5 lines of code. In this guide we’ll provide clear code examples to get you started with working with APIs. We’ll also give you a brief on REST and JSON for understanding data interchange standards.
Why APIs in Data Science?
In short, code reuse is the reason we need more APIs in Data Science. Code reuse is core across software, but somehow in Data Science we struggle with it. With more capable APIs we can largely solve our issues of code reuse. We need to establish a mindset of deploy and maintain once, reuse everywhere.
APIs fit beautifully into the current data stack. It’s great to be able to get all your models and data into one clean API that anyone throughout your project or company can access with the right credentials. For an example of how this might look:
With this API, you’re building a way for all your users to concurrently access your data stack. They no longer have to copy models and redeploy it themselves. However, we need some easy way to get our users access to our API “endpoints.” Enter JSON.
Using JSON
JSON is a standard file format used for storing and transmitting data. Almost all APIs use JSON for sending and receiving text and numbers. XML is often used as well for its flexibility. However, in JSON we can send images and other types by encoding the data as text and numbers.
For our code example, we’re going to use the Spawner API to do some ML tasks in very few lines of code. We’ll access the Spawner API documentation to see what endpoints are available for use.
We’ll use Python for making our “requests.”
There are 2 main REST API “verbs” we’ll focus on: GET and POST. We’ll focus on GET whenever we’re retrieving data from the API and not sending data. We’ll use POST whenever we’re sending data for inference and expecting a result sent back.
To illustrate this further, here’s how this might look:
In the above GET request we’re making a request to the API where there’s only one field: <token>. Many APIs require the user to have a token for authentication. We could very simple add our token to our code for this request. Here’s the above request in code:
# Tokens available at https://spawner.ai token = 'sp_vnz938vnzjd93nvnz' url = "https://spawnerapi.com/fundamentals/" + token response = requests.get(url) print(response.json())
The above fundamentals endpoint returns ratings for all covered equities in the S&P 500. It’s a Machine Learning model written by training many examples of historical data on fundamentals across cash flows, balance sheets, and basic equity data.
The POST is different from the GET because we’re sending the data in JSON format as [{‘text’:’what is the p/e ratio of apple?}] which the API will be able to process and send the result back. Here’s the POST in code:
def answer(): text = ‘What is the p/e ratio of apple?’ url = ‘https://spawnerapi.com/answer/' + token data = {‘text’: text} headers = {‘Content-type’: ‘application/json’} x = requests.post(url, data=json.dumps(data), headers=headers) print(x.text)
# Tokens available at https://spawner.ai token = 'sp_vnz938vnzjd93nvnz' text = 'What is the p/e ratio of apple?' url = 'https://spawnerapi.com/answer/' + token data = {'text': text} headers = {'Content-type': 'application/json'} x = requests.post(url, data=json.dumps(data), headers=headers) print(x.text)
The /answer endpoint is a Natural Language Processing endpoint that takes any financial question and returns an answer!
You can read about more useful Machine Learning endpoints here.
And here’s some example code for getting started with APIs in Python as well as in a Jupyter Notebook of API examples for easier use.
Closing
In your own company, you should be building your models and properly exposing them in an API, perhaps with various permutations of the same model. Whether or not you use the Spawner API or choose to build your own API ecosystem internally, keep things nice and clean, write good documentation, and encourage others to make good use of your existing code!
Recommend
-
38
SQL (Structured Query Language) is a standardised...
-
54
E xperience with version control is fast becoming a requirement for all data scientists. Version control can help data scientists work better as a team, facilitating collaboration on projects, sharing work a...
-
73
Who Do I Think I Am My name’s Darrin, and I’ve been a Data Scientist at Outcome Health for close to 2 years now. We’re a health tech company focused on patient education at the point of care. In English, that m...
-
59
Last month, I got an interesting request for a contract assignment on Upwork. An HR company called “FS consulting” tasked me with building an algorithm relating demographics to job performance. As a data scientist I was p...
-
43
Photo by Kristopher Roller o...
-
10
-
7
The Most Complete Guide to PyTorch for Data ScientistsBy Rahul Agarwal 08 September 2020...
-
5
The essential guide to debugging and error resolving for data scientistsSolve errors faster and have more time for creative work
-
14
A Guide For Engineers And Scientists — Python Numerical Methods Python Programming And Numerical Methods: A Guid...
-
2
Domain Knowledge: A Field Guide For New Data ScientistsHow you can develop domain knowledge without on-the-job exposure.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK