PWKCLF#
- class mlquantify.classification.methods.PWKCLF(alpha=1, n_neighbors=10, algorithm='auto', metric='euclidean', leaf_size=30, p=2, metric_params=None, n_jobs=None)[source]#
Learner based on k-Nearest Neighbors (KNN) to use in the PWK method.
This classifier adjusts the influence of neighbors using class weights derived from the
alphaparameter. Thealphaparameter controls the influence of class imbalance.- Parameters:
- alphafloat, default=1
Controls the influence of class imbalance. Must be >= 1.
- n_neighborsint, default=10
Number of neighbors to use.
- algorithm{‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, default=’auto’
Algorithm to compute nearest neighbors.
- metricstr, default=’euclidean’
Distance metric to use.
- leaf_sizeint, default=30
Leaf size passed to the tree-based algorithms.
- pint, default=2
Power parameter for the Minkowski metric.
- metric_paramsdict, optional
Additional keyword arguments for the metric function.
- n_jobsint, optional
Number of parallel jobs to run for neighbors search.
Examples
>>> from sklearn.datasets import load_breast_cancer >>> from sklearn.model_selection import train_test_split >>> from mlquantify.methods.aggregative import PWK >>> from mlquantify.utils.general import get_real_prev >>> from mlquantify.classification import PWKCLF >>> >>> # Load dataset >>> features, target = load_breast_cancer(return_X_y=True) >>> >>> # Split into training and testing sets >>> X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.3, random_state=32) >>> >>> # Create and configure the PWKCLF learner >>> learner = PWKCLF(alpha=1, n_neighbors=10) >>> >>> # Create the PWK quantifier >>> model = PWK(learner=learner) >>> >>> # Train the model >>> model.fit(X_train, y_train) >>> >>> # Predict prevalences >>> y_pred = model.predict(X_test) >>> >>> # Display results >>> print("Real:", get_real_prev(y_test)) >>> print("PWK:", y_pred)
- fit(X, y)[source]#
Fit the PWKCLF model to the training data.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Training features.
- yarray-like of shape (n_samples,)
Training labels.
- Returns:
- selfobject
The fitted instance.
- get_metadata_routing()[source]#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- get_params(deep=True)[source]#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- predict(X)[source]#
Predict class labels for samples in X.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Input data to predict.
- Returns:
- y_predarray of shape (n_samples,)
Predicted class labels.
- set_params(**params)[source]#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.