内容简介:APIs are the backbone of software everywhere. APIs allow us to package up and expose code for other users or software to consume. And atIn short, code reuse is the reason we need more APIs in Data Science. Code reuse is core across software, but somehow in
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!
以上所述就是小编给大家介绍的《API Guide for Data Scientists》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
人月神话(40周年中文纪念版)
(美) 布鲁克斯(Brooks, F. P.) 著 / UML China翻译组,汪颖 译 / 清华大学出版社 / 2015-4-1 / 68.00元
在软件领域,很少能有像《人月神话》一样具有深远影响力和畅销不衰的著作。Brooks博士为人们管理复杂项目提供了最具洞察力的见解,既有很多发人深省的观点,又有大量软件工程的实践。本书内容来自Brooks博士在IBM公司SYSTEM/360家族和OS/360中的项目管理经验,该项目堪称软件开发项目管理的典范。该书英文原版一经面世,即引起业内人士的强烈反响,后又译为德、法、日、俄、中、韩等多种文字,全球......一起来看看 《人月神话(40周年中文纪念版)》 这本书的介绍吧!