Skip to content

Instantly share code, notes, and snippets.

@mhash1m
Created March 1, 2020 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhash1m/de43bda88fad56c5278f792d0257cb1f to your computer and use it in GitHub Desktop.
Save mhash1m/de43bda88fad56c5278f792d0257cb1f to your computer and use it in GitHub Desktop.
from dffml import Features, DefFeature
from dffml.noasync import train, accuracy, predict
from dffml_model_scikit import LinearRegressionModel
def highLevelApi_example(feature_def, pred_feature_def, train_data, accuracy_data, prediction_data):
"""
.. doctest::
>>> highLevelApi_example(Features(
... DefFeature("Years", int, 1),
... DefFeature("Expertise", int, 1),
... DefFeature("Trust", float, 1),
... ), DefFeature("Salary", int, 1),
... [
... {"Years": 0, "Expertise": 1, "Trust": 0.2, "Salary": 10},
... {"Years": 1, "Expertise": 3, "Trust": 0.4, "Salary": 20},
... {"Years": 2, "Expertise": 5, "Trust": 0.6, "Salary": 30},
... {"Years": 3, "Expertise": 7, "Trust": 0.8, "Salary": 40},
... ],
... [
... {"Years": 4, "Expertise": 9, "Trust": 1.0, "Salary": 50},
... {"Years": 5, "Expertise": 11, "Trust": 1.2, "Salary": 60},
... ],
... [
... {"Years": 6, "Expertise": 13, "Trust": 1.4},
... {"Years": 7, "Expertise": 15, "Trust": 1.6},
... ]
... )
Accuracy: 1.0
{'Salary': {'confidence': 1.0, 'value': 70.0}}
{'Salary': {'confidence': 1.0, 'value': 80.0}}
"""
## Creating a model
model = LinearRegressionModel(
features= feature_def
, predict=pred_feature_def
)
## train_data, accuracy_data and prediction_data are lists of dictionaries here.
## Training our model
train(model, *train_data)
## Checking our model's accuracy
print("Accuracy:", accuracy(model, *accuracy_data,))
## Making predictions on on new data
for i, features, prediction in predict(
model,
*prediction_data
):
print(prediction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment