Q1: Implement Linear Regression for House Price Prediction
Build a simple machine learning model that predicts house prices based on house size. Create sample data containing house sizes and prices, train a Linear Regression model, and predict the price of a new house. Logic: prepare the feature and target data, create the model, train it using fit(), and use predict() to generate a prediction.
import numpy as np
from sklearn.linear_model import LinearRegression
# Training data
X = np.array([[800], [1000], [1200], [1500], [1800]])
y = np.array([200000, 250000, 300000, 375000, 450000])
# Create model
model = LinearRegression()
# Train model
model.fit(X, y)
# Predict price for a 1400 sq ft house
new_house = np.array([[1400]])
prediction = model.predict(new_house)
print("Predicted House Price:", prediction[0])Q2: Build a Logistic Regression Model for Classification
Create a classification model that predicts whether a student will pass based on study hours. Use Logistic Regression to classify the student as 0 or 1. Logic: prepare study-hour data and binary labels, train the classifier, and use predict() to classify a new student's result.
import numpy as np
from sklearn.linear_model import LogisticRegression
# Study hours
X = np.array([[1], [2], [3], [4], [5], [6], [7], [8]])
# 0 = Fail, 1 = Pass
y = np.array([0, 0, 0, 0, 1, 1, 1, 1])
# Create and train model
model = LogisticRegression()
model.fit(X, y)
# Predict result for a student studying 5.5 hours
new_student = np.array([[5.5]])
prediction = model.predict(new_student)
probability = model.predict_proba(new_student)
print("Predicted Class:", prediction[0])
print("Class Probabilities:", probability)Q3: Implement a Decision Tree Classifier
Build a Decision Tree model to classify whether a customer is likely to purchase a product based on age and annual income. Train the model using sample customer data and predict the result for a new customer. Logic: define input features and binary purchase labels, train DecisionTreeClassifier, and use the trained model for prediction.
import numpy as np
from sklearn.tree import DecisionTreeClassifier
# Features: Age and Annual Income (in thousands)
X = np.array([
[22, 25],
[25, 30],
[28, 35],
[35, 45],
[40, 50],
[45, 60],
[50, 70],
[55, 80]
])
# 0 = No Purchase, 1 = Purchase
y = np.array([0, 0, 0, 1, 1, 1, 1, 1])
# Create and train model
model = DecisionTreeClassifier(random_state=42)
model.fit(X, y)
# Predict for a new customer
new_customer = np.array([[38, 48]])
prediction = model.predict(new_customer)
print("Predicted Purchase Class:", prediction[0])Q4: Find Accuracy, Precision, Recall and F1 Score
Given actual and predicted classification results, calculate common machine learning evaluation metrics. Logic: compare the actual labels with model predictions and use scikit-learn metrics to calculate accuracy, precision, recall, and F1 score.
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# Actual values
actual = [1, 1, 0, 1, 0, 0, 1, 0]
# Model predictions
predicted = [1, 1, 0, 0, 0, 1, 1, 0]
accuracy = accuracy_score(actual, predicted)
precision = precision_score(actual, predicted)
recall = recall_score(actual, predicted)
f1 = f1_score(actual, predicted)
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1 Score:", f1)Q5: Build a K-Nearest Neighbors Classification Model
Use K-Nearest Neighbors (KNN) to classify customers based on age and income. Split the dataset into training and testing sets, train a KNN classifier, and evaluate its accuracy. Logic: prepare the features and labels, split the data, create KNeighborsClassifier, train it, make predictions, and calculate accuracy.
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# Features: Age and Income
X = np.array([
[20, 20],
[22, 25],
[25, 30],
[28, 35],
[35, 45],
[40, 50],
[45, 60],
[50, 70],
[55, 75],
[60, 80]
])
# Customer category
# 0 = Low Value, 1 = High Value
y = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
# Create KNN model
model = KNeighborsClassifier(n_neighbors=3)
# Train model
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Predictions:", y_pred)
print("Actual Values:", y_test)
print("Accuracy:", accuracy)Q6: Perform Data Preprocessing Before Machine Learning
Prepare a dataset containing missing values and categorical data before training a machine learning model. Fill missing numerical values, encode the categorical feature, and create a clean feature matrix. Logic: use Pandas to identify missing values, fill them using the mean, and convert categorical values into numerical form using one-hot encoding.
import pandas as pd
# Create sample dataset
data = {
"Age": [22, 25, None, 30, 35],
"Salary": [25000, 30000, 35000, None, 50000],
"Department": ["IT", "HR", "IT", "Sales", "HR"]
}
df = pd.DataFrame(data)
print("Original Dataset:")
print(df)
# Fill missing numerical values
for column in ["Age", "Salary"]:
df[column] = df[column].fillna(df[column].mean())
# One-hot encode Department
encoded_df = pd.get_dummies(df, columns=["Department"], dtype=int)
print("\nPreprocessed Dataset:")
print(encoded_df)Q7: Detect Overfitting Using Training and Testing Accuracy
Train a Decision Tree classifier and compare its training accuracy with testing accuracy. A large difference between training and testing performance can indicate that the model may be overfitting the training data. Logic: split the dataset, train the model, calculate predictions for both training and testing data, and compare the scores.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# Load dataset
iris = load_iris()
X = iris.data
y = iris.target
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
# Create a deep decision tree
model = DecisionTreeClassifier(random_state=42)
model.fit(X_train, y_train)
# Training predictions
train_pred = model.predict(X_train)
# Testing predictions
test_pred = model.predict(X_test)
# Calculate accuracy
train_accuracy = accuracy_score(y_train, train_pred)
test_accuracy = accuracy_score(y_test, test_pred)
print("Training Accuracy:", train_accuracy)
print("Testing Accuracy:", test_accuracy)
if train_accuracy - test_accuracy > 0.10:
print("Possible overfitting detected")
else:
print("No large accuracy gap detected")