Understanding what machine learning is through the self-driving car example, mastering the core concepts of training and prediction.
Figure 1: Concept art of a self-driving vehicle.
Learning Objectives
After reading this post, you will be able to:
Understand the fundamental difference between machine learning and traditional programming
Know the three conditions where machine learning is applicable
Master the core concepts of “training” and “prediction”
Distinguish between supervised, unsupervised, reinforcement, and deep learning
Theory
The Self-Driving Car Challenge
Imagine a self-driving car navigating through traffic. It needs to constantly “see” its surroundings and identify pedestrians, vehicles, traffic lights, etc.:
Figure 2: The object recognition challenge — Camera Feed → [???] → Classification
What goes in [???]? Traditional programming might approach it like this:
# Traditional programming approach (pseudocode)defis_car(image):ifhas_four_wheels(image)andhas_windows(image)andhas_headlights(image):returnTruereturnFalse
But here’s the problem:
What if half the car is occluded, showing only two wheels?
What about trucks, motorcycles, or exotic vehicles?
Real-world scenarios are too complex to enumerate all rules!
This is where machine learning comes in.
What is Machine Learning?
Machine learning takes a completely different approach:
# Visualize some digits with their labelsfig,axes=plt.subplots(3,6,figsize=(10,5))fig.suptitle('Handwritten Digits Dataset (with labels)',fontsize=14)fori,axinenumerate(axes.flat):ax.imshow(digits.images[i],cmap='gray_r')ax.set_title(f'Label: {digits.target[i]}',fontsize=10)ax.axis('off')plt.tight_layout()plt.show()plt.savefig('assets/digits_labeled.png',dpi=150)
Figure 3: Sample handwritten digits from the MNIST dataset
Experience Training and Prediction
Although we haven’t learned specific algorithms yet, let’s experience the complete ML workflow:
fromsklearn.model_selectionimporttrain_test_splitfromsklearn.neighborsimportKNeighborsClassifierfromsklearn.metricsimportaccuracy_score# Prepare dataX=digits.data# Features: pixel valuesy=digits.target# Labels: 0-9# Split into training and test setsX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)print(f"Training set size: {len(X_train)}")print(f"Test set size: {len(X_test)}")# Train model (using K-Nearest Neighbors, will cover later)model=KNeighborsClassifier(n_neighbors=3)model.fit(X_train,y_train)# This is "training"# Predicty_pred=model.predict(X_test)# This is "prediction"# Evaluate accuracyaccuracy=accuracy_score(y_test,y_pred)print(f"\nModel accuracy on test set: {accuracy:.2%}")
fromPILimportImageimportnumpyasnpimportmatplotlib.pyplotaspltfromsklearn.datasetsimportload_digitsfromsklearn.neighborsimportKNeighborsClassifier# Train the model (same as before)digits=load_digits()X,y=digits.data,digits.targetclf=KNeighborsClassifier(n_neighbors=3)clf.fit(X,y)defpredict_custom_digit(image_path,visualize=True):"""
Load a custom handwritten digit image and predict its value.
Image should be a white digit on black background, any size.
"""# Load original imageimg_original=Image.open(image_path).convert('L')# Resize to 8x8img_resized=img_original.resize((8,8),Image.Resampling.LANCZOS)# Convert to array and normalize (sklearn digits are 0-16 scale)img_array=np.array(img_resized)img_processed=16-(img_array/255.0*16)# Invert colorsimg_flat=img_processed.flatten().reshape(1,-1)# Predictprediction=clf.predict(img_flat)[0]probabilities=clf.predict_proba(img_flat)[0]confidence=probabilities[prediction]*100# Visualizationifvisualize:fig,axes=plt.subplots(1,3,figsize=(10,3))# Original imageaxes[0].imshow(img_original,cmap='gray')axes[0].set_title('Original Image')axes[0].axis('off')# Processed 8x8 imageaxes[1].imshow(img_processed,cmap='gray_r')axes[1].set_title('Processed (8×8)')axes[1].axis('off')# Prediction confidence barcolors=['green'ifi==predictionelse'lightgray'foriinrange(10)]axes[2].barh(range(10),probabilities,color=colors)axes[2].set_yticks(range(10))axes[2].set_xlabel('Confidence')axes[2].set_title(f'Prediction: {prediction} ({confidence:.1f}%)')plt.tight_layout()plt.savefig('assets/custom_prediction.png',dpi=150)plt.show()print(f"Predicted digit: {prediction}")print(f"Confidence: {confidence:.1f}%")returnprediction# Usage example:predict_custom_digit('my_digit.png')
Figure 5: Custom digit prediction — Original image, preprocessed input, and model confidence
💡 Tips for best results:
Draw a white digit on black background
Center the digit in the image
Use a thick brush/pen stroke
Save as PNG format
Deep Dive
Q1: Why does learning from training data work on new data?
This is the core question of machine learning! Simply put, we assume training and test data come from the same distribution.
Q2: Deep learning is so powerful — why learn traditional ML?
Deep learning requires massive datasets; traditional methods may work better on small data
Deep learning needs powerful hardware (GPUs); traditional methods are lighter
Traditional methods offer better interpretability — they can tell you “why” a prediction was made
Q3: How do I choose which ML method to use?
Summary
Core Concept
Key Points
Machine Learning
Machines learn from data instead of explicit programming