Comprehensive one-page visual playbook covering the complete Python data science lifecycle. This document details environment setup (Conda), data engineering (NumPy, Pandas), visualization (Matplotlib, Seaborn), and machine learning workflows with deep dives into regression and clustering techniques.
- Master the modern Python data science stack.
- Understand vectorization, broadcasting, and array manipulation.
- Perform exploratory data analysis (EDA) with visual insights.
- Implement supervised (Regression) and unsupervised (Clustering) learning algorithms.
The ecosystem consists of four primary layers: Environments, Core Libraries, Visualization, and Modeling.
Why? Isolates dependencies. Prevents "dependency hell" where library A requires version X of package Y, breaking library B.
# Create isolated environment named 'datascience'
conda create --name datascience python=3.10
# Activate
conda activate datascience
# Install packages (Jupyter, NumPy, Pandas, Scikit-learn)
conda install jupyter numpy pandas scikit-learn matplotlib seaborn
# Save environment file for reproducibility
conda env export > environment.ymlFoundation for scientific computing. Replaces slow Python lists with C-based arrays.
Vectorization: Applying operations to entire arrays at once (avoids Python loops).
import numpy as np
# 1D Array (Vector)
arr = np.array([1, 2, 3, 4, 5])
normalized = (arr - np.mean(arr)) / np.std(arr)
# 2D Array (Matrix)
matrix = np.array([[1, 2], [3, 4]])
result = matrix * 2 # Vectorized multiplication
# Broadcasting Example
matrix + 10 # Adds 10 to every elementHigh-performance data structures: Series (1D) and DataFrame (2D).
import pandas as pd
# Load data
df = pd.read_csv('data.csv')
# Handle Missing Data (Imputation)
df.fillna(df.mean(), inplace=True)
# Group By & Aggregate
sales_by_category = df.groupby('Category')['Sales'].sum()
# Merge
df_final = pd.merge(df1, df2, on='ID')General-purpose, highly customizable. "Plotting for Programmers."
import matplotlib.pyplot as plt
# Basic Plot
plt.plot(x, y)
plt.title('My Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
# Subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
ax1.scatter(x, y)
ax2.hist(data)
plt.tight_layout()Built on Matplotlib. Beautiful defaults. Statistical inference.
import seaborn as sns
# Heatmap (Correlation)
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
# Pairplot (Relationships across all pairs)
sns.pairplot(df)
# Distribution Plots
sns.histplot(data['Age'], kde=True) # Kernel Density Estimate
sns.boxplot(x='Category', y='Sales', data=df)Real EDA Workflow:
df.head(): Quick inspection.df.info(): Data types & missing values.df.describe(): Summary statistics.df.columns: Column names.- Correlation Heatmap: Identify relationships.
- Pairplot/Scatter Matrix: Visualize distributions.
- Boxplots per Category: Compare groups.
Definition: Learning a mapping function from input variables (X) to an output variable (y).
Key Algorithms:
- Linear Regression
- Logistic Regression
- Decision Trees
- Random Forest
- Support Vector Machines (SVM)
- k-Nearest Neighbors (k-NN)
Universal Pipeline:
Implementation Steps:
- Split Data (Train/Test sets)
- Feature Scaling (StandardScaler or MinMaxScaler)
- Train Model
- Predict
- Evaluate (Metrics)
Simple Linear Regression (1 variable): Fitting the "best line" to minimize vertical distance (residuals).
Multiple Linear Regression (many variables): Fitting the "best plane/hyperplane" to minimize squared errors.
Key Requirement: Linear relationship between variables.
1. Simple Linear Regression
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt
# Prepare Data
X = df[['StudyHours']] # Must be 2D
y = df['ExamScore']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Train
model = LinearRegression()
model.fit(X_train, y_train)
print(f"Intercept (b0): {model.intercept_}")
print(f"Coefficient (b1): {model.coef_[0]}")
# Predict & Evaluate
y_pred = model.predict(X_test)
print(f"MSE: {mean_squared_error(y_test, y_pred)}")
print(f"R2 Score: {r2_score(y_test, y_pred)}")
# Visualization
plt.scatter(X_test, y_test, color='black')
plt.plot(X_test, y_pred, color='blue', linewidth=3)
plt.show()2. Multiple Linear Regression
# X now has multiple features
X = df[['Feature1', 'Feature2', 'Feature3']]
y = df['Target']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2
)
# Feature Scaling (CRITICAL for Multiple Regression)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
model.fit(X_train_scaled, y_train)
y_pred = model.predict(X_test_scaled)What are Residuals?
Good Model: Random scatter around y=0.
Bad Model (Patterns):
- Heteroscedasticity (Fan Shape): Error variance is not constant. Typically fixed by applying transformations (e.g., Log) to the target variable.
- Non-linear Pattern (Curve): Indicates that the relationship is not linear. Can be captured using Polynomial Regression or non-linear models.
You can explore these concepts interactively at: 👉 Live Dashboard on GitHub Pages
This interactive layout features dynamic Javascript-based charts:
- Performance Sandbox: Comparing pure Python vs NumPy vectorization speed.
- Visual EDA Bubble Chart: Simulating multivariate relationships.
- Sklearn Pipeline Composition: Interactive donut chart of standard processing workflows.
- Linear & Polynomial Fitting: Visualizing Ordinary Least Squares and polynomial curves.
- Plotly 3D Surface Regression: Rotatable 3D plane regression.
The project is structured to allow you to keep your HTML, CSS, and JS code separate while rendering the visualization directly inside your Jupyter Notebook cells.
- index.html - Core dashboard layout.
- styles.css - Dashboard styling.
- script.js - Chart engine.
- python_ml_ecosystem_notes.ipynb - Interactive Jupyter Notebook.
- render_visualization.py - Python helper module.
Open python_ml_ecosystem_notes.ipynb and run the cell containing:
import render_visualization as rv
rv.render_bundled()This script reads your local code and runs the dashboard securely inside a sandboxed iframe, ensuring your Tailwind CSS styling does not break the Jupyter Notebook interface.



