Two worked end-to-end workflows: building a classification model and performing a clustering analysis.
Load and explore data
python
import pandas as pd
df = pd.read_csv('data.csv')
X = df.drop('target', axis=1)
y = df['target']
Split data with stratification
python
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, stratify=y, random_state=42
)
Create preprocessing pipeline ```python from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.compose import ColumnTransformer
# Handle numeric and categorical features separately preprocessor = ColumnTransformer([ ('num', StandardScaler(), numeric_features), ('cat', OneHotEncoder(), categorical_features) ]) ```
Build complete pipeline
python
model = Pipeline([
('preprocessor', preprocessor),
('classifier', RandomForestClassifier(random_state=42))
])
Tune hyperparameters ```python from sklearn.model_selection import GridSearchCV
param_grid = { 'classifier__n_estimators': [100, 200], 'classifier__max_depth': [10, 20, None] }
grid_search = GridSearchCV(model, param_grid, cv=5) grid_search.fit(X_train, y_train) ```
best_model = grid_search.best_estimator_ y_pred = best_model.predict(X_test) print(classification_report(y_test, y_pred)) ```
scaler = StandardScaler() X_scaled = scaler.fit_transform(X) ```
scores = [] for k in range(2, 11): kmeans = KMeans(n_clusters=k, random_state=42) labels = kmeans.fit_predict(X_scaled) scores.append(silhouette_score(X_scaled, labels))
optimal_k = range(2, 11)[np.argmax(scores)] ```
Apply clustering
python
model = KMeans(n_clusters=optimal_k, random_state=42)
labels = model.fit_predict(X_scaled)
Visualize with dimensionality reduction ```python from sklearn.decomposition import PCA
pca = PCA(n_components=2) X_2d = pca.fit_transform(X_scaled)
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=labels, cmap='viridis') ```