100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Scikit-learn Cheat Sheet

Scikit-learn Cheat Sheet

Core scikit-learn workflow covering train/test splitting, pipelines, preprocessing, common estimators, cross-validation, hyperparameter tuning, and evaluation metrics.

2 PagesIntermediateApr 12, 2026

Train/Test Split & Fit

Standard supervised learning workflow.

python
from sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerfrom sklearn.linear_model import LogisticRegressionX_train, X_test, y_train, y_test = train_test_split(    X, y, test_size=0.2, random_state=42, stratify=y)scaler = StandardScaler()X_train = scaler.fit_transform(X_train)   # fit + transform on train onlyX_test = scaler.transform(X_test)         # transform only on testmodel = LogisticRegression(max_iter=1000)model.fit(X_train, y_train)y_pred = model.predict(X_test)print(model.score(X_test, y_test))        # mean accuracy

Pipelines & ColumnTransformer

Chain preprocessing and a model into one estimator.

python
from sklearn.pipeline import Pipelinefrom sklearn.compose import ColumnTransformerfrom sklearn.preprocessing import StandardScaler, OneHotEncoderfrom sklearn.ensemble import RandomForestClassifierpreprocess = ColumnTransformer([    ("num", StandardScaler(), ["age", "income"]),    ("cat", OneHotEncoder(handle_unknown="ignore"), ["city"]),])pipe = Pipeline([    ("preprocess", preprocess),    ("clf", RandomForestClassifier(n_estimators=200, random_state=42)),])pipe.fit(X_train, y_train)pipe.predict(X_test)

Common Estimators

Frequently used models by task.

  • LogisticRegression- linear classifier for binary/multiclass problems
  • RandomForestClassifier / Regressor- ensemble of decision trees, strong baseline
  • SVC- support vector classifier, effective on smaller/high-dim data
  • KNeighborsClassifier- instance-based classifier using nearest neighbors
  • LinearRegression / Ridge / Lasso- linear regression with optional L2/L1 regularization
  • KMeans- centroid-based unsupervised clustering
  • PCA- dimensionality reduction via principal components

Cross-Validation & GridSearchCV

Evaluate robustly and tune hyperparameters.

python
from sklearn.model_selection import cross_val_score, GridSearchCVscores = cross_val_score(pipe, X, y, cv=5, scoring="f1_macro")print(scores.mean(), scores.std())param_grid = {    "clf__n_estimators": [100, 200, 400],    "clf__max_depth": [None, 10, 20],}grid = GridSearchCV(pipe, param_grid, cv=5, scoring="accuracy", n_jobs=-1)grid.fit(X_train, y_train)print(grid.best_params_, grid.best_score_)
Pro Tip

Always fit preprocessing steps (scalers, encoders) only on the training fold — wrap them in a Pipeline so cross_val_score and GridSearchCV refit them per fold automatically, avoiding data leakage from the test set.

Was this cheat sheet helpful?

Explore Topics

#ScikitLearn#ScikitLearnCheatSheet#DataScience#Intermediate#Train#Test#Split#Fit#MachineLearning#Testing#DevOps#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet