Python: Sklearn
Scikit-learn (sklearn) is Python's gold-standard ML library for classical machine learning from preprocessing and feature engineering to model training, evaluation, and pipelines.
Contents 6 Sections
1Core Workflow
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix# Split data X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42, stratify=y )
# Scale features scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) # fit+transform on train X_test_scaled = scaler.transform(X_test) # transform only on test
# Train model model = LogisticRegression(max_iter=1000, C=1.0) model.fit(X_train_scaled, y_train)
# Evaluate y_pred = model.predict(X_test_scaled) print(classification_report(y_test, y_pred)) print(confusion_matrix(y_test, y_pred)) ```
2Preprocessing
from sklearn.preprocessing import (
StandardScaler, MinMaxScaler, RobustScaler,
LabelEncoder, OneHotEncoder, OrdinalEncoder
)
from sklearn.impute import SimpleImputer, KNNImputer# Handle missing values imputer = SimpleImputer(strategy='median') # or 'mean', 'most_frequent' X_imputed = imputer.fit_transform(X)
# Encode categoricals ohe = OneHotEncoder(sparse_output=False, handle_unknown='ignore') X_encoded = ohe.fit_transform(X_categorical)
# Feature scaling scaler = RobustScaler() # robust to outliers X_scaled = scaler.fit_transform(X)
# Feature selection from sklearn.feature_selection import SelectKBest, f_classif, RFE selector = SelectKBest(score_func=f_classif, k=10) X_selected = selector.fit_transform(X, y) ```
3Algorithms Cheat Sheet
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.ensemble import (
RandomForestClassifier, RandomForestRegressor,
GradientBoostingClassifier, GradientBoostingRegressor,
AdaBoostClassifier, VotingClassifier, StackingClassifier
)
from sklearn.svm import SVC, SVR
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB, MultinomialNB
from sklearn.cluster import KMeans, DBSCAN, AgglomerativeClustering
from sklearn.decomposition import PCA, TruncatedSVD
from sklearn.neural_network import MLPClassifier# All share the same API: fit(), predict(), score() ```
4Pipelines
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer# Separate numeric and categorical columns numeric_features = ['age', 'salary', 'tenure'] categorical_features = ['department', 'role']
numeric_transformer = Pipeline([ ('imputer', SimpleImputer(strategy='median')), ('scaler', StandardScaler()) ])
categorical_transformer = Pipeline([ ('imputer', SimpleImputer(strategy='constant', fill_value='missing')), ('onehot', OneHotEncoder(handle_unknown='ignore')) ])
preprocessor = ColumnTransformer([ ('num', numeric_transformer, numeric_features), ('cat', categorical_transformer, categorical_features) ])
# Full pipeline pipe = Pipeline([ ('preprocessor', preprocessor), ('classifier', RandomForestClassifier(n_estimators=100, random_state=42)) ])
pipe.fit(X_train, y_train) score = pipe.score(X_test, y_test) ```
5Hyperparameter Tuning
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV, cross_val_score
from scipy.stats import randint, uniform# Grid Search exhaustive param_grid = { 'classifier__n_estimators': [50, 100, 200], 'classifier__max_depth': [None, 10, 20], 'classifier__min_samples_split': [2, 5, 10] }
grid_search = GridSearchCV( pipe, param_grid, cv=5, scoring='f1_macro', n_jobs=-1, verbose=1 ) grid_search.fit(X_train, y_train) print(grid_search.best_params_)
# Randomized Search faster for large spaces param_dist = { 'classifier__n_estimators': randint(50, 500), 'classifier__max_depth': [None, 5, 10, 20, 30], 'classifier__max_features': uniform(0.1, 0.9) }
random_search = RandomizedSearchCV(pipe, param_dist, n_iter=50, cv=5, n_jobs=-1) random_search.fit(X_train, y_train) ```
6Model Evaluation & Selection
from sklearn.metrics import (
accuracy_score, precision_score, recall_score, f1_score,
roc_auc_score, average_precision_score, mean_absolute_error,
mean_squared_error, r2_score
)
from sklearn.model_selection import StratifiedKFold, learning_curve
import matplotlib.pyplot as plt# Cross-validation cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) scores = cross_val_score(pipe, X, y, cv=cv, scoring='roc_auc') print(f"AUC: {scores.mean():.3f} ± {scores.std():.3f}")
# Learning curves diagnose over/underfitting train_sizes, train_scores, test_scores = learning_curve( pipe, X, y, cv=5, n_jobs=-1, train_sizes=np.linspace(0.1, 1.0, 10) )
plt.plot(train_sizes, train_scores.mean(axis=1), label='Train') plt.plot(train_sizes, test_scores.mean(axis=1), label='Validation') plt.legend() plt.show() ```
Explore the Full Portfolio
This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.
The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.
For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.