API Guide for Data Scientists

栏目: IT技术 · 发布时间: 4年前

内容简介: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.

API Guide for Data Scientists

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:

API Guide for Data Scientists

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:

API Guide for Data Scientists

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.

API Guide for Data Scientists

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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

性能之巅

性能之巅

Brendan Gregg / 徐章宁、吴寒思、陈磊 / 电子工业出版社 / 2015-8-15 / 128

《性能之巅:洞悉系统、企业与云计算》基于Linux 和Solaris 系统阐述了适用于所有系统的性能理论和方法,Brendan Gregg 将业界普遍承认的性能方法、工具和指标收集于本书之中。阅读本书,你能洞悉系统运作的方式,学习到分析和提高系统与应用程序性能的方法,这些性能方法同样适用于大型企业与云计算这类最为复杂的环境的性能分析与调优。一起来看看 《性能之巅》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具