DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
WORKSHEET 2.2
Student Name: Ankit Vashisth UID: 22BCS13378
Branch: CSE Section/Group: 707-A
Semester: 4th Date of Performance: 14-03-2024
Subject Name: Python Subject Code: 22CSH-259
1. Aim: Imagine you are a sales analyst for retail company. Your goal is
to develop a model that accurately predict sales trends over time gather
his trial sales data which can be month/years.
a) Evaluate the accuracy of sales prediction by company
interpolated sales figures. Adjust the integrate curve model
approx values upto 4 precision.
b) Plot the interpolated roles figures along actual data. Use
Newton divided interpolation method.
2. Source Code: import numpy as np
import matplotlib.pyplot as plt
# Define the Newton divided interpolation function
def newton_divided_interpolation(x, y): n =
len(x)
coefficients = np.zeros(n)
for i in range(n):
coefficients[i] = y[i] for j in
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
range(1, n): for i in
range(n - 1, j - 1, -1):
coefficients[i] = (coefficients[i] - coefficients[i - 1]) / (x[i] - x[i - j])
return coefficients
# Define a function to evaluate the interpolated value at a given point def
evaluate_interpolated_value(coefficients, x_values, x):
n = len(coefficients)
interpolated_value = coefficients[0]
for i in range(1, n):
term = coefficients[i]
for j in range(i):
term *= (x - x_values[j])
interpolated_value += term
return interpolated_value
# Actual sales data
actual_months = [1, 2, 3, 4, 5] # Months
actual_sales = [100, 150, 200, 250, 300] # Sales figures
# Interpolated sales data
interpolated_months = [1, 2, 3, 4, 5] # Months
interpolated_sales = [100, 150, 200, 250, 300] # Interpolated sales figures
# Evaluate coefficients for interpolation
coefficients = newton_divided_interpolation(actual_months, actual_sales)
# Evaluate interpolated sales figures
interpolated_sales_predicted = [evaluate_interpolated_value(coefficients,
actual_months, month) for month in interpolated_months]
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
# Round interpolated sales figures to 4 decimal places
interpolated_sales_predicted = [round(value, 4) for value in
interpolated_sales_predicted]
# Plotting the actual and interpolated sales figures plt.plot(actual_months,
actual_sales, 'bo-', label='Actual Sales') plt.plot(interpolated_months,
interpolated_sales_predicted, 'r^-', label='Interpolated Sales')
plt.title('Actual vs Interpolated Sales')
plt.xlabel('Month') plt.ylabel('Sales')
plt.legend() plt.grid(True)
plt.show()
OUTPUT:-