Provide models,preprocessing, eval and pipelines
Everything is one of these three types:
Estimator
Anything that learns parameters from data.
Examples:
LinearRegression()KMeans()RandomForestClassifier()StandardScaler()
Every estimator exposes:
.fit(X, y)→ compute internal parameters.predict(X)→ use parameters on new data.transform(X)→ use parameters to modify X (only for transformers)
Transformer
Special estimator that transforms input data using learned parameters.
Examples:
StandardScaler()(learns mean & variance, then normalizes)TfidfVectorizer()(learns vocabulary & IDF, then converts text to vectors)PCA()(learns principal components, then projects data)
t = Transformer()
t.fit(X)
X_new = t.transform(X)
Predictor
Estimator that can make predictions (predict or predict_proba) after fitting.
Examples:
SVC()LogisticRegression()KNeighborsClassifier()
Pipelines
If an ML solution is:
raw text → vectorizer → scaler → classifier
we don’t want to do:
X = vectorizer.fit_transform(text)
X_scaled = scaler.fit_transform(X)
model.fit(X_scaled, y)Because:
- we can accidentally leak training info into validation
- Reproducing exact steps becomes hard
- Hyperparameter tuning must keep steps together
A pipeline enforces an assembly-line structure:
from sklearn.pipeline import Pipeline
clf = Pipeline([
("tfidf", TfidfVectorizer()),
("model", LogisticRegression())
])
clf.fit(text, y)
Now the whole chain acts as one estimator.
Modules
- sklearn.pipeline
- Pipeline
- FeatureUnion
- make_pipeline
- make_union
- sklearn.feature_extraction
- VarianceThreshold
-
- text
- CountVectorizer
- TfidfVectorizer
- HashingVectorizer
- image
- extract_patches_2d
- PatchExtractor
- sklearn.preprocessing
- StandardScaler
- MinMaxScaler
- RobustScaler
- Normalizer
- OneHotEncoder
- LabelEncoder
- PolynomialFeatures
- Binarizer
- QuantileTransformer
- PowerTransformer
- sklearn.model_selection
- train_test_split
- KFold
- StratifiedKFold
- ShuffleSplit
- GridSearchCV
- RandomizedSearchCV
- cross_val_score
- cross_validate
- sklearn.metrics
- accuracy_score
- f1_score
- precision_score
- recall_score
- roc_auc_score
- mean_squared_error
- r2_score
- silhouette_score
- confusion_matrix
- classification_report
- sklearn.linear_model
- LinearRegression
- LogisticRegression
- Ridge
- Lasso
- ElasticNet
- SGDClassifier
- SGDRegressor
- sklearn.svm
- SVC
- SVR
- LinearSVC
- LinearSVR
- sklearn.tree
- DecisionTreeClassifier
- DecisionTreeRegressor
- ExtraTreeClassifier
- ExtraTreeRegressor
- export_text
- export_graphviz
- sklearn.ensemble
- RandomForestClassifier
- RandomForestRegressor
- GradientBoostingClassifier
- GradientBoostingRegressor
- AdaBoostClassifier
- AdaBoostRegressor
- ExtraTreesClassifier
- ExtraTreesRegressor
- VotingClassifier
- VotingRegressor
- BaggingClassifier
- BaggingRegressor
- sklearn.neighbors
- KNeighborsClassifier
- KNeighborsRegressor
- NearestNeighbors
- sklearn.naive_bayes
- GaussianNB
- MultinomialNB
- BernoulliNB
- CategoricalNB
- sklearn.cluster
- KMeans
- MiniBatchKMeans
- DBSCAN
- AgglomerativeClustering
- MeanShift
- SpectralClustering
- sklearn.compose
- ColumnTransformer
- TransformedTargetRegressor
- make_column_transformer
- sklearn.decomposition
- PCA
- KernelPCA
- NMF
- TruncatedSVD
- FastICA
- sklearn.impute
- SimpleImputer
- KNNImputer
- sklearn.covariance
- EllipticEnvelope
- EmpiricalCovariance
- GraphicalLasso
- MinCovDet
- sklearn.utils
- shuffle
- resample
- Bunch
- sklearn.datasets
- load_iris
- load_wine
- load_breast_cancer
- load_diabetes
- load_digits
- load_linnerud
- load_boston (removed)
- fetch_20newsgroups
- fetch_openml
- make_classification
- make_regression
- make_blobs
- make_moons
- make_circles