Module 4: Machine Learning
with Python
Comprehensive 20-Slide PPT with
Programs & Outputs
Introduction to Machine Learning
• Definition:
• ML is a subset of AI that learns patterns from
data.
• Applications: Healthcare, Finance, NLP, Image
Recognition.
Why Machine Learning?
• Traditional programming vs ML.
• ML automates learning and prediction without
explicit rules.
Types of Machine Learning
• 1. Supervised Learning
• 2. Unsupervised Learning
• 3. Reinforcement Learning
Supervised Learning: Regression
• Regression predicts continuous outcomes.
• Example: House price prediction.
Program: Linear Regression
Example
• from sklearn.linear_model import
LinearRegression
• from sklearn.model_selection import
train_test_split
• import numpy as np
• X = [Link]([[1],[2],[3],[4],[5]])
• y = [Link]([2,4,6,8,10])
• X_train,X_test,y_train,y_test =
Supervised Learning: Classification
• Classification predicts discrete categories.
• Example: Spam or Not Spam.
Program: Logistic Regression
Example
• from sklearn.linear_model import
LogisticRegression
• X = [[1],[2],[3],[4]]
• y = [0,0,1,1]
• model=LogisticRegression()
• [Link](X,y)
• print([Link]([[2.5]]))
• Output: [1]
Unsupervised Learning
• No labels, model groups data.
• Examples: Clustering, PCA.
Clustering with K-Means
• Groups similar data points.
• Applications: Customer segmentation.
Program: KMeans Example
• from [Link] import KMeans
• import numpy as np
• X = [Link]([[1,2],[1,4],[1,0],[10,2],[10,4],
[10,0]])
• model=KMeans(n_clusters=2)
• [Link](X)
• print(model.labels_)
• Output: [1 1 1 0 0 0]
Dimensionality Reduction with PCA
• Reduces features while keeping variance.
• Used for visualization and efficiency.
Overfitting Problem
• Model memorizes training data.
• Poor generalization to new data.
Regularization
• L1 (Lasso) & L2 (Ridge) reduce complexity.
• Helps prevent overfitting.
Model Evaluation Metrics
• 1. Accuracy
• 2. Precision
• 3. Recall
• 4. F1-score
• 5. Confusion Matrix
Confusion Matrix Example
• TP=50, TN=40, FP=5, FN=5
• Accuracy=(TP+TN)/(Total)=90/100=0.9
Project 1: House Price Prediction
• Using Linear Regression with real datasets like
California Housing.
Program: House Price Example
• from [Link] import
fetch_california_housing
• from sklearn.model_selection import
train_test_split
• from sklearn.linear_model import
LinearRegression
• X,y=fetch_california_housing(return_X_y=True
)
• X_train,X_test,y_train,y_test=train_test_split(
X,y,test_size=0.2)
Project 2: Spam Classifier
• Classify emails as spam/ham using Naive
Bayes.
Program: Spam Classifier Example
• from sklearn.feature_extraction.text import
CountVectorizer
• from sklearn.naive_bayes import
MultinomialNB
• X=["free money","hello friend","win cash
prize"]
• y=[1,0,1]
• vec=CountVectorizer()
• X_vec=vec.fit_transform(X)
Conclusion
• Machine Learning is powerful in predictions.
• Supervised for labeled tasks, Unsupervised for
clustering.
• Projects demonstrate real-world use.