机器学习加python项目_使用高级python技术记录您的机器学习项目,第1部分装饰器...

机器学习加python项目

Python decorators can be extremely useful in reducing repetitive code in your projects. Even though the syntax can be quite confusing at first, the payload can be extremely useful by providing a strong and robust boilerplate for your code.

Python装饰器在减少项目中的重复代码方面非常有用。 尽管起初语法可能很混乱,但是有效负载通过为代码提供强大而健壮的样板可以非常有用。

Here is a quick and simple example of a decorator:

这是装饰器的简单示例:

The output of the above code would read “Cat Says Meow”. The sound_maker function encloses the animal function, lets the animal function execute and decorates it with the print statement “Says Meow”.

上面代码的输出将显示为“猫说喵”。 sound_maker函数将动物函数括起来,让动物函数执行并用打印语句“ Says Meow”装饰它。

Now, to avoid writing the below to decorate any function,

现在,为了避免编写以下内容来修饰任何功能,

wrapped_object = sound_maker(animal)

one can turn to the python’s syntactic sugar syntax “@” sign. Then the below can be converted to:

可以转向python的语法糖语法“ @”符号。 然后可以将以下内容转换为:

This code would read the same as the previously mentioned one, but with less code overhead!

这段代码的读法与前面提到的相同,但是代码开销却更少!

Let us now step back and see how we can take advantage decorators to streamline our machine learning workflows.

现在让我们退后一步,看看如何利用装饰器简化我们的机器学习工作流程。

Scikit-learn package contains a collection of tools for building machine learning projects. It includes a huge variety of classifiers, regressors, clustering algorithms, sample datasets and more! The package is also well known for its standard and easy fit/predict methods. If you’ve worked with Scikit-learn before, chances are that you have used fit/predict on multiple models inside your projects to find the right model for your data. Let’s take a look at an example of doing this using the “iris dataset”.

Scikit-learn软件包包含用于构建机器学习项目的工具集合。 它包括各种各样的分类器,回归器,聚类算法,样本数据集等等! 该包装还以其标准和简便的装配/预测方法而闻名。 如果您以前使用过Scikit-learn ,则很有可能在项目中的多个模型上使用了拟合/预测来为数据找到合适的模型。 让我们看一个使用“ iris数据集”执行此操作的示例。

Here’s a preview of x:

这是x的预览:

Image for post

For all of the scikit-learn users out there, how many of us have been guilty of the below?

对于所有的scikit学习用户来说,我们当中有多少人犯了以下罪行?

Obviously, the above code involves a lot of repetition and can get really confusing when you are differentiating what score came from where (especially if you are fitting more models for comparison or if you are using Jupyter Notebooks! ). We could really use some custom functions and decorators to log this activity ;)

显然,以上代码涉及很多重复,当您区分分数来自何处时(尤其是如果您要拟合更多模型进行比较或使用Jupyter Notebooks时),可能会使您感到非常困惑。 我们真的可以使用一些自定义函数和装饰器来记录此活动;)

First let us create a custom function that will do the meat of fitting/predicting on our data:

首先,让我们创建一个自定义函数,以对数据进行拟合/预测:

Now let’s create a model registry decorator!

现在,让我们创建一个模型注册表装饰器!

Let’s break this down a bit… This decorator takes in a function, passes it into a wrapper (notice this is a special decorator too! Read more about it here) that takes in args and kwargs of the function to be decorated. Those args and kwargs are then used to run the function, as well as to log the function’s outputs into a dictionary “registry”.

让我们来分解一下……这个装饰器接受一个函数,并将其传递给一个包装器(注意,这也是一个特殊的装饰器!在此处了解更多信息),其中包含要装饰的函数的args和kwargs。 然后,这些arg和kwarg用于运行该函数,并将该函数的输出记录到字典“注册表”中。

Now that we’ve defined this model_registry, we can use it to decorate our fitter function:

现在我们已经定义了这个model_registry,我们可以用它来装饰我们的装配函数:

And when we run the below code,

当我们运行以下代码时,

we get the below output when printing the registry dictionary!

打印注册表字典时,我们得到以下输出!

Image for post

Voila! Keeping track of fitted models is now easier!

瞧! 现在更容易跟踪拟合的模型!

However, we can take this a step further. This time, let’s modify our decorator definition to log the metrics of our models into files. To do this, we’ll first define a supplementary factory function that will act like a python context manager and will write our files for us. We’ll do this using the contextmanager decorator! (Read more about it here).

但是,我们可以更进一步。 这次,让我们修改装饰器定义,以将模型的指标记录到文件中。 为此,我们将首先定义一个补充工厂函数,该函数将充当python上下文管理器,并为我们编写文件。 我们将使用contextmanager装饰器完成此操作! (在此处了解更多信息)。

Then we’ll correct our model_registry decorator to include this factory function:

然后,我们将更正我们的model_registry装饰器,使其包含以下工厂函数:

This decorator will now register our models into a registry dictionary, then create a classification table for the model’s predicted outputs vs. true outputs and will write that table to a text file. All you have to do is run the below!

现在,此修饰器会将我们的模型注册到注册表字典中,然后为模型的预测输出与真实输出创建分类表,并将该表写入文本文件。 您所要做的就是运行以下内容!

This can of course be taken further by adding other metrics and information regarding the models to the files. But…that’s a story for another article!

当然,可以通过将其他有关模型的度量和信息添加到文件中来进一步解决此问题。 但这是另一篇文章的故事!

Hope you all enjoyed learning about decorators and using them to streamline you machine learning experiments!

希望大家都喜欢学习装饰器,并使用它们来简化您的机器学习实验!

翻译自: https://medium.com/swlh/documenting-your-machine-learning-projects-using-advanced-python-techniques-part-1-decorators-b54b356eef86

机器学习加python项目

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值