微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以安装 sklearn 管道的单独部分?

如何解决是否可以安装 sklearn 管道的单独部分?

考虑使用以下 sklearn Pipeline

pipeline = make_pipeline(
    TfidfVectorizer(),LinearRegression()
)

我对 TfidfVectorizer 进行了预训练,因此当我调用 pipeline.fit(X,y) 时,我只想安装 LinearRegression,而不想改装 TfidfVectorizer

我可以提前应用转换并在转换后的数据上拟合 LinearRegression,但是在我的项目中我有很多转换器在管道中,其中一些经过预训练,有些没有,所以我正在寻找一种不围绕 sklearn 估计器编写另一个包装器并保持在一个 Pipeline 对象范围内的方法

在我看来,它应该是 estimators 对象中的一个参数,代表在调用 .fit() 时不重新拟合对象(如果对象已经拟合)。

解决方法

查看“内存”参数。它缓存来自管道的转换器。

https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.make_pipeline.html

pipeline = make_pipeline(
    TfidfVectorizer(),LinearRegression(),memory='cache_directory'
)
,

您可以通过如下定义管道来仅找到回归量:

pipeline = make_pipeline(steps = [
    ('vectorizer',TfidfVectorizer()),('regressor',LinearRegression())
])

然后

pipeline['regressor']

应该只给你回归量。

,

您只能使用此技巧来安装变压器一次

// Get list of domains and audience from the config
     var authorities = Configuration["Auth:Domain"].Split(',').Distinct().ToList();
     var audience = Configuration["Auth:Audience"];
     // Add default empty schema schema selection policy
     var authenticationBuilder = services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(
         options =>
         {
             // forward to corresponding schema based on token's issuer 
             // this will read the token and check the token issues,if the token issuer is registered in config then redirect to that schema
             options.ForwardDefaultSelector = context =>
             {
                 string authorization = context.Request.Headers[HeaderNames.Authorization];

                 if (!string.IsNullOrEmpty(authorization))
                 {
                     if (authorization.StartsWith("Bearer ",StringComparison.OrdinalIgnoreCase))
                     {
                         var token = authorization.Substring("Bearer ".Length).Trim();

                         var jwtHandler = new JwtSecurityTokenHandler();
                         if (jwtHandler.CanReadToken(token))
                         {
                             var jwtToken = jwtHandler.ReadJwtToken(token);
                             if (authorities.Contains(jwtToken.Issuer))
                                 return jwtToken.Issuer;
                         }
                     }
                 }
                 return null;
             };
         });

     // Register all configured schemas 
     foreach (var auth in authorities)
     {
         authenticationBuilder.AddJwtBearer(auth,options =>
         {

             options.SaveToken = true;
             options.Audience = audience;
             options.Authority = auth;
             options.TokenValidationParameters = new TokenValidationParameters
             {
                 NameClaimType = "sub",ValidateIssuer = true,ValidateAudience = true,ValidateLifetime = true,RequireSignedTokens = true,ValidateIssuerSigningKey = true
             };
         });

     }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。