Model Building
Model building is the process of selecting an algorithm, training it on data, and tuning it to make accurate predictions. Python's Scikit-learn library pro...
Building Machine Learning Models in Python
Model building is the process of selecting an algorithm, training it on data, and tuning it to make accurate predictions. Python's Scikit-learn library provides a consistent interface for dozens of algorithms, making it easy to swap models and compare results.
The universal Scikit-learn pattern: create, fit, predict. from sklearn.linear_model import LinearRegression model = LinearRegression() model.fit(X_train, y_train) predictions = model.predict(X_test)
Before fitting any model, split your data to get an honest estimate of performance: from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
The random_state parameter ensures reproducibility. The test_size=0.2 reserves 20% for testing.
Feature scaling is critical for distance-based algorithms (KNN, SVM) and gradient descent-based models (linear regression, neural networks): from sklearn.preprocessing import StandardScaler scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # use training stats on test data
Pipelines bundle preprocessing and modeling steps to prevent data leakage and simplify deployment: from sklearn.pipeline import Pipeline pipe = Pipeline([('scaler', StandardScaler()), ('model', LinearRegression())]) pipe.fit(X_train, y_train) score = pipe.score(X_test, y_test)
Hyperparameter tuning with GridSearchCV exhaustively tests combinations of parameters: from sklearn.model_selection import GridSearchCV params = {'model__C': [0.1, 1, 10], 'model__kernel': ['linear', 'rbf']} gs = GridSearchCV(pipe, params, cv=5, scoring='accuracy') gs.fit(X_train, y_train) print(gs.best_params_)
Cross-validation (cv=5) splits training data into 5 folds and evaluates on each, giving a more robust performance estimate than a single train/test split.
Practical tip: Start with a simple baseline model (e.g., predicting the mean for regression, the majority class for classification). Only a model that beats the baseline is worth pursuing further.
Exam tip: Overfitting occurs when a model learns training data noise. Signs include high training accuracy but low test accuracy. Combat it with regularization, simpler models, or more training data.