Skip to content
This repository has been archived by the owner on Aug 25, 2024. It is now read-only.

model: scikit: Added New scikit model #280

Merged
merged 6 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- GaussianProcessRegressor
- OrthogonalMatchingPursuit
- Lars
- Ridge
- `AsyncExitStackTestCase` which instantiates and enters async and non-async
`contextlib` exit stacks. Provides temporary file creation.
### Changed
Expand Down
2 changes: 2 additions & 0 deletions docs/plugins/dffml_model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ Predicting with trained model:
| | OrthogonalMatchingPursuit | scikitomp | `scikitomp <https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.OrthogonalMatchingPursuit.html#sklearn.linear_model.OrthogonalMatchingPursuit/>`_ |
| +-------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | Lars | scikitlars | `scikitlars <https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lars.html#sklearn.linear_model.Lars/>`_ |
| +-------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | Ridge | scikitridge | `scikitridge <https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html#sklearn.linear_model.Ridge/>`_ |
+----------------+-------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Classification | KNeighborsClassifier | scikitknn | `scikitknn <https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier/>`_ |
| +-------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Expand Down
2 changes: 2 additions & 0 deletions model/scikit/dffml_model_scikit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
| | OrthogonalMatchingPursuit | scikitomp | `scikitomp <https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.OrthogonalMatchingPursuit.html#sklearn.linear_model.OrthogonalMatchingPursuit/>`_ |
| +-------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | Lars | scikitlars | `scikitlars <https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lars.html#sklearn.linear_model.Lars/>`_ |
| +-------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| | Ridge | scikitridge | `scikitridge <https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html#sklearn.linear_model.Ridge/>`_ |
+----------------+-------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Classification | KNeighborsClassifier | scikitknn | `scikitknn <https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier/>`_ |
| +-------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Expand Down
2 changes: 2 additions & 0 deletions model/scikit/dffml_model_scikit/scikit_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
RANSACRegressor,
OrthogonalMatchingPursuit,
Lars,
Ridge,
)

from dffml.util.cli.arg import Arg
Expand Down Expand Up @@ -165,6 +166,7 @@ class NoDefaultValue:
OrthogonalMatchingPursuit,
applicable_features,
),
("scikitridge", "Ridge", Ridge, applicable_features),
("scikitlars", "Lars", Lars, applicable_features),
]:

Expand Down
1 change: 1 addition & 0 deletions model/scikit/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
f"scikitgpr = {IMPORT_NAME}.scikit_models:GaussianProcessRegressorModel",
f"scikitomp = {IMPORT_NAME}.scikit_models:OrthogonalMatchingPursuitModel",
f"scikitlars = {IMPORT_NAME}.scikit_models:LarsModel",
f"scikitridge = {IMPORT_NAME}.scikit_models:RidgeModel",
]
},
)
5 changes: 3 additions & 2 deletions model/scikit/tests/test_scikit.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ async def test_02_predict(self):
elif self.MODEL_TYPE is "REGRESSION":
correct = FEATURE_DATA_REGRESSION[int(repo.src_url)][3]
self.assertGreater(
prediction, correct - (correct * 0.20)
prediction, correct - (correct * 0.40)
)
self.assertLess(prediction, correct + (correct * 0.20))
self.assertLess(prediction, correct + (correct * 0.40))


FEATURE_DATA_CLASSIFICATION = [
Expand Down Expand Up @@ -208,6 +208,7 @@ async def test_02_predict(self):
"GaussianProcessRegressor",
"OrthogonalMatchingPursuit",
"Lars",
"Ridge",
]

for clf in CLASSIFIERS:
Expand Down
96 changes: 96 additions & 0 deletions tests/integration/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,99 @@ async def test_run(self):
self.assertIn("value", results)
results = results["value"]
self.assertEqual(4, results)


class TestScikitRegression(IntegrationCLITestCase):
async def test_run(self):
self.required_plugins("dffml-model-scikit")
# Create the training data
train_filename = self.mktempfile() + ".csv"
pathlib.Path(train_filename).write_text(
inspect.cleandoc(
"""
crim,zn,indus,chas,nox,rm,age,dis,rad,tax,ptratio,b,lstat,medv
0.00632,18,2.31,0,0.538,6.575,65.2,4.09,1,296,15.3,396.9,4.98,24
0.02731,0,7.07,0,0.469,6.421,78.9,4.9671,2,242,17.8,396.9,9.14,21.6
0.02729,0,7.07,0,0.469,7.185,61.1,4.9671,2,242,17.8,392.83,4.03,34.7
0.03237,0,2.18,0,0.458,6.998,45.8,6.0622,3,222,18.7,394.63,2.94,33.4
0.06905,0,2.18,0,0.458,7.147,54.2,6.0622,3,222,18.7,396.9,5.33,36.2
"""
)
+ "\n"
)
# Create the test data
test_filename = self.mktempfile() + ".csv"
pathlib.Path(test_filename).write_text(
inspect.cleandoc(
"""
crim,zn,indus,chas,nox,rm,age,dis,rad,tax,ptratio,b,lstat,medv
0.02985,0,2.18,0,0.458,6.43,58.7,6.0622,3,222,18.7,394.12,5.21,28.7
0.08829,12.5,7.87,0,0.524,6.012,66.6,5.5605,5,311,15.2,395.6,12.43,22.9
"""
)
+ "\n"
)
# Create the prediction data
predict_filename = self.mktempfile() + ".csv"
pathlib.Path(predict_filename).write_text(
inspect.cleandoc(
"""
crim,zn,indus,chas,nox,rm,age,dis,rad,tax,ptratio,b,lstat,medv
0.14455,12.5,7.87,0,0.524,6.172,96.1,5.9505,5,311,15.2,396.9,19.15,27.1
"""
)
+ "\n"
)
# Features
features = "-model-features def:crim:float:1 def:zn:float:1 def:indus:float:1 def:chas:int:1 def:nox:float:1 def:rm:float:1 def:age:int:1 def:dis:float:1 def:rad:int:1 def:tax:float:1 def:ptratio:float:1 def:b:float:1 def:lstat:float:1".split()
# Train the model
await CLI.cli(
"train",
"-model",
"scikitridge",
*features,
"-model-predict",
"medv",
"-sources",
"training_data=csv",
"-source-filename",
train_filename,
)
# Assess accuracy
await CLI.cli(
"accuracy",
"-model",
"scikitridge",
*features,
"-model-predict",
"medv",
"-sources",
"test_data=csv",
"-source-filename",
test_filename,
)
# Ensure JSON output works as expected (#261)
with contextlib.redirect_stdout(self.stdout):
# Make prediction
await CLI._main(
"predict",
"all",
"-model",
"scikitridge",
*features,
"-model-predict",
"medv",
"-sources",
"predict_data=csv",
"-source-filename",
predict_filename,
)
results = json.loads(self.stdout.getvalue())
self.assertTrue(isinstance(results, list))
self.assertTrue(results)
results = results[0]
self.assertIn("prediction", results)
results = results["prediction"]
self.assertIn("value", results)
results = results["value"]
self.assertTrue(results is not None)