def wrap_high_level_accuracy(state): model = SLRModel( features=Features(Feature("Years", int, 1), ), predict=Feature("Salary", int, 1), location="tempdir", ) train( model, { "Years": 0, "Salary": 10 }, { "Years": 1, "Salary": 20 }, { "Years": 2, "Salary": 30 }, { "Years": 3, "Salary": 40 }, ) yield
async def test_models(self): with tempfile.TemporaryDirectory() as tempdir: # Model the HTTP API will pre-load model = SLRModel( features=Features(Feature("f1", float, 1)), predict=Feature("ans", int, 1), directory=tempdir, ) # y = m * x + b for equation SLR is solving for m = 5 b = 3 # Train the model await train(model, *[{ "f1": x, "ans": m * x + b } for x in range(0, 10)]) await accuracy( model, *[{ "f1": x, "ans": m * x + b } for x in range(10, 20)]) async with ServerRunner.patch(HTTPService.server) as tserver: cli = await tserver.start( HTTPService.server.cli( "-insecure", "-port", "0", "-models", "mymodel=slr", "-model-mymodel-directory", tempdir, "-model-mymodel-features", "f1:float:1", "-model-mymodel-predict", "ans:int:1", )) async with self.post( cli, f"/model/mymodel/predict/0", json={ f"record_{x}": { "features": { "f1": x } } for x in range(20, 30) }, ) as response: response = await response.json() records = response["records"] self.assertEqual(len(records), 10) for record in records.values(): should_be = m * record["features"]["f1"] + b prediction = record["prediction"]["ans"]["value"] percent_error = abs(should_be - prediction) / should_be self.assertLess(percent_error, 0.2)
def wrap_noasync_accuracy(state): model = SLRModel( features=Features(Feature("Years", int, 1), ), predict=Feature("Salary", int, 1), directory="tempdir", ) train( model, { "Years": 0, "Salary": 10 }, { "Years": 1, "Salary": 20 }, { "Years": 2, "Salary": 30 }, { "Years": 3, "Salary": 40 }, ) yield
def setUpClass(cls): # Create a temporary directory to store the trained model cls.model_dir = tempfile.TemporaryDirectory() # Create the training data cls.train_data = [] for x, y in TRAIN_DATA: cls.train_data.append({"X": x, "Y": y}) # Create the test data cls.test_data = [] for x, y in TEST_DATA: cls.test_data.append({"X": x, "Y": y}) # Create an instance of the model cls.model = SLRModel( directory=cls.model_dir.name, predict=Feature("Y", float, 1), features=Features(Feature("X", float, 1)), )
from dffml import Features, DefFeature from dffml.noasync import train, accuracy, predict from dffml.model.slr import SLRModel model = SLRModel( features=Features(DefFeature("f1", float, 1)), predict=DefFeature("ans", int, 1), ) # Train the model train(model, "dataset.csv") # Assess accuracy (alternate way of specifying data source) print("Accuracy:", accuracy(model, "dataset.csv")) # Make prediction for i, features, prediction in predict(model, {"f1": 0.8, "ans": 0}): features["ans"] = prediction["ans"]["value"] print(features)
async def test_scorer(self): with tempfile.TemporaryDirectory() as tempdir: model = SLRModel( features=Features(Feature("f1", float, 1)), predict=Feature("ans", int, 1), location=tempdir, ) # y = m * x + b for equation SLR is solving for m = 5 b = 3 # Train the model await train(model, *[{ "f1": x, "ans": m * x + b } for x in range(0, 10)]) source = JSONSource( filename=pathlib.Path(tempdir, "source.json"), allowempty=True, readwrite=True, ) # Record the source will have in it await save( source, *[ Record( str(i), data={"features": { "f1": x, "ans": (m * x) + b }}, ) for i, x in enumerate(range(10, 20)) ], ) async with ServerRunner.patch(HTTPService.server) as tserver: cli = await tserver.start( HTTPService.server.cli( "-insecure", "-port", "0", "-models", "mymodel=slr", "-model-mymodel-location", tempdir, "-model-mymodel-features", "f1:float:1", "-model-mymodel-predict", "ans:int:1", "-features", "ans:int:1", "-sources", "mysource=json", "-source-mysource-filename", str(source.config.filename), "-scorers", "myscorer=mse", )) async with self.post(cli, "/scorer/myscorer/mymodel/score", json=["mysource"]) as r: self.assertEqual(await r.json(), {"accuracy": 0.0})
import asyncio from dffml import train, Features, DefFeature from dffml.operation.output import GetSingle from dffml.df.memory import MemoryOrchestrator from dffml.operation.mapping import create_mapping from dffml.operation.preprocess import literal_eval from dffml.df.types import DataFlow, Input, InputFlow from dffml.operation.io import AcceptUserInput, print_output from dffml.operation.model import model_predict, ModelPredictConfig from dffml.model.slr import SLRModel slr_model = SLRModel( features=Features(DefFeature("Years", int, 1), ), predict=DefFeature("Salary", int, 1), ) # This Dataflow takes input from stdio using `AcceptUserInput` # operation. The string input which corresponds to feature `Years` # is converted to `int`/`float` by # `literal_eval` operation. # `create_mapping` operation creates a mapping using the numeric output # of `literal_eval` eg. {"Years":34}. # The mapping is then fed to `model_predict` operation which # uses the `slr` model trained above to make prediction. The prediction is then printed to # stdout using `print_output` operation. dataflow = DataFlow( operations={ "get_user_input": AcceptUserInput, "literal_eval_input": literal_eval, "create_feature_map": create_mapping,