Topic 05 / 8

Machine Learning with scikit-learn

~18 min read  //  Data Science Series  //  Coding India

The Scikit-Learn API

Scikit-learn is the standard library for traditional machine learning in Python. Its brilliance lies in its consistent, predictable API. Nearly every model follows the exact same pattern:

  1. Initialize: model = ModelClass(hyperparameters)
  2. Train: model.fit(X_train, y_train)
  3. Predict: predictions = model.predict(X_test)

Supervised Learning: Regression vs. Classification

In Supervised Learning, you have a target variable (what you want to predict). If the target is a continuous number (e.g., house price), it’s a Regression problem. If the target is a category (e.g., spam or not spam), it’s a Classification problem.

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# Split data into training and testing sets
X = df.drop("is_churned", axis=1) # Features
y = df["is_churned"]              # Target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Train the model
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

Unsupervised Learning: Clustering

In Unsupervised Learning, there is no target variable. You are asking the algorithm to find hidden patterns or groupings in the data. K-Means is a popular algorithm for this.

from sklearn.cluster import KMeans

# Find 3 distinct customer segments
kmeans = KMeans(n_clusters=3)
df["segment"] = kmeans.fit_predict(df[["recency", "frequency", "monetary"]])