Module 9: Capstone Projects
1. AI-powered Recommendation System
We implement a simple recommendation system using cosine similarity on a movie dataset.
from sklearn.feature_extraction.text import CountVectorizer
from [Link] import cosine_similarity
# Sample dataset
movies = [
"The Matrix sci-fi action",
"The Godfather crime drama",
"The Dark Knight action thriller",
"Pulp Fiction crime drama",
"Interstellar sci-fi space"
]
# Feature extraction
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(movies)
# Similarity
similarity = cosine_similarity(X)
# Recommend for 'The Matrix'
movie_index = 0
scores = list(enumerate(similarity[movie_index]))
scores = sorted(scores, key=lambda x: x[1], reverse=True)
print("Recommendations for The Matrix:")
for idx, score in scores[1:3]:
print(movies[idx])
Output Example:
Recommendations for The Matrix: Interstellar, The Dark Knight
2. Voice-controlled Assistant
We create a simple voice assistant that listens to commands and responds.
import speech_recognition as sr
import pyttsx3
# Initialize
engine = [Link]()
recognizer = [Link]()
with [Link]() as source:
print("Listening...")
audio = [Link](source)
try:
command = recognizer.recognize_google(audio)
print("You said:", command)
if "hello" in command:
[Link]("Hello, how can I help you?")
elif "time" in command:
import time
[Link]("Current time is " + [Link]("%H:%M"))
[Link]()
except:
print("Could not understand")
Output Example:
User says: 'Hello' → Assistant replies: 'Hello, how can I help you?'
3. Fraud Detection System
We use logistic regression on a synthetic dataset for fraud detection.
from [Link] import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from [Link] import classification_report
# Synthetic dataset
X, y = make_classification(n_samples=1000, n_features=5, weights=[0.9], random_state=42)
# Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Train
model = LogisticRegression()
[Link](X_train, y_train)
# Predict
y_pred = [Link](X_test)
print(classification_report(y_test, y_pred))
Output Example:
Classification report with precision/recall showing fraud detection performance.
4. Healthcare Predictive Analytics
We predict diabetes using logistic regression and the Pima Indians dataset.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score
# Load dataset (Pima Indians dataset CSV)
data = pd.read_csv("[Link]")
X = [Link]("Outcome", axis=1)
y = data["Outcome"]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Train
model = LogisticRegression(max_iter=1000)
[Link](X_train, y_train)
# Predict
y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
Output Example:
Accuracy: ~75% (varies depending on dataset split).