问题描述
考虑使用以下 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
};
});
}