Models LassoModel


class LassoModel

Entity LassoModel

Attributes

last_read_date Read-only property - Last time this model’s data was accessed.
name Set or get the name of the model object.
status Read-only property - Current model life cycle status.

Methods

__init__(self[, name, _info]) Create a ‘new’ instance of a Lasso Model.
predict(self, frame[, observation_columns]) Predict the labels for the data points
publish(self) Creates a tar file that will be used as input to the scoring engine
test(self, frame, label_column[, observation_columns]) Predict test frame labels and return metrics.
train(self, frame, value_column, observation_columns[, initial_weights, ...]) Train Lasso Model
__init__(self, name=None)

Create a ‘new’ instance of a Lasso Model.

Parameters:

name : unicode (default=None)

User supplied name.

Returns:

: Model

A new instance of a LassoModel

lasso (least absolute shrinkage and selection operator) (also Lasso or LASSO)[R17]_ is a regression analysis method that performs both variable selection and regularization in order to enhance the prediction accuracy and interpretability of the statistical model it produces.

footnotes

[R17]https://en.wikipedia.org/wiki/Lasso

Examples

Consider the following model trained and tested on the sample data set in frame ‘frame’. Consider the following frame containing two columns.

>>> frame.inspect()
[#]  x1   y
===============
[0]  0.0    0.0
[1]  1.0    2.5
[2]  2.0    5.0
[3]  3.0    7.5
[4]  4.0   10.0
[5]  5.0   12.5
[6]  6.0   13.0
[7]  7.0  17.15
[8]  8.0   18.5
[9]  9.0   23.5
>>> model = ta.LassoModel()
[===Job Progress===]
>>> results = model.train(frame, 'y', ['x1'])
[===Job Progress===]
>>> results
{u'intercept': 0.0, u'weights': [2.4387285895043913]}
>>> predicted_frame = model.predict(frame)
[===Job Progress===]
>>> predicted_frame.inspect()
[#]  x1   y      predicted_value
================================
[0]  0.0    0.0              0.0
[1]  1.0    2.5     2.4387285895
[2]  2.0    5.0    4.87745717901
[3]  3.0    7.5    7.31618576851
[4]  4.0   10.0    9.75491435802
[5]  5.0   12.5    12.1936429475
[6]  6.0   13.0     14.632371537
[7]  7.0  17.15    17.0711001265
[8]  8.0   18.5     19.509828716
[9]  9.0   23.5    21.9485573055
>>> test_metrics = model.test(predicted_frame, 'predicted_value')
[===Job Progress===]
>>> test_metrics
{u'mean_squared_error': 0.0, u'r_2': 1.0, u'mean_absolute_error': 0.0, u'root_mean_squared_error': 0.0}
>>> model.publish()
[===Job Progress===]

Take the path to the published model and run it in the Scoring Engine

>>> import requests
>>> headers = {'Content-type': 'application/json', 'Accept': 'application/json,text/plain'}

Posting a request to get the metadata about the model

>>> r =requests.get('http://mymodel.demotrustedanalytics.com/v2/metadata')
>>> r.text
u'{"model_details":{"model_type":"SVM with SGD Model","model_class":"org.trustedanalytics.atk.scoring.models.SVMWithSGDScoreModel","model_reader":"org.trustedanalytics.atk.scoring.models.SVMWithSGDModelReaderPlugin","custom_values":{}},"input":[{"name":"data","value":"Double"}],"output":[{"name":"data","value":"Double"},{"name":"Prediction","value":"Double"}]}'

Posting a request to version 1 of Scoring Engine supporting strings for requests and response:

>>> r = requests.post('http://mymodel.demotrustedanalytics.com/v1/score?data=-48.0', headers=headers)
>>> r.text
u'1.0'

Posting a request to version 1 with multiple records to score:

>>> r = requests.post('http://mymodel.demotrustedanalytics.com/v1/score?data=-48.0&data=73.0', headers=headers)
>>> r.text
u'1.0,0.0'

Posting a request to version 2 of Scoring Engine supporting Json for requests and responses.

>>> r = requests.post("http://mymodel.demotrustedanalytics.com/v2/score", json={"records": [{"data": -48.0}]})
>>> r.text
u'{"data":[{"data":-48.0,"Prediction":1.0}]}'

Posting a request to version 2 with multiple records to score:

>>> r = requests.post("http://mymodel.demotrustedanalytics.com/v2/score", json={"records": [{"data": -48.0},{"data": 73.0}]})
>>> r.text
u'{"data":[{"data":-48.0,"Prediction":1.0},{"data":73.0,"Prediction":0.0}]}'