scikitlearn仪器工具
sklearn-instrumentation的Python项目详细描述
scikit学习模型的通用仪器工具。^{tt1$与包{tt1}兼容{tt1}与包}兼容{tt1}学习{tt1}。在
检测将装饰器应用于BaseEstimator-派生类或实例的方法。默认情况下,检测程序将检测应用于以下方法(除非这些方法是实例的属性):
- 适合
- 预测
- 预测概率
- 转换
- _适合
- _预测
- _预测概率
- _转换
sklearn instrumentation支持完整的sklearn兼容包的插装,以及模型的递归插装(元估计器,如Pipeline,甚至是单个估计器,如RandomForestClassifier)
安装
sklearn instrumentation包在pypi上可用,可以使用pip安装
pip install sklearn-instrumentation
成套仪表
插入任何具有BaseEstimator-派生类的sklearn兼容包。在
^{pr2}$完整示例:
importloggingfromsklearn.datasetsimportload_irisfromsklearn.decompositionimportPCAfromsklearn.ensembleimportRandomForestClassifierfromsklearn.pipelineimportFeatureUnionfromsklearn.pipelineimportPipelinefromsklearn.preprocessingimportStandardScalerfromsklearn_instrumentationimportSklearnInstrumentorfromsklearn_instrumentation.instruments.loggingimportTimeElapsedLoggerlogging.basicConfig(level=logging.INFO)# Create an instrumentor and instrument sklearninstrumentor=SklearnInstrumentor(instrument=TimeElapsedLogger())instrumentor.instrument_packages(["sklearn"])# Create a toy model for classificationss=StandardScaler()pca=PCA(n_components=3)rf=RandomForestClassifier()classification_model=Pipeline(steps=[("fu",FeatureUnion(transformer_list=[("ss",ss),("pca",pca),]),),("rf",rf),])X,y=load_iris(return_X_y=True)# Observe loggingclassification_model.fit(X,y)# INFO:sklearn_instrumentation.instruments.logging:Pipeline.fit starting.# INFO:sklearn_instrumentation.instruments.logging:Pipeline._fit starting.# INFO:sklearn_instrumentation.instruments.logging:StandardScaler.fit starting.# INFO:sklearn_instrumentation.instruments.logging:StandardScaler.fit elapsed time: 0.0006406307220458984 seconds# INFO:sklearn_instrumentation.instruments.logging:StandardScaler.transform starting.# INFO:sklearn_instrumentation.instruments.logging:StandardScaler.transform elapsed time: 0.0001430511474609375 seconds# INFO:sklearn_instrumentation.instruments.logging:PCA._fit starting.# INFO:sklearn_instrumentation.instruments.logging:PCA._fit elapsed time: 0.0006711483001708984 seconds# INFO:sklearn_instrumentation.instruments.logging:Pipeline._fit elapsed time: 0.0026731491088867188 seconds# INFO:sklearn_instrumentation.instruments.logging:BaseForest.fit starting.# INFO:sklearn_instrumentation.instruments.logging:BaseForest.fit elapsed time: 0.1768970489501953 seconds# INFO:sklearn_instrumentation.instruments.logging:Pipeline.fit elapsed time: 0.17983102798461914 seconds# Observe loggingclassification_model.predict(X)# INFO:sklearn_instrumentation.instruments.logging:Pipeline.predict starting.# INFO:sklearn_instrumentation.instruments.logging:FeatureUnion.transform starting.# INFO:sklearn_instrumentation.instruments.logging:StandardScaler.transform starting.# INFO:sklearn_instrumentation.instruments.logging:StandardScaler.transform elapsed time: 0.00024509429931640625 seconds# INFO:sklearn_instrumentation.instruments.logging:_BasePCA.transform starting.# INFO:sklearn_instrumentation.instruments.logging:_BasePCA.transform elapsed time: 0.0002181529998779297 seconds# INFO:sklearn_instrumentation.instruments.logging:FeatureUnion.transform elapsed time: 0.0012080669403076172 seconds# INFO:sklearn_instrumentation.instruments.logging:ForestClassifier.predict starting.# INFO:sklearn_instrumentation.instruments.logging:ForestClassifier.predict_proba starting.# INFO:sklearn_instrumentation.instruments.logging:ForestClassifier.predict_proba elapsed time: 0.013531208038330078 seconds# INFO:sklearn_instrumentation.instruments.logging:ForestClassifier.predict elapsed time: 0.013692140579223633 seconds# INFO:sklearn_instrumentation.instruments.logging:Pipeline.predict elapsed time: 0.015219926834106445 seconds# Remove instrumentationinstrumentor.uninstrument_packages(["sklearn"])# Observe no loggingclassification_model.predict(X)
机器学习模型仪器
仪器任何sklearn兼容的训练估计器或元估计器。在
fromsklearn_instrumentationimportSklearnInstrumentorinstrumentor=SklearnInstrumentor(instrument=my_instrument)instrumentor.instrument_estimator(estimator=my_ml_pipeline)
示例:
importloggingfromsklearn.datasetsimportload_irisfromsklearn_instrumentationimportSklearnInstrumentorfromsklearn_instrumentation.instruments.loggingimportTimeElapsedLoggerfromsklearn.ensembleimportRandomForestClassifierlogging.basicConfig(level=logging.INFO)# Train a classifierX,y=load_iris(return_X_y=True)rf=RandomForestClassifier()rf.fit(X,y)# Create an instrumentor which decorates BaseEstimator methods with# logging output when entering and exiting methods, with time elapsed logged# on exit.instrumentor=SklearnInstrumentor(instrument=TimeElapsedLogger())# Apply the decorator to all BaseEstimators in each of these librariesinstrumentor.instrument_estimator(rf)# Observe the logging outputrf.predict(X)# INFO:sklearn_instrumentation.instruments.logging:ForestClassifier.predict starting.# INFO:sklearn_instrumentation.instruments.logging:ForestClassifier.predict_proba starting.# INFO:sklearn_instrumentation.instruments.logging:ForestClassifier.predict_proba elapsed time: 0.014165163040161133 seconds# INFO:sklearn_instrumentation.instruments.logging:ForestClassifier.predict elapsed time: 0.014327764511108398 seconds# Remove the decorator from all BaseEstimators in each of these librariesinstrumentor.uninstrument_estimator(rf)# No more loggingrf.predict(X)
仪器仪表
这个包附带了一些记录X或执行时间的工具。您可以通过创建一个decorator来创建自己的工具,遵循这个模式
fromfunctoolsimportwrapsdefmy_instrumentation(func,**dkwargs):"""Wrap an estimator method with instrumentation. :param func: The method to be instrumented. :param dkwargs: Decorator kwargs, which can be passed to the decorator at decoration time. For estimator instrumentation this allows different parametrizations for each ml model. """@wraps(func)defwrapper(*args,**kwargs):"""Wrapping function. :param args: The args passed to methods, typically just ``X`` and/or ``y`` :param kwargs: The kwargs passed to methods, usually weights or other params """# Code goes here before execution of the estimator methodretval=func(*args,**kwargs)# Code goes here after execution of the estimator methodreturnretvalreturnwrapper
要创建有状态的工具,请使用带有__call__方法的类来实现装饰器:
fromfunctoolsimportwrapsfromsklearn_instrumentation.instruments.baseimportBaseInstrumentclassMyInstrument(BaseInstrument)def__init__(self,*args,**kwargs):# handle any statefulness herepassdef__call__(self,func,**dkwargs):"""Wrap an estimator method with instrumentation. :param func: The method to be instrumented. :param dkwargs: Decorator kwargs, which can be passed to the decorator at decoration time. For estimator instrumentation this allows different parametrizations for each ml model. """@wraps(func)defwrapper(*args,**kwargs):"""Wrapping function. :param args: The args passed to methods, typically just ``X`` and/or ``y`` :param kwargs: The kwargs passed to methods, usually weights or other params """# Code goes here before execution of the estimator methodretval=func(*args,**kwargs)# Code goes here after execution of the estimator methodreturnretvalreturnwrapper
要通过不同ml模型的kwargs:
instrumentor=SklearnInstrumentor(instrument=my_instrument)instrumentor.instrument_estimator(estimator=ml_model_1,instrument_kwargs={"name":"awesome_model"})instrumentor.instrument_estimator(estimator=ml_model_2,instrument_kwargs={"name":"better_model"})
- 项目
标签: