Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python...

30
#AnalyticsX Copyright © 2016, SAS Institute Inc. All rights reserved. Using Python With SAS® Cloud Analytic Services (CAS) Kevin Smith Software Developer SAS Xiangxiang Meng Sr Product Manager SAS

Transcript of Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python...

Page 1: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#AnalyticsXC o p y r ig ht © 201 6, SAS In st i tute In c. A l l r ig hts r ese rve d.

Using Python With SAS® Cloud Analytic Services (CAS)

Kevin SmithSoftware DeveloperSAS

Xiangxiang MengSr Product ManagerSAS

Page 2: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

What is it?

Open-source Python interface

to SAS’ cloud-based,

fault-tolerant, in-memory

analytics server.

Page 3: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

What Does it Do?

▪ Connects to CAS using binary (currently Linux only) or REST interface

▪ Calls CAS analytic actions and returns results in Python objects

▪ Implements a Pandas API that calls CAS actions in the background

+

Page 4: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

In a Nutshell▪ Access to SAS cloud analytics from Python

▪ Results as Python objects and Pandas DataFrames

▪ Support for familiar Pandas API

▪ Issue tracking and collaboration in development through GitHub project

+pandas

GitHub+

Page 5: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

SWAT SAS Wrapper for Analytics

Transfer

Page 6: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

How Does it Work?

# ipython

In[1]: import swat

In[2]: conn = swat.CAS(host, port,

userid, password)

Page 7: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Calling CAS Actions

In[3]: stat = conn.serverstatus()

In[4]: user = conn.userinfo()

In[5]: conn.help()

Page 8: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Loading Data

In[6]: out = conn.loadtable(path='…',

caslib='…')

path= file path, database table, ESP, etc.

caslib= configured CAS data connector

Page 9: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

CASTables

In[7]: tbl = out.casTable

# Or, tbl = conn.CASTable('cas.attrition')

In[8]: tbl.columninfo()

CASTable objects contain a reference to a CAS table as well as filtering and grouping options, and computed columns.

Page 10: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Page 11: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Exploring Data

In[9]: tbl.summary()

In[10]: tbl.freq(inputs='Attrition')

Actions that take a CAS table as input can be called directly on the CASTable object.

Page 12: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Page 13: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Training Analytical Models

In[11]:tbl.logistic(

target='Attrition',

inputs=['Gender', 'MaritalStatus',

'AccountAge'],

nominals = ['Gender', 'MaritalStatus']

)

Page 14: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

CAS Python

Page 15: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Pandas-style DataFrame API

In[12]: tbl.describe()

In[13]: tbl.groupby('Gender').describe()

In[14]: tbl[['Gender', 'AccountAge']].head()

Many Pandas DataFrame features are available on the CASTable objects.

Page 16: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Pandas-style Data Readers

In[15]: conn.read_csv(

'/path/to/local/file.csv')

In[16]: conn.read_sql_query(sqlconn,

'select * from foo')

Pandas data reader methods can be used on CAS connections as well.

Page 17: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Visualization

Bokeh

Page 18: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Bokeh

In[17]: from bokeh.charts import Bar

In[18]: from bokeh.plotting import show, figure

In[19]: out = tbl[['Attrition']].freq()['Frequency']

In[20]: p = Bar(out, 'FmtVar', values='Frequency', … )

In[21]: show(p)

Page 19: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Page 20: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Slice Dice

Page 21: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

By Groups

In[22]: tbl.groupby(['Origin',

'Make']).describe()

By groups work the same way that they do in Pandas.

Page 22: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Indexing

In[23]: tbl[['Make', 'Model',

'MSRP']].describe()

Selecting columns also works like in a Pandas Dataframe.

Page 23: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Indexing

In[23]: tbl[['Make', 'Model',

'MSRP']].logistic(…)

Selecting columns also populates the inputs= parameter in CAS actions.

Page 24: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Filtering

In[24]: tbl[(tbl.MSRP > 90000) &

(tbl.Cylinders < 12)].head()

CAS tables can be filtered using boolean indexing just like DataFrames.

Page 25: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Python SAS

Page 26: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

Data Step

In[24]: conn.runcode(code='''

data cars_temp;

set cars;

sqrt_MSRP = sqrt(MSRP);

MPG_avg = (MPG_city + MPG_highway) / 2;

run;

''')

The runcodeCAS action executes Data step code.

Page 27: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

SQL

In[24]: conn.fedsql.execdirect(query='''

select make, model, msrp,

mpg_highway from cars

where msrp > 80000 and

mpg_highway > 20

''')

The fedsql.execdirectCAS action executes SQL code.

Page 28: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

ODS-style Rendering

Page 29: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

#analyticsx

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r i ghts res erve d.

SWATDemo

Page 30: Using Python With SAS® Cloud Analytic Services …...Access to SAS cloud analytics from Python Results as Python objects and Pandas DataFrames Support for familiar Pandas API Issue

C o p y r ig ht © 201 6, SAS In st i tute In c. A l l r ig hts r ese rve d.

#AnalyticsX