using python with sas® cloud analytic services …...access to sas cloud analytics from python...
TRANSCRIPT
#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
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.
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
+
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+
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
#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)
#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()
#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
#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.
#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.
#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.
#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.
#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']
)
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
#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.
#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.
#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
#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)
#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.
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
#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.
#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.
#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.
#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.
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
#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.
#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.
#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
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
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