0% found this document useful (0 votes)
3 views11 pages

ML Practical File

The document outlines three machine learning experiments: Simple Linear Regression using a salary dataset, Multiple Linear Regression using a startup dataset, and Logistic Regression using the Titanic dataset. Each experiment includes objectives, theoretical background, datasets, Python programs, expected outputs, and conclusions about the model's performance. The results indicate strong predictive capabilities for salary based on experience, startup profit based on multiple features, and passenger survival on the Titanic.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

ML Practical File

The document outlines three machine learning experiments: Simple Linear Regression using a salary dataset, Multiple Linear Regression using a startup dataset, and Logistic Regression using the Titanic dataset. Each experiment includes objectives, theoretical background, datasets, Python programs, expected outputs, and conclusions about the model's performance. The results indicate strong predictive capabilities for salary based on experience, startup profit based on multiple features, and passenger survival on the Titanic.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MACHINE LEARNING

PRACTICAL FILE

Subject Machine Learning Lab

Experiments 1. Linear Regression (Salary Dataset) 2.


Multiple Linear Regression 3. Logistic
Regression (Titanic Dataset)

Academic Year 2025-26


Experiment 1: Linear Regression using Salary Dataset

Experiment No. 1
Title Linear Regression using Salary Dataset
Objective To implement Simple Linear Regression to predict salary based on
years of experience
Tools / IDE Python 3.x, Jupyter Notebook / VS Code
Libraries NumPy, Pandas, Matplotlib, Scikit-Learn

Theory
Linear Regression is a supervised machine learning algorithm used to model the relationship between
a dependent variable (target) and one independent variable (feature). It fits a straight line through the
data points that minimizes the sum of squared differences between observed and predicted values.

Mathematical Formula
The equation of Simple Linear Regression is:
y = b0 + b1 * x
Where:
• y = Dependent variable (Salary)
• x = Independent variable (Years of Experience)
• b0 = Y-intercept (bias)
• b1 = Slope (coefficient)

Cost Function – Mean Squared Error (MSE)


MSE = (1/n) * Σ(y_actual - y_predicted)²

Dataset
The dataset contains two columns: YearsExperience and Salary. It has approximately 30 records
representing real-world employee data.

YearsExperience Salary (Actual) Description


1.1 39343 Entry Level
1.3 46205 Entry Level
3.2 54445 Junior
5.1 66029 Mid Level
7.9 83088 Senior
10.5 116969 Expert

Python Program
# Experiment 1: Linear Regression using Salary Dataset

import numpy as np
import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score

# Step 1: Load Dataset


dataset = pd.read_csv('salary_data.csv')
X = [Link][:, :-1].values # Years of Experience
y = [Link][:, 1].values # Salary

# Step 2: Split into Training and Test sets (80/20)


X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0)

# Step 3: Train the Linear Regression model


regressor = LinearRegression()
[Link](X_train, y_train)

# Step 4: Predict on Test Set


y_pred = [Link](X_test)

# Step 5: Evaluate the Model


print('Intercept (b0):', regressor.intercept_)
print('Coefficient (b1):', regressor.coef_)
print('R-Squared Score:', r2_score(y_test, y_pred))
print('MSE:', mean_squared_error(y_test, y_pred))

# Step 6: Visualise Training Set Results


[Link](X_train, y_train, color='red', label='Actual')
[Link](X_train, [Link](X_train), color='blue', label='Predicted')
[Link]('Salary vs Experience (Training Set)')
[Link]('Years of Experience')
[Link]('Salary')
[Link]()
[Link]()
Expected Output
Sample output from running the program:
Intercept (b0): 26780.09
Coefficient (b1): [9312.57]
R-Squared Score: 0.9749
MSE: 31270951.72

Conclusion
In this experiment, we successfully implemented Simple Linear Regression using the Salary dataset.
The model achieved an R² score of approximately 0.97, indicating a strong linear relationship between
years of experience and salary. The regression line fits the data well, confirming that linear regression
is an effective model for this type of prediction.
Experiment 2: Linear Regression using Multiple Independent
Variables

Experiment No. 2
Title Linear Regression using Multiple Independent Variables
Objective To implement Multiple Linear Regression to predict a target variable
using several independent features
Tools / IDE Python 3.x, Jupyter Notebook / VS Code
Libraries NumPy, Pandas, Matplotlib, Scikit-Learn

Theory
Multiple Linear Regression (MLR) is an extension of Simple Linear Regression that models the
relationship between two or more independent variables (features) and a single dependent variable
(target). It fits a hyperplane through the data in multi-dimensional space.

Mathematical Formula
y = b0 + b1*x1 + b2*x2 + b3*x3 + ... + bn*xn
Where:
• y = Dependent variable (e.g., Profit)
• x1, x2, ..., xn = Independent variables (e.g., R&D Spend, Administration, Marketing)
• b0 = Intercept
• b1, b2, ..., bn = Coefficients for each feature

Assumptions of Multiple Linear Regression


• Linearity: The relationship between features and target is linear.
• No Multicollinearity: Independent variables should not be highly correlated.
• Homoscedasticity: Constant variance of errors.
• Normality: Residuals should be normally distributed.

Dataset Description
We use the 50 Startups dataset which includes R&D Spend, Administration cost, Marketing Spend,
State (encoded), and Profit as the target variable.

R&D Spend Administration Marketing Spend Profit


165349.20 136897.80 471784.10 192261.83

162597.70 151377.59 443898.53 191792.06

153441.51 101145.55 407934.54 191050.39

144372.41 118671.85 383199.62 182901.99

142107.34 91391.77 366168.42 166187.94

Python Program
# Experiment 2: Multiple Linear Regression

import numpy as np
import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import LabelEncoder, OneHotEncoder
from [Link] import ColumnTransformer
from [Link] import r2_score, mean_squared_error

# Step 1: Load Dataset


dataset = pd.read_csv('50_Startups.csv')
X = [Link][:, :-1].values
y = [Link][:, 4].values

# Step 2: Encode Categorical Variable (State column)


ct = ColumnTransformer(
transformers=[('encoder', OneHotEncoder(), [3])],
remainder='passthrough')
X = [Link](ct.fit_transform(X))

# Step 3: Avoid Dummy Variable Trap


X = X[:, 1:]

# Step 4: Split Dataset into Training and Test sets


X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0)

# Step 5: Train the Multiple Linear Regression model


regressor = LinearRegression()
[Link](X_train, y_train)

# Step 6: Predict on Test Set


y_pred = [Link](X_test)

# Step 7: Compare Actual vs Predicted


comparison = [Link]((y_pred.reshape(-1,1),
y_test.reshape(-1,1)), axis=1)
print('Actual vs Predicted:')
print(comparison)

# Step 8: Evaluate
print('R-Squared Score:', r2_score(y_test, y_pred))
print('MSE:', mean_squared_error(y_test, y_pred))

# Step 9: Visualise
[Link](range(len(y_test)), y_test, label='Actual', alpha=0.7)
[Link](range(len(y_pred)), y_pred, label='Predicted', alpha=0.7)
[Link]('Multiple Linear Regression: Actual vs Predicted Profit')
[Link]('Test Sample Index')
[Link]('Profit')
[Link]()
[Link]()

Expected Output
Actual vs Predicted:
[[103282.38 103282.38]
[144259.40 144259.40]
[146121.95 146121.95]
...]
R-Squared Score: 0.9347
MSE: 8.35e+07

Conclusion
In this experiment, Multiple Linear Regression was applied to predict startup profit using R&D Spend,
Administration, Marketing Spend, and State. The model achieved an R² score of approximately 0.93,
indicating a good fit. Encoding the categorical 'State' variable using One-Hot Encoding was a crucial
preprocessing step to enable the model to process non-numeric data.
Experiment 3: Logistic Regression using Titanic Dataset

Experiment No. 3
Title Logistic Regression using Titanic Dataset
Objective To implement Logistic Regression to predict passenger survival on the
Titanic
Tools / IDE Python 3.x, Jupyter Notebook / VS Code
Libraries NumPy, Pandas, Matplotlib, Seaborn, Scikit-Learn

Theory
Logistic Regression is a supervised classification algorithm used when the target variable is categorical
(binary or multi-class). Unlike Linear Regression which predicts continuous values, Logistic Regression
predicts the probability that an instance belongs to a particular class, and uses a threshold (typically
0.5) to make the final prediction.

Sigmoid / Logistic Function


P(y=1|x) = 1 / (1 + e^(-z))
where z = b0 + b1*x1 + b2*x2 + ...
The sigmoid function maps any real-valued number to a value between 0 and 1. This output represents
the probability of belonging to class 1 (Survived = 1 in our case).

Decision Boundary
If P >= 0.5 → Predict Survived (1)
If P < 0.5 → Predict Not Survived (0)

Loss Function – Binary Cross Entropy


Loss = -(1/n) * Σ[y*log(p) + (1-y)*log(1-p)]

Dataset Description
The Titanic dataset contains demographic and travel information about passengers. Key features used
for prediction:

Feature Description
Survived Target: 0 = Not survived, 1 = Survived
Pclass Ticket class (1st, 2nd, 3rd)
Sex Gender of passenger (encoded)
Age Age of passenger in years
SibSp Number of siblings/spouses aboard
Parch Number of parents/children aboard
Fare Passenger fare paid
Embarked Port of embarkation (C/Q/S)

Python Program
# Experiment 3: Logistic Regression using Titanic Dataset

import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import StandardScaler
from [Link] import (accuracy_score, confusion_matrix,
classification_report)

# Step 1: Load Dataset


dataset = pd.read_csv('[Link]')
print([Link]())
print([Link]().sum())

# Step 2: Data Preprocessing


# Drop irrelevant columns
[Link](['Name', 'Ticket', 'Cabin', 'PassengerId'], axis=1, inplace=True)

# Fill missing Age with median


dataset['Age'].fillna(dataset['Age'].median(), inplace=True)

# Fill missing Embarked with mode


dataset['Embarked'].fillna(dataset['Embarked'].mode()[0], inplace=True)

# Encode Sex: male=0, female=1


dataset['Sex'] = dataset['Sex'].map({'male': 0, 'female': 1})

# One-Hot Encode 'Embarked'


dataset = pd.get_dummies(dataset, columns=['Embarked'], drop_first=True)

# Step 3: Feature Selection


X = [Link]('Survived', axis=1)
y = dataset['Survived']
# Step 4: Split Dataset (80/20)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)

# Step 5: Feature Scaling


sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = [Link](X_test)

# Step 6: Train the Logistic Regression model


classifier = LogisticRegression(max_iter=1000, random_state=42)
[Link](X_train, y_train)

# Step 7: Predict on Test Set


y_pred = [Link](X_test)

# Step 8: Evaluate the Model


print('Accuracy Score:', accuracy_score(y_test, y_pred))
print('Confusion Matrix:')
print(confusion_matrix(y_test, y_pred))
print('Classification Report:')
print(classification_report(y_test, y_pred))

# Step 9: Visualise Confusion Matrix


cm = confusion_matrix(y_test, y_pred)
[Link](cm, annot=True, fmt='d', cmap='Blues',
xticklabels=['Not Survived','Survived'],
yticklabels=['Not Survived','Survived'])
[Link]('Confusion Matrix – Titanic Logistic Regression')
[Link]('Predicted')
[Link]('Actual')
[Link]()

Expected Output
Accuracy Score: 0.8156

Confusion Matrix:
[[93 12]
[21 53]]

Classification Report:
precision recall f1-score support
0 0.82 0.89 0.85 105
1 0.82 0.72 0.76 74
accuracy 0.82 179
Confusion Matrix Interpretation
Term Formula Value Meaning

True Positive (TP) Survived, predicted 53 Correctly predicted


Survived survivors

True Negative (TN) Not survived, predicted 93 Correctly predicted non-


Not survived survivors

False Positive (FP) Not survived, predicted 12 Type I Error


Survived

False Negative (FN) Survived, predicted Not 21 Type II Error


survived

Accuracy (TP+TN)/(Total) ~81.6% Overall correctness

Conclusion
In this experiment, Logistic Regression was implemented to classify Titanic passengers as survived or
not survived. After preprocessing (handling missing values, encoding categorical variables, and feature
scaling), the model achieved an accuracy of approximately 81.6%. The confusion matrix confirms the
model's effectiveness in distinguishing between the two classes. Logistic Regression is well-suited for
binary classification problems like this survival prediction task.

You might also like