Machine Learning for Business with Python
Instructor: Charlie Flanagan
Location: Stanford (Tech 68)
What This Course Was All About
This course was more than just an introduction to algorithms—it was about bridging the gap between data science and real-world decision-making. We focused on the application of machine learning in business settings, where the goal isn’t just technical accuracy, but informed action. By the end of the course, we learned how to clean and explore data, build predictive models, interpret results, and most importantly, use those models to guide business strategies with confidence.
Core Concepts Explained
1. Regression Analysis – Framing the Foundation
Regression models are essential tools in the business data science toolbox. We explored both:
- Linear Regression: Used when the target variable is numeric (e.g., predicting monthly sales).
- Logistic Regression: Used when the target is categorical (e.g., will a customer churn: yes or no?).
What makes regression so powerful isn’t just prediction—it’s explanation. Linear regression helps us quantify relationships between variables, answering questions like “How does an increase in marketing spend affect sales?” Logistic regression provides similar insights for classification problems.
We also learned to interpret regression coefficients, slopes, and intercepts to understand both the direction and magnitude of effects. Visual tools like residual plots and R² helped validate model performance, ensuring that we didn’t just build models—we understood them.
2. Data Cleaning – The Unsung Hero
Before any modeling begins, data must be prepared. We learned that high-quality predictions require high-quality data. Our data cleaning workflow included:
- Handling missing values by understanding their cause and impact.
- Identifying and treating outliers that can skew model performance.
- Detecting multicollinearity—where two or more inputs are highly correlated, leading to unstable coefficients in regression.
Through this process, we embraced a key lesson: even the most advanced model cannot fix bad data. Investing time upfront to understand and clean data pays dividends throughout the modeling process. Garbage in = garbage out.
3. Decision Trees & Ensemble Methods – Intuitive and Powerful
Decision trees are simple, interpretable models that split data into decision paths. They are especially effective in business settings because they visually map out decisions in a way that non-technical stakeholders can understand.
We then explored ensemble methods that build on decision trees:
- Random Forests: Combine multiple trees to reduce overfitting and increase robustness.
- XGBoost: An advanced boosting method that builds trees sequentially, focusing on correcting previous errors. It is widely used in industry for its high accuracy and flexibility.
These models also introduced the concept of feature importance, which highlights the variables that most influence predictions—a key advantage when translating models into business insight.
4. Model Evaluation – Beyond Accuracy
Accuracy alone is often misleading, especially in imbalanced datasets. We learned to use a suite of performance metrics:
- Precision: What proportion of predicted positives were correct?
- Recall: What proportion of actual positives were correctly identified?
- F1 Score: A balance between precision and recall.
- ROC and PR Curves: Visual tools to evaluate model performance at different thresholds.
We discussed how business context influences metric choice. For example, if you're screening for fraud or weapons, high recall is crucial—you'd rather catch false positives than miss a true threat.
5. Real-World Business Applications
Throughout the course, we applied machine learning to problems that businesses face daily, such as:
- Customer Churn: Identifying customers at risk of leaving so that companies can intervene proactively.
- Pricing Strategy: Using data to estimate demand sensitivity and optimize product pricing.
- Marketing Effectiveness: Evaluating the true impact of advertising campaigns on sales and engagement.
- Loan Defaults: Predicting who is likely to repay or default, aiding risk management in finance.
These case studies emphasized the importance of clearly defining the business question, selecting appropriate data, and presenting results in ways that decision-makers can act on confidently.
Model Interpretability with SHAP
One of the most important topics we covered was model interpretability. Black-box models like XGBoost often deliver superior accuracy, but their inner workings can be opaque. This is where SHAP (Shapley Additive Explanations) comes in.
SHAP assigns each feature a contribution value for a given prediction, allowing us to answer key questions:
- Why did the model make this prediction?
- Which features had the most influence—positively or negatively?
This transparency is crucial in business, where stakeholders need to trust and understand the models before taking action. SHAP bridges that gap, making advanced models more accessible and auditable.
Tools and Platforms
- Google Colab: Our primary coding environment—cloud-based and accessible from any device without installation.
- GPUs: Optional acceleration for training models faster.
- Kaggle: A hands-on platform to explore datasets, practice modeling, and learn from the global data science community.
Final Thoughts
Charlie emphasized that machine learning success isn’t about building the most complex model—it’s about asking the right question, deeply understanding your data, and clearly communicating your findings. Whether it’s optimizing a marketing strategy or detecting customer churn, the ultimate goal is to support business decisions with clarity and confidence.
Above all, we learned that models should support decisions, not replace them.
Code Highlights and Lessons from the Course
1. Text Sentiment Classification with TF-IDF and Logistic Regression
Purpose: Predict whether a stock market message has positive or negative sentiment.
Lesson: Natural Language Processing (NLP) transforms text into a structured format that models can understand. TF-IDF emphasizes important terms, and logistic regression performs well for binary classification.
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
pipeline = make_pipeline(TfidfVectorizer(), LogisticRegression())
pipeline.fit(X_train, y_train)
# Predict sentiment
pipeline.predict_proba(["TSLA is a Long!"])
Accuracy was evaluated with precision, recall, and F1 score, giving us a clear picture of model performance in real-world classification problems.
2. Exploratory Data Analysis & Linear Regression (Movie Revenue)
Purpose: Understand how production budget affects worldwide box office gross.
Lesson: Linear regression is a powerful starting point for modeling relationships between variables. It also supports interpretability for stakeholders.
import statsmodels.api as sm
X = sm.add_constant(movies_data['Production_Budget'])
y = movies_data['Worldwide_Gross']
model = sm.OLS(y, X).fit()
print(model.summary())
The R² value of 0.62 indicates that around 62% of the variance in revenue is explained by production budget—highlighting a meaningful, though not complete, correlation.
3. Genre Effects in Revenue Prediction (Multivariable Regression)
Purpose: Evaluate how genre and budget together influence revenue.
Lesson: Including categorical features like genre allows us to build more comprehensive and accurate models. We used dummy variables to represent genres.
X = pd.get_dummies(movies_data[['Production_Budget', 'Major_Genre']], drop_first=True)
X = sm.add_constant(X)
model = sm.OLS(y, X).fit()
print(model.summary())
This approach improved R² slightly and offered insight into which genres outperform others when budget is controlled.
4. Neural Network for Fuel Efficiency Prediction
Purpose: Predict miles-per-gallon (MPG) using vehicle attributes.
Lesson: Neural networks require normalized inputs and offer flexibility in learning non-linear relationships. MAE and MSE help evaluate performance for regression tasks.
model = tf.keras.Sequential([
layers.Dense(64, activation='relu', input_shape=[len(train_dataset.keys())]),
layers.Dense(64, activation='relu'),
layers.Dense(1)
])
model.compile(loss='mse', optimizer='adam', metrics=['mae', 'mse'])
model.fit(normed_train_data, train_labels, epochs=100, validation_split=0.2)
This taught us the importance of feature scaling, early stopping to prevent overfitting, and error analysis via prediction histograms.