SvmModel __init__¶
-
__init__
(self, name=None)¶ Create a ‘new’ instance of a Support Vector Machine model.
Parameters: name : unicode (default=None)
User supplied name.
Returns: : Model
A new instance of SvmModel
Support Vector Machine [R6] is a supervised algorithm used to perform binary classification. A Support Vector Machine constructs a high dimensional hyperplane which is said to achieve a good separation when a hyperplane has the largest distance to the nearest training-data point of any class. This model runs the MLLib implementation of SVM [R7] with SGD [R8] optimizer. The SVMWithSGD model is initialized, trained on columns of a frame, used to predict the labels of observations in a frame, and tests the predicted labels against the true labels. During testing, labels of the observations are predicted and tested against the true labels using built-in binary Classification Metrics.
footnotes
[R6] https://en.wikipedia.org/wiki/Support_vector_machine [R7] https://spark.apache.org/docs/1.5.0/mllib-linear-methods.html#linear-support-vector-machines-svms [R8] https://en.wikipedia.org/wiki/Stochastic_gradient_descent Examples
Consider the following model trained and tested on the sample data set in frame ‘frame’.
Consider the following frame containing three columns.
>>> frame.inspect() [#] data label ================= [0] -48.0 1 [1] -75.0 1 [2] -63.0 1 [3] -57.0 1 [4] 73.0 0 [5] -33.0 1 [6] 100.0 0 [7] -54.0 1 [8] 78.0 0 [9] 48.0 0
>>> model = ta.SvmModel() [===Job Progress===] >>> train_output = model.train(frame, 'label', ['data']) [===Job Progress===]
>>> predicted_frame = model.predict(frame, ['data']) [===Job Progress===] >>> predicted_frame.inspect() [#] data label predicted_label ================================== [0] -48.0 1 1 [1] -75.0 1 1 [2] -63.0 1 1 [3] -57.0 1 1 [4] 73.0 0 0 [5] -33.0 1 1 [6] 100.0 0 0 [7] -54.0 1 1 [8] 78.0 0 0 [9] 48.0 0 0
>>> test_metrics = model.test(predicted_frame, 'predicted_label') [===Job Progress===]
>>> test_metrics Precision: 1.0 Recall: 1.0 Accuracy: 1.0 FMeasure: 1.0 Confusion Matrix: Predicted_Pos Predicted_Neg Actual_Pos 7 0 Actual_Neg 0 7
>>> 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}]}'