QuadraticDiscriminantAnalysis#
- class sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis(*, priors=None, reg_param=0.0, store_covariance=False, tol=0.0001)[source]#
Quadratic Discriminant Analysis.
A classifier with a quadratic decision boundary, generated by fitting class conditional densities to the data and using Bayes’ rule.
The model fits a Gaussian density to each class.
Added in version 0.17.
For a comparison between
QuadraticDiscriminantAnalysis
andLinearDiscriminantAnalysis
, see Linear and Quadratic Discriminant Analysis with covariance ellipsoid.Read more in the User Guide.
- Parameters:
- priorsarray-like of shape (n_classes,), default=None
Class priors. By default, the class proportions are inferred from the training data.
- reg_paramfloat, default=0.0
Regularizes the per-class covariance estimates by transforming S2 as
S2 = (1 - reg_param) * S2 + reg_param * np.eye(n_features)
, where S2 corresponds to thescaling_
attribute of a given class.- store_covariancebool, default=False
If True, the class covariance matrices are explicitly computed and stored in the
self.covariance_
attribute.Added in version 0.17.
- tolfloat, default=1.0e-4
Absolute threshold for the covariance matrix to be considered rank deficient after applying some regularization (see
reg_param
) to eachSk
whereSk
represents covariance matrix for k-th class. This parameter does not affect the predictions. It controls when a warning is raised if the covariance matrix is not full rank.Added in version 0.17.
- Attributes:
- covariance_list of len n_classes of ndarray of shape (n_features, n_features)
For each class, gives the covariance matrix estimated using the samples of that class. The estimations are unbiased. Only present if
store_covariance
is True.- means_array-like of shape (n_classes, n_features)
Class-wise means.
- priors_array-like of shape (n_classes,)
Class priors (sum to 1).
- rotations_list of len n_classes of ndarray of shape (n_features, n_k)
For each class k an array of shape (n_features, n_k), where
n_k = min(n_features, number of elements in class k)
It is the rotation of the Gaussian distribution, i.e. its principal axis. It corresponds toV
, the matrix of eigenvectors coming from the SVD ofXk = U S Vt
whereXk
is the centered matrix of samples from class k.- scalings_list of len n_classes of ndarray of shape (n_k,)
For each class, contains the scaling of the Gaussian distributions along its principal axes, i.e. the variance in the rotated coordinate system. It corresponds to
S^2 / (n_samples - 1)
, whereS
is the diagonal matrix of singular values from the SVD ofXk
, whereXk
is the centered matrix of samples from class k.- classes_ndarray of shape (n_classes,)
Unique class labels.
- n_features_in_int
Number of features seen during fit.
Added in version 0.24.
- feature_names_in_ndarray of shape (
n_features_in_
,) Names of features seen during fit. Defined only when
X
has feature names that are all strings.Added in version 1.0.
See also
LinearDiscriminantAnalysis
Linear Discriminant Analysis.
Examples
>>> from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis >>> import numpy as np >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) >>> y = np.array([1, 1, 1, 2, 2, 2]) >>> clf = QuadraticDiscriminantAnalysis() >>> clf.fit(X, y) QuadraticDiscriminantAnalysis() >>> print(clf.predict([[-0.8, -1]])) [1]
- decision_function(X)[source]#
Apply decision function to an array of samples.
The decision function is equal (up to a constant factor) to the log-posterior of the model, i.e.
log p(y = k | x)
. In a binary classification setting this instead corresponds to the differencelog p(y = 1 | x) - log p(y = 0 | x)
. See Mathematical formulation of the LDA and QDA classifiers.- Parameters:
- Xarray-like of shape (n_samples, n_features)
Array of samples (test vectors).
- Returns:
- Cndarray of shape (n_samples,) or (n_samples, n_classes)
Decision function values related to each class, per sample. In the two-class case, the shape is (n_samples,), giving the log likelihood ratio of the positive class.
- fit(X, y)[source]#
Fit the model according to the given training data and parameters.
Changed in version 0.19:
store_covariances
has been moved to main constructor asstore_covariance
Changed in version 0.19:
tol
has been moved to main constructor.- Parameters:
- Xarray-like of shape (n_samples, n_features)
Training vector, where
n_samples
is the number of samples andn_features
is the number of features.- yarray-like of shape (n_samples,)
Target values (integers).
- Returns:
- selfobject
Fitted estimator.
- get_metadata_routing()[source]#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequest
encapsulating 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]#
Perform classification on an array of test vectors X.
The predicted class C for each sample in X is returned.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Vector to be scored, where
n_samples
is the number of samples andn_features
is the number of features.
- Returns:
- Cndarray of shape (n_samples,)
Estimated probabilities.
- predict_log_proba(X)[source]#
Return log of posterior probabilities of classification.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Array of samples/test vectors.
- Returns:
- Cndarray of shape (n_samples, n_classes)
Posterior log-probabilities of classification per class.
- predict_proba(X)[source]#
Return posterior probabilities of classification.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Array of samples/test vectors.
- Returns:
- Cndarray of shape (n_samples, n_classes)
Posterior probabilities of classification per class.
- score(X, y, sample_weight=None)[source]#
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Test samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
True labels for
X
.- sample_weightarray-like of shape (n_samples,), default=None
Sample weights.
- Returns:
- scorefloat
Mean accuracy of
self.predict(X)
w.r.t.y
.
- 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.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') QuadraticDiscriminantAnalysis [source]#
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns:
- selfobject
The updated object.
Gallery examples#
Linear and Quadratic Discriminant Analysis with covariance ellipsoid