Добавлены параметры моделей, веса блоков и руководство по настройке

This commit is contained in:
2026-10-25 16:08:00 +03:00
parent 10e2444d23
commit 5a4a19e0df
7 changed files with 537 additions and 11 deletions

View File

@@ -45,6 +45,9 @@ class FeatureConfig:
lsa_components: int = 100
stylo_scaler: str = "standard" # standard | minmax | none
stylo_normalize: bool = True
# Веса блоков в FeatureUnion: stylo, char, pos, lsa_word, lsa_pos и т.д.
# Пустой словарь означает равный вес блоков.
block_weights: dict[str, float] = field(default_factory=dict)
include_stylo: bool = True
include_lsa: bool = True
char_max_features: int = 5000
@@ -150,7 +153,18 @@ def build_feature_union(config: FeatureConfig) -> Any:
if len(branches) == 1:
return branches[0][1]
return FeatureUnion(branches)
union_kwargs: dict[str, Any] = {}
if config.block_weights:
branch_names = {name for name, _ in branches}
unknown = set(config.block_weights) - branch_names
if unknown:
raise ValueError(
f"Неизвестные блоки в block_weights: {sorted(unknown)}. "
f"Доступны: {sorted(branch_names)}"
)
union_kwargs["transformer_weights"] = dict(config.block_weights)
return FeatureUnion(branches, **union_kwargs)
def make_classifier(name: str, **params: Any) -> Any:
@@ -159,19 +173,24 @@ def make_classifier(name: str, **params: Any) -> Any:
strategy = params.pop("strategy", "most_frequent")
return DummyClassifier(strategy=strategy)
if name == "multinomial_nb":
return MultinomialNB(alpha=params.pop("alpha", 1.0))
return MultinomialNB(
alpha=params.pop("alpha", 1.0),
fit_prior=params.pop("fit_prior", True),
)
if name == "logreg":
return LogisticRegression(
C=params.pop("C", 1.0),
solver="lbfgs",
max_iter=5000,
class_weight="balanced",
solver=params.pop("solver", "lbfgs"),
max_iter=params.pop("max_iter", 5000),
class_weight=params.pop("class_weight", "balanced"),
)
if name == "linear_svc":
return LinearSVC(
C=params.pop("C", 1.0),
class_weight="balanced",
max_iter=20_000,
loss=params.pop("loss", "squared_hinge"),
tol=params.pop("tol", 1e-4),
class_weight=params.pop("class_weight", "balanced"),
max_iter=params.pop("max_iter", 20_000),
dual="auto",
)
if name == "random_forest":
@@ -179,7 +198,9 @@ def make_classifier(name: str, **params: Any) -> Any:
n_estimators=params.pop("n_estimators", 400),
max_depth=params.pop("max_depth", None),
min_samples_leaf=params.pop("min_samples_leaf", 1),
class_weight="balanced_subsample",
min_samples_split=params.pop("min_samples_split", 2),
max_features=params.pop("max_features", "sqrt"),
class_weight=params.pop("class_weight", "balanced_subsample"),
n_jobs=-1,
random_state=params.pop("random_state", 42),
)
@@ -188,7 +209,9 @@ def make_classifier(name: str, **params: Any) -> Any:
n_estimators=params.pop("n_estimators", 400),
max_depth=params.pop("max_depth", None),
min_samples_leaf=params.pop("min_samples_leaf", 1),
class_weight="balanced",
min_samples_split=params.pop("min_samples_split", 2),
max_features=params.pop("max_features", "sqrt"),
class_weight=params.pop("class_weight", "balanced"),
n_jobs=-1,
random_state=params.pop("random_state", 42),
)