Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

API reference

Scikit-Learn API reference

scikit-learn machine learning

Identifiers
134
With full guides
130
Types
3

Topics covered

  • Estimator API
  • Train/test split
  • Preprocessing
  • Pipelines
  • Classification
  • Regression
  • Clustering
  • Model selection

Class

NameWhat it does
LinearRegressionLinearRegression fits an ordinary least-squares model by minimizing residual squared error. Use it to predict continuous targets from one or more features, with optional intercept fitting and non-negative coefficient constraints.
LogisticRegressionLogisticRegression is a regularized classifier for binary and multiclass problems. It supports dense or sparse features and several solvers with different penalty and multiclass capabilities.
RandomForestClassifierA classifier that combines many decision trees trained on dataset subsamples. Averaging their outputs improves predictive accuracy and helps control overfitting.
KMeansKMeans partitions data into a chosen number of clusters by iteratively assigning samples to centroids and updating those centroids. It supports k-means++ or custom initialization and Lloyd or Elkan optimization.
StandardScalerStandardScaler removes each feature's training mean and scales by its training standard deviation. It is useful for putting numeric features on comparable scales before modeling.
PipelinePipeline chains transformers in order and optionally finishes with a predictor. It lets the complete sequence be fitted, cross-validated, and parameter-tuned as one estimator.
GridSearchCVGridSearchCV exhaustively evaluates estimator hyperparameter combinations from a parameter grid using cross-validation. After fitting, it retains the best configuration and can delegate supported estimator methods.
DecisionTreeClassifierA supervised decision-tree classifier that learns feature-based rules for binary or multiclass prediction. Its split criterion, growth limits, weighting, randomness, and pruning behavior are configurable.
SVCSVC is a libsvm-based C-support vector classifier for binary and multiclass problems. It supports several kernels, with RBF as the default, and handles multiclass classification through one-vs-one models.
PCAPCA performs linear dimensionality reduction by centering data and using singular value decomposition to project it into a lower-dimensional space. It supports exact, randomized, covariance-based, and truncated SVD strategies.
RidgeRidge is a linear regression estimator that minimizes squared error with an L2 penalty on coefficients. Its alpha parameter controls regularization strength, and it supports single- and multi-target regression.
LassoLasso is a linear regression estimator with an L1 penalty. It encourages sparse coefficients by allowing some feature weights to become exactly zero.
ElasticNetElasticNet is linear regression with combined L1 and L2 regularization. Use alpha to control overall penalty strength and l1_ratio to control the L1/L2 balance.
SGDClassifierSGDClassifier trains regularized linear classifiers with stochastic gradient descent. It supports configurable losses, sparse or dense floating-point features, and incremental learning through partial_fit.
SGDRegressorSGDRegressor fits a regularized linear regression model using stochastic gradient descent. It supports several loss functions and L1, L2, or elastic-net penalties.
PerceptronPerceptron is a linear classifier backed by SGDClassifier. It uses perceptron loss with a constant learning rate and supports optional regularization, early stopping, and incremental fitting.
DecisionTreeRegressorDecisionTreeRegressor predicts continuous targets by recursively partitioning feature space with decision rules. Its criterion, split strategy, depth, leaf-size, and pruning parameters control how the tree is built and how complex it becomes.
RandomForestRegressorRandomForestRegressor is an ensemble of decision-tree regressors that averages predictions from trees trained on dataset subsamples. It improves predictive accuracy and helps control over-fitting.
GradientBoostingClassifierGradientBoostingClassifier builds a classification model by adding regression trees stage by stage to optimize a differentiable loss. It supports binary and multiclass classification, with log loss as the default objective.
GradientBoostingRegressorGradientBoostingRegressor fits an additive regression model by sequentially training trees against the negative gradient of a chosen loss. Tune learning_rate and n_estimators together to balance each tree’s contribution against the number of stages.
HistGradientBoostingClassifierHistogram-based gradient boosting classifier for fast tree ensembles on large datasets. It natively handles NaNs and offers controls for regularization, early stopping, categorical features, and constraints.
AdaBoostClassifierAdaBoostClassifier is a classification meta-estimator that sequentially fits classifiers while emphasizing difficult training examples. It combines the fitted classifiers, with n_estimators and learning_rate controlling the ensemble.
ExtraTreesClassifierExtraTreesClassifier is an ensemble classifier that fits randomized decision trees on dataset sub-samples and averages them. It supports classification with randomized splits and can natively handle missing values for random splits.
BaggingClassifierAn ensemble classifier that trains base classifiers on randomized subsets of samples and features, then aggregates their predictions. It is commonly used to reduce variance in high-variance estimators.
VotingClassifierVotingClassifier combines named classifiers by majority labels or aggregated probabilities. It supports hard and soft voting, optional classifier weights, and parallel fitting.
StackingClassifierStackingClassifier combines predictions from named base classifiers with a final classifier. It trains the final classifier on cross-validated base predictions to improve generalization.
SVRSVR is scikit-learn’s epsilon-support vector regression estimator. It supports linear and nonlinear kernels, with C controlling regularization and epsilon defining the no-penalty prediction tube.
LinearSVCLinearSVC is a linear support-vector classifier implemented with liblinear. It supports dense and sparse inputs, flexible penalties and losses, and one-vs-the-rest multiclass classification.
KNeighborsClassifierKNeighborsClassifier implements supervised classification by voting among a sample’s nearest neighbors. It supports uniform or distance-based voting and configurable neighbor-search algorithms and distance metrics.
KNeighborsRegressorKNeighborsRegressor predicts continuous targets from the nearest training samples. It supports uniform, inverse-distance, or callable weighting and configurable neighbor-search algorithms and distance metrics.
GaussianNBGaussianNB is a Gaussian Naive Bayes classifier that models each feature with a class-specific normal distribution. It supports both batch training with fit and incremental learning with partial_fit.
MultinomialNBMultinomialNB is a Naive Bayes classifier for multinomial data, particularly discrete features such as word counts. It can also work with fractional values such as tf-idf and uses additive smoothing by default.
BernoulliNBBernoulliNB is a Naive Bayes classifier for multivariate Bernoulli data. It is designed for binary or Boolean features and can optionally binarize numeric inputs.
NearestNeighborsNearestNeighbors is an unsupervised estimator for finding nearby samples in a fitted dataset. It supports fixed-count and radius-based queries with configurable search algorithms and distance metrics.
MiniBatchKMeansMiniBatchKMeans performs K-Means clustering with randomly sampled mini-batches instead of repeatedly processing the full dataset. It usually reduces computation time while accepting a possible decrease in clustering quality.
DBSCANDBSCAN is a density-based clustering estimator that expands clusters from dense core samples, supports arbitrary-shaped groups, and marks low-density samples as noise. It does not require the number of clusters to be specified in advance.
AgglomerativeClusteringAgglomerativeClustering hierarchically groups samples by repeatedly merging clusters. Choose either a target cluster count or a distance threshold, with linkage and optional connectivity controlling the merges.
SpectralClusteringClusters samples by embedding an affinity matrix through the normalized Laplacian and then labeling points in the embedded space. It is particularly useful for small, non-convex clusters where center-and-spread descriptions are inadequate.
MeanShiftMeanShift performs unsupervised clustering with a flat kernel, automatically discovering cluster centers from sample density. It updates candidate centroids toward local means and removes near-duplicate candidates.
BirchBirch is a memory-efficient, online clustering estimator that incrementally builds a Clustering Feature Tree. It can produce final clusters or compress data into subclusters for a later clustering algorithm.
GaussianMixtureClass
TruncatedSVDTruncatedSVD reduces feature matrices to a chosen number of dimensions using truncated singular value decomposition without centering the data. It efficiently supports sparse count and TF-IDF matrices, where the transformation is commonly called latent semantic analysis (LSA).
NMFNMF factorizes a non-negative matrix X into non-negative matrices W and H whose product approximates X. It is useful for additive representations such as dimensionality reduction, source separation, and topic extraction.
FastICAFastICA is a transformer for estimating independent sources from mixed observations. It supports configurable component count, whitening, nonlinear contrast functions, iterative convergence, and initialization.
KernelPCAKernelPCA performs nonlinear dimensionality reduction using kernels. It can also learn an approximate inverse mapping back to the original feature space.
LatentDirichletAllocationLatentDirichletAllocation is a topic-model estimator that learns latent topics from a document-term matrix using variational Bayes. It supports batch and online training and can transform documents into topic distributions.
MinMaxScalerMinMaxScaler linearly maps each feature independently into a chosen interval, defaulting to [0, 1]. It learns feature extrema during fitting and reuses them for later transformations.
RobustScalerRobustScaler centers features by their median and scales them using a configurable quantile range. Its default interquartile-range statistics are less affected by outliers than mean-and-variance scaling.
MaxAbsScalerMaxAbsScaler scales each feature by its largest absolute training value. It preserves zero-centered structure and sparsity because it does not subtract feature means.
NormalizerNormalizer rescales each nonzero sample independently to unit l1, l2, or max norm. It supports dense and sparse data and is stateless, so it does not need fitting.
OneHotEncoderOneHotEncoder converts categorical features into binary one-hot columns. It learns or accepts category vocabularies and can control output sparsity, unknown values, dropped categories, and infrequent-category grouping.
OrdinalEncoderClass
LabelEncoderLabelEncoder converts target labels into integer codes from 0 to n_classes - 1 and can convert those codes back. It is for y, not input features X.
PolynomialFeaturesPolynomialFeatures expands input columns into polynomial and interaction terms up to a chosen degree. It supports bounded degree ranges, interaction-only expansion, optional bias, and dense-output memory order.
KBinsDiscretizerKBinsDiscretizer converts continuous features into intervals using uniform, quantile, or one-dimensional k-means boundaries. It can emit sparse one-hot, dense one-hot, or ordinal integer representations.
FunctionTransformerFunctionTransformer turns an arbitrary callable into a scikit-learn transformer. It is designed for stateless operations such as logarithmic feature transforms or custom scaling.
PowerTransformerPowerTransformer applies a feature-wise Box-Cox or Yeo-Johnson transformation to make data more Gaussian-like. It estimates each feature's power parameter by maximum likelihood and standardizes the result by default.
QuantileTransformerQuantileTransformer maps each feature independently through an estimated quantile function to produce uniform or normal marginal distributions. It is a robust nonlinear preprocessing method that reduces the influence of marginal outliers.
RandomizedSearchCVRandomizedSearchCV samples a fixed number of hyperparameter settings and evaluates them with cross-validation. It is useful when exhaustive grid search is too costly.
KFoldKFold creates k train/validation splits by holding out each fold once and training on the remaining folds. It can optionally shuffle fold assignment and make that ordering reproducible.
StratifiedKFoldStratifiedKFold generates classification cross-validation folds that preserve class proportions. It returns train/test indices and can optionally shuffle class samples reproducibly.
TimeSeriesSplitTimeSeriesSplit is a cross-validator for time-ordered data. It builds progressively later test folds while training on earlier observations, with optional limits on training size, test size, and a gap.
ColumnTransformerColumnTransformer applies different transformers to selected columns of an array or pandas DataFrame and concatenates their outputs into one feature space. It is useful for heterogeneous data requiring separate preprocessing paths.
FeatureUnionFeatureUnion runs multiple named transformers in parallel and concatenates their outputs into one feature space. It is useful for combining different feature-extraction approaches in a single estimator.
SelectKBestSelectKBest ranks features using a univariate scoring function and keeps the k highest-scoring ones. It is useful for reducing feature dimensionality before fitting an estimator.
RFERFE selects features by repeatedly fitting an importance-producing estimator and removing the least important features. It returns rankings and a Boolean mask for the retained subset.
SelectFromModelSelectFromModel is a meta-transformer that keeps features whose model-derived importance meets a threshold. It can fit the wrapped estimator itself or consume a prefit estimator.
TfidfVectorizerTfidfVectorizer converts raw documents into a sparse matrix of TF-IDF features. It combines CountVectorizer and TfidfTransformer functionality in one estimator.
CountVectorizerCountVectorizer converts text documents into a sparse matrix of token-occurrence features. It learns a vocabulary unless one is supplied and supports configurable preprocessing, tokenization, stop-word filtering, and n-grams.
RidgeCVRidgeCV fits ridge regression while selecting the best regularization strength by cross-validation. It defaults to efficient leave-one-out cross-validation and supports custom scoring and validation strategies.
LassoCVLassoCV fits Lasso models over a regularization path and selects the penalty alpha by cross-validation. It uses coordinate descent and exposes the selected model and validation results.
ElasticNetCVElasticNetCV fits Elastic Net models along a regularization path and uses cross-validation to select regularization settings. It then refits the chosen model on the full training set.
BayesianRidgeBayesianRidge is a probabilistic linear regression estimator with Bayesian regularization. It learns noise and weight precisions from the data while fitting the model.
HuberRegressorHuberRegressor is an L2-regularized linear regression model that reduces the influence of outliers. It switches from squared to absolute loss when scaled residuals exceed epsilon.
PassiveAggressiveClassifierPassiveAggressiveClassifier is a linear online classifier using passive-aggressive updates. It supports hinge loss for PA-I and squared hinge loss for PA-II, but the class is deprecated in scikit-learn 1.8 and planned for removal in 1.10.
NuSVCNuSVC is a support-vector classifier whose nu parameter controls the allowed fraction of margin errors and the minimum fraction of support vectors. It supports multiple kernel types, including the default RBF kernel.
ComplementNBComplementNB is a Complement Naive Bayes classifier designed for imbalanced datasets. It uses complement-class statistics and is especially useful for text classification with nonnegative features.
CategoricalNBCategoricalNB is a Naive Bayes classifier for categorically distributed features. Encode each feature’s categories as contiguous integers starting at zero before fitting.
OPTICSOPTICS estimates clustering structure while preserving density information over a range of neighborhood radii. It extracts clusters with either the Xi method or a DBSCAN-like method.
AffinityPropagationAffinityPropagation clusters data by exchanging messages to identify representative samples, or exemplars. It supports Euclidean similarities or a precomputed affinity matrix, with preferences influencing the number of clusters.
IncrementalPCAIncrementalPCA performs PCA-style SVD-based dimensionality reduction while processing data in batches. It is designed for large or sparse inputs when ordinary batch PCA would use too much memory.
IsomapIsomap is a nonlinear dimensionality-reduction estimator that embeds data while preserving geodesic distances. It constructs shortest-path distances on a neighborhood graph and converts them into lower-dimensional coordinates.
LocallyLinearEmbeddingLocallyLinearEmbedding learns a low-dimensional representation by preserving local neighborhood relationships. Its method parameter supports standard, Hessian, modified, and local tangent-space alignment variants.
TSNETSNE is a nonlinear method for embedding high-dimensional samples into a lower-dimensional space for visualization. It models pairwise similarities as probabilities and optimizes their low-dimensional representation.
MDSMDS embeds pairwise dissimilarities in a lower-dimensional space using SMACOF. It supports metric and nonmetric variants and can use Euclidean or precomputed dissimilarities.
IsolationForestIsolationForest detects outliers by randomly partitioning feature space into trees. Observations isolated with shorter average paths are treated as more anomalous.
OneClassSVMOneClassSVM is an unsupervised estimator for detecting outliers by estimating the support of a distribution. It uses kernel methods and labels samples as inliers or outliers.
LocalOutlierFactorLocalOutlierFactor is an unsupervised estimator for detecting observations whose local density is substantially lower than that of their nearest neighbors. It uses k-nearest-neighbor distances to compute local outlier scores.
CalibratedClassifierCVCalibratedClassifierCV calibrates a classifier’s probability estimates with cross-validation. It supports sigmoid, isotonic, and temperature scaling, and can average predictions from calibrated fold-specific models.
MultiOutputClassifierMultiOutputClassifier extends a single-output classifier to multi-target classification by fitting one classifier per target. It can parallelize target-wise fit, prediction, and supported partial fitting.
OneVsRestClassifierOneVsRestClassifier wraps a binary classifier to solve multiclass problems by fitting one model per class against all other classes. It also supports multilabel classification through independent binary models for each label.

Function

NameWhat it does
train_test_splitSplits aligned arrays or matrices into random training and test subsets. It supports proportional or absolute subset sizes, reproducible shuffling, and optional stratification by class labels.
cross_val_scoreEvaluate an estimator by cross-validation and return one score for each split. The function can use a named metric, a custom scorer, or the estimator's default scorer.
accuracy_scoreComputes classification accuracy by comparing predicted labels with true labels. It returns a fraction by default or a correct-sample count when normalize=False.
confusion_matrixCompute a classification confusion matrix from true and predicted labels. The result counts or normalizes observations by true and predicted class.
fitfit trains a scikit-learn estimator from feature data and, for supervised learning, targets. It stores learned state and returns the fitted estimator.
predictpredict applies a fitted scikit-learn estimator to input samples and returns predictions. Classification produces class labels; regression produces numeric values.
cross_validateEvaluate one or more metrics across cross-validation folds while recording fit and score times. Optional outputs include training scores, fitted estimators, and the indices used for each split.
cross_val_predictGenerate one out-of-fold estimate for every input sample by fitting separate estimators on cross-validation training folds. Use it to obtain predictions for visualization or downstream analysis, not as a substitute for per-fold generalization metrics.
learning_curveComputes cross-validated training and test scores for multiple training-set sizes. It helps assess how estimator performance changes as more training data is provided.
validation_curveEvaluate an estimator across values of one parameter and collect cross-validation training and test scores. The result helps reveal how that parameter affects model performance.
precision_scoreCompute classification precision: the ratio of true positives to all predicted positives. It supports binary, multiclass, and multilabel targets through configurable label selection and averaging.
recall_scoreComputes recall, the proportion of actual positive cases identified by the predictions. It supports binary, multiclass, and multilabel targets through configurable label selection and averaging.
f1_scoreCompute the F1 score, the harmonic mean of precision and recall. It ranges from 0 to 1 and supports binary, multiclass, and multilabel classification through averaging options.
roc_auc_scoreComputes the area under the receiver operating characteristic curve from prediction scores. It supports binary, multiclass, and multilabel classification, with restrictions determined by the input type and options.
classification_reportBuilds a per-class classification-metrics report from true and predicted labels. It returns formatted text by default or structured data when output_dict=True.
mean_squared_errorComputes mean squared error, a regression loss that summarizes squared differences between true and predicted targets. It returns one aggregate value by default or per-output values when requested.
mean_absolute_errorComputes mean absolute error, a non-negative regression loss based on absolute differences between true and predicted targets. It can weight samples and either preserve or aggregate errors across multiple outputs.
r2_scoreComputes the coefficient of determination for regression predictions. A score of 1.0 is best; scores can be negative, and multi-output results can be aggregated or returned separately.
roc_curveComputes the receiver operating characteristic for binary classification by evaluating scores at decision thresholds. Returns false-positive rates, true-positive rates, and the thresholds that produce them.
precision_recall_curveComputes precision-recall pairs over score thresholds for binary classification. It accepts positive-class probabilities or classifier decision scores and returns precision, recall, and thresholds.
log_lossComputes classification log loss, also called logistic or cross-entropy loss, from true labels and predicted probabilities. It evaluates the negative log-likelihood of the true labels and returns the mean by default.
silhouette_scoreComputes the mean Silhouette Coefficient for a clustering assignment. Scores near 1 indicate well-separated clusters, near 0 indicate overlap, and negative values often indicate unsuitable assignments.
make_pipelineConstructs a scikit-learn Pipeline from estimators supplied in order. It automatically generates lowercase step names from estimator types.
make_column_transformerCreates a ColumnTransformer from transformer-and-column pairs without requiring explicit transformer names. Names are generated automatically from transformer types.
make_classificationGenerates a synthetic n-class classification problem with controllable class structure, feature categories, and noise. It returns feature data and integer labels by default.
make_regressionGenerates synthetic regression data from a random linear model. It supports configurable feature structure, informative features, target count, Gaussian noise, shuffling, and optional recovery of the true coefficients.
make_blobsGenerates synthetic multiclass data as isotropic Gaussian clusters. It returns feature samples and integer cluster labels, with optional generated center coordinates.
load_irisLoads scikit-learn’s built-in Iris dataset for multi-class classification. It returns a metadata-rich Bunch by default, or data and targets directly with configurable pandas output.
load_digitsLoads scikit-learn’s built-in handwritten-digits classification dataset. It can return a metadata-rich Bunch or a features-and-target tuple, with optional class restriction and pandas output.
fetch_openmlDownloads a dataset from OpenML by name or numeric ID. It can cache the download and return either a dataset container or separate features and target.
permutation_importanceComputes feature importance for a fitted estimator by repeatedly shuffling each feature and measuring the resulting score change. It returns per-feature averages, variability, and raw scores across permutations.
balanced_accuracy_scoreComputes balanced accuracy for binary or multiclass classification by averaging recall across classes. It is designed to provide a more informative score than ordinary accuracy on imbalanced datasets.
cohen_kappa_scoreComputes Cohen’s kappa, a chance-adjusted measure of agreement between two classification labelings. It supports optional ordinal disagreement weights, sample weights, explicit labels, and an undefined-result replacement value.
matthews_corrcoefComputes the Matthews correlation coefficient (MCC) for binary or multiclass classification. The score ranges from -1 for inverse prediction to +1 for perfect prediction, with 0 representing average random prediction.
adjusted_rand_scoreComputes the Rand similarity between two clusterings and corrects it for chance. It is symmetric, returns 1.0 for identical partitions up to label permutation, and is near 0.0 for random labelings.
GroupKFoldGroupKFold performs K-fold cross-validation while keeping each group wholly within one fold. Every group is used as test data exactly once, preventing the same group from appearing in both training and test data.
LeaveOneOutLeaveOneOut is a cross-validator that holds out one sample at a time while using all remaining samples for training. It creates as many splits as there are samples.
RepeatedKFoldRepeatedKFold repeats K-fold cross-validation several times, randomizing the fold assignments for each repetition. Use random_state when reproducible splits are needed.

Module

NameWhat it does
transformModule
fit_transformfit_transform fits a transformer on data and then transforms that data in one call. It accepts optional targets and fit parameters, returning the transformed feature array.
predict_probaModule
scoreaccuracy_score evaluates classification predictions by comparing true and predicted labels. It returns either the fraction of correct samples or their count, with optional sample weighting.
fit_predictfit_predict fits an estimator and returns predictions for the same training data in one call. It is especially useful for unsupervised, transductive estimators and clustering workflows.

Search across every package and filter by type in your dashboard.

Start learning