Project Machine Learning
In this final chapter, we will combine all the concepts we have learned into one end-to-end ML project: from data loading all the way to model deployment.
Complete ML Pipeline
1. Problem Definition
2. Data Loading
3. EDA (Exploratory Data Analysis)
4. Data Preprocessing
5. Feature Engineering
6. Model Selection
7. Training & Evaluation
8. Hyperparameter Tuning
9. Deployment
Step 1: Data Loading & EDA
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target
print(f"Shape: {df.shape}")
print(f"\nTarget distribution:\n{df['target'].value_counts()}")
print(f"\nMissing values:\n{df.isnull().sum().sum()}")
print(f"\nBasic statistics:\n{df.describe()}")
Step 2: Data Preprocessing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
X = df.drop('target', axis=1)
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
preprocessor = Pipeline([
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())
])
X_train_processed = preprocessor.fit_transform(X_train)
X_test_processed = preprocessor.transform(X_test)
Step 3: Feature Selection
from sklearn.feature_selection import SelectKBest, f_classif
selector = SelectKBest(f_classif, k=15)
X_train_selected = selector.fit_transform(X_train_processed, y_train)
X_test_selected = selector.transform(X_test_processed)
selected_features = np.array(data.feature_names)[selector.get_support()]
print(f"Selected features: {selected_features.tolist()}")
Step 4: Model Training & Comparison
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
models = {
'Logistic Regression': LogisticRegression(max_iter=1000),
'Random Forest': RandomForestClassifier(n_estimators=100),
'SVM': SVC(kernel='rbf'),
'KNN': KNeighborsClassifier(n_neighbors=5),
'Gradient Boosting': GradientBoostingClassifier(n_estimators=100)
}
results = {}
for name, model in models.items():
scores = cross_val_score(model, X_train_selected, y_train, cv=5)
results[name] = scores
print(f"{name:25s}: {scores.mean():.4f} (+/- {scores.std():.4f})")
Step 5: Hyperparameter Tuning
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import GradientBoostingClassifier
param_grid = {
'n_estimators': [100, 200],
'learning_rate': [0.05, 0.1, 0.2],
'max_depth': [3, 5],
'min_samples_split': [2, 5]
}
grid = GridSearchCV(
GradientBoostingClassifier(random_state=42),
param_grid, cv=5, scoring='accuracy', n_jobs=-1
)
grid.fit(X_train_selected, y_train)
print(f"Best params: {grid.best_params_}")
print(f"Best CV score: {grid.best_score_:.4f}")
Step 6: Final Evaluation
from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay
best_model = grid.best_estimator_
y_pred = best_model.predict(X_test_selected)
print("Classification Report:")
print(classification_report(y_test, y_pred, target_names=['Malignant', 'Benign']))
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(cm, display_labels=['Malignant', 'Benign'])
disp.plot(cmap='Blues')
plt.title('Confusion Matrix - Final Model')
plt.show()
Step 7: Feature Importance
import pandas as pd
importance = pd.Series(
best_model.feature_importances_,
index=selected_features
).sort_values(ascending=False)
print("Top 10 Features:")
print(importance.head(10))
plt.figure(figsize=(10, 6))
importance.head(10).plot(kind='barh')
plt.title('Top 10 Feature Importances')
plt.xlabel('Importance')
plt.gca().invert_yaxis()
plt.tight_layout()
plt.show()
Step 8: Save Model
import joblib
# Save model
joblib.dump(best_model, 'breast_cancer_model.pkl')
joblib.dump(preprocessor, 'preprocessor.pkl')
# Load model
loaded_model = joblib.load('breast_cancer_model.pkl')
Step 9: Simple Prediction Function
def predict_cancer(model, preprocessor, features):
features_processed = preprocessor.transform([features])
prediction = model.predict(features_processed)[0]
probability = model.predict_proba(features_processed)[0]
result = "Benign" if prediction == 1 else "Malignant"
confidence = max(probability) * 100
return f"Prediction: {result} ({confidence:.1f}%)"
# Usage example
sample = X_test.iloc[0].values
print(predict_cancer(best_model, preprocessor, sample))
ML Project Checklist
- [ ] Problem definition is clear
- [ ] Data has been collected and understood
- [ ] EDA has been performed
- [ ] Missing values have been handled
- [ ] Feature scaling has been applied
- [ ] Train-test split with stratify
- [ ] Multiple models have been tried
- [ ] Cross validation has been used
- [ ] Hyperparameter tuning has been performed
- [ ] Model has been evaluated with the right metrics
- [ ] Feature importance has been analyzed
- [ ] Model has been saved for deployment
Deployment Tips
- Use Flask/FastAPI for a REST API
- Use Docker for containerization
- Use Streamlit for an interactive dashboard
- Use ONNX for a universal model format
- Monitor model performance after deployment
Congratulations! You have completed the Machine Learning tutorial, from the basics to an end-to-end project. Keep learning and practicing to sharpen your ML skills!