Model Slr

class dffml.model.slr.SLRModel(config: BaseConfig)[source]

Logistic Regression training one variable to predict another.

The dataset used for training

dataset.csv

f1,ans
0.1,0
0.7,1
0.6,1
0.2,0
0.8,1

Train the model

$ dffml train \
    -model slr \
    -model-features f1:float:1 \
    -model-predict ans:int:1 \
    -model-location tempdir \
    -sources f=csv \
    -source-filename dataset.csv

Assess the accuracy

$ dffml accuracy \
    -model slr \
    -model-features f1:float:1 \
    -model-predict ans:int:1 \
    -model-location tempdir \
    -features ans:int:1 \
    -sources f=csv \
    -source-filename dataset.csv \
    -scorer mse \
1.0

Make a prediction

predict.csv

f1
0.8
$ dffml predict all \
    -model slr \
    -model-features f1:float:1 \
    -model-predict ans:int:1 \
    -model-location tempdir \
    -sources f=csv \
    -source-filename predict.csv
[
    {
        "extra": {},
        "features": {
            "f1": 0.8
        },
        "key": "0",
        "last_updated": "2020-11-15T16:22:25Z",
        "prediction": {
            "ans": {
                "confidence": 0.9355670103092784,
                "value": 1
            }
        }
    }
]

Example usage of Logistic Regression using Python

slr.py

from dffml import Features, Feature, SLRModel
from dffml.noasync import train, score, predict
from dffml.accuracy import MeanSquaredErrorAccuracy

model = SLRModel(
    features=Features(Feature("f1", float, 1)),
    predict=Feature("ans", int, 1),
    location="tempdir",
)

# Train the model
train(model, "dataset.csv")

# Assess accuracy (alternate way of specifying data source)
scorer = MeanSquaredErrorAccuracy()
print("Accuracy:", score(model, scorer, Feature("ans", int, 1), "dataset.csv"))

# Make prediction
for i, features, prediction in predict(model, {"f1": 0.8, "ans": 0}):
    features["ans"] = prediction["ans"]["value"]
    print(features)
$ python slr.py
Accuracy: 0.9355670103092784
{'f1': 0.8, 'ans': 1}
CONFIG

alias of SLRModelConfig

class dffml.model.slr.SLRModelConfig(predict: dffml.feature.feature.Feature, features: dffml.feature.feature.Features, location: pathlib.Path)[source]
no_enforce_immutable()

By default, all properties of a config object are immutable. If you would like to mutate immutable properties, you must explicitly call this method using it as a context manager.

Examples

>>> from dffml import config
>>>
>>> @config
... class MyConfig:
...     C: int
>>>
>>> config = MyConfig(C=2)
>>> with config.no_enforce_immutable():
...     config.C = 1