0% found this document useful (0 votes)
2 views

ML termwork

The document outlines multiple problem statements for creating Python programs that perform various tasks including statistical calculations, data type demonstrations, control statements, file handling, and clustering algorithms. Each problem statement includes objectives, algorithms, source code, and expected outputs. The tasks range from basic programming concepts to advanced data analysis techniques using libraries like NumPy, SciPy, and Matplotlib.

Uploaded by

Viyan Singh
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)
2 views

ML termwork

The document outlines multiple problem statements for creating Python programs that perform various tasks including statistical calculations, data type demonstrations, control statements, file handling, and clustering algorithms. Each problem statement includes objectives, algorithms, source code, and expected outputs. The tasks range from basic programming concepts to advanced data analysis techniques using libraries like NumPy, SciPy, and Matplotlib.

Uploaded by

Viyan Singh
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
You are on page 1/ 30

Problem Statement 1: Create a list using the input from the user and perform the following task:

• calculate the mean, median and mode.


• Display the list in sorted order and also use loop control statement: continue, pass & break
Objective: Create a Python program that takes user input to build a list, calculates the mean, median, and
mode of the list, displays the sorted list, and demonstrates the usage of continue, pass, and break loop
control statements.
Algorithm:

Source Code:
Output:

Problem Statement 2: Create a list using the input from the user and perform the following task:
• Range, Average Deviation, Absolute Deviations, Squared Deviations, Standard Deviation
Objective: Create a Python program that accepts user input to build a list, calculates the range, average
deviation, absolute deviations, squared deviations, and standard deviation.
Algorithm:

Source Code:
Output:

Problem Statement 3: Calculate outlier in the list.


Objective: Create a Python program that accepts user input to build a list and calculates outliers using the
Interquartile Range (IQR) method.
Algorithm:

Source Code:
Output:
Problem Statement 4: Write a program that demonstrates the use of various data types (integer, float,
string, boolean, list, tuple, set, dictionary) and prints their values and types.
Objective: Create a Python program that demonstrates the usage of basic data types: integer, float, string,
boolean, list, tuple, set, and dictionary. The program should print the value and type of each data type.
Algorithm:

Source Code:
Output:

Problem Statement 5: Write a program that demonstrates arithmetic, comparison, and logical operators
along with operator precedence in Python.
Objective: Create a Python program that demonstrates the use of arithmetic, comparison, and logical
operators, along with showcasing operator precedence.
Algorithm:

Source Code:
Output:
Problem Statement 6: Write a program that demonstrates how to convert between different data types
(e.g., integer to string, string to integer, etc.).
Objective: Create a Python program that illustrates conversions between integers, floats, strings, booleans,
and collections such as lists, tuples, and sets.
Algorithm:

Source Code:
Output:
Problem Statement 7: Write a program that takes input from the user for their name and age, then prints a
greeting message using that information.
Objective: Create a Python program that takes input for the user's name and age and prints a personalized
greeting message.
Algorithm:

Source Code:

Output:
Problem Statement 8: Write a program that demonstrates both single-line and multi-line comments in
Python.
Objective: Create a Python program that demonstrates the use of single-line and multi-line comments
effectively.
Algorithm:

Source Code:

Output:
Problem Statement 9: Write a program that imports the built-in math module and uses it to calculate the
square root of a number and print the value of pi
Objective: Create a Python program that imports the math module, calculates the square root of a user-
provided number, and prints the value of pi.
Algorithm:

Source Code:

Output:
Problem Statement 10: Write a program that demonstrates control statements using if-else, for loop, and
while loop. • If-Else Statement • For Loop • While Loop
Objective: Create a Python program that demonstrates:
1. Conditional control using an if-else statement.
2. Iteration using a for loop.
3. Iteration using a while loop.
Algorithm:

Source Code:
num = int(input("Enter a number: "))
print("Positive" if num > 0 else "Zero/Negative")

print("\nSquares of numbers:")
for item in range(1, 6): # Example range
print(item**2, end=", ")

print("\nCountdown:")
count = 3
while count > 0:
print(count, end=", ")
count -= 1
print("Go!")

Output:
Problem Statement 11: Write a program that defines a function to greet a user and then calls that function.
Objective: Write a Python program that defines a function to greet a user by their name and then calls that
function to display the greeting.
Algorithm:

Source Code:
def greet_user(name):
print(f"Hello, {name}! Welcome!")

user_name = input("Enter your name: ")

greet_user(user_name)

Output:
Problem Statement 12: Write a program that demonstrates the use of some Python built-in functions like
abs(), max(), min(), len(), and round().
Objective: Write a Python program that demonstrates the usage of built-in functions: abs(), max(), min(),
len(), and round().
Algorithm:

Source Code:
number, float_number, numbers_list = -42, 3.14159, [10, 20, 30, 5, 15]

print(f"Absolute: {abs(number)}, Max: {max(numbers_list)}, Min: {min(numbers_list)}")


print(f"Length: {len(numbers_list)}, Rounded: {round(float_number)}, Rounded (2dp):
{round(float_number, 2)}")

Output:

Problem Statement 13: Write a program that imports the random module and generates a random integer
between 1 and 100.
Objective: Write a Python program that imports the random module and generates a random integer
between 1 and 100.
Algorithm:

Source Code:
import random

random_number = random.randint(1, 100)


print(f"The random number generated is: {random_number}")

Output:
Problem Statement 14: Write a program that demonstrates file handling by performing the following
operations: a) Create a file b) Writing data to a file c) Reading data from the file d) Appending new data to
the file e) Reading the file line by line
Objective: Write a Python program that performs the following file handling operations:
1. Create a file. 2.Write data to the file. 3. Read data from the file. 4. Append new data to the file. 5.
Read the file line by line.
Algorithm:

Source Code:
with open("example.txt", "w") as file:
file.write("This is the first line of the file.\n")
file.write("This is the second line of the file.\n")

with open("example.txt", "r") as file:


content = file.read()
print("Content of the file after writing:")
print(content)

with open("example.txt", "a") as file:


file.write("This is an appended line.\n")

with open("example.txt", "r") as file:


print("Reading the file line by line:")
for line in file:
print(line.strip())

Output:
Problem Statement 15: Write a Python program to perform K-Means clustering on a given dataset and
visualize the clustered data points along with their centroids.
Objective: Write a Python program to:
1. Perform K-Means clustering on a given dataset.
2. Visualize the clustered data points and their centroids using Matplotlib.
Algorithm:

Source Code:
Output:

Problem Statement 16: Implement DBScan algorithm using Python on a given dataset.
Objective: To implement the DBSCAN clustering algorithm on a dataset to identify clusters of varying
shape and size, and mark outliers/noise.
Algorithm:

Source Code:

Output:

Problem Statement 17: Implement PCA on a given dataset iris and reduce it to 2 principal components.
Objective: To perform dimensionality reduction on the Iris dataset using PCA and reduce the features from
4 to 2 while preserving as much variance as possible.
Algorithm:

Source Code:

Output:
Problem Statement 18: Write a program to extract the features from a given dataset using PCA and then
forward the extracted features to K-means clustering algorithm for cluster generation
Objective: Reduce high-dimensional data using PCA, then cluster the data using K-Means on the extracted
principal components.
Algorithm:

Source Code:

Output:
Problem Statement 19: Write a program to extract the features from a given dataset using PCA and then
forward the extracted features to DBSCAN clustering algorithm for cluster generation.
Objective: Extract meaningful features using PCA and apply DBSCAN clustering to detect dense regions
(clusters) in the reduced feature space.
Algorithm:

Source Code:

Output:

Problem Statement 20: Write a program to demonstrate the uses of NumPy and SciPy.
Objective: Showcase basic operations using NumPy (array manipulation, math) and SciPy (scientific
computing: integration, optimization).
Algorithm:

Source Code:

Output:

Problem Statement 21: Write a program to demonstrate the uses of Matplotlib and Scikit Learn.
Objective: Use Scikit-learn to train a machine learning model, and Matplotlib to visualize the results.
Algorithm:

Source Code:

Output:

Figure 2 Accuracy with 3 neighbors: 0.733 Figure 1 Accuracy with 5 neighbors:


0.733

Problem Statement 21: Write a program to demonstrate the uses of Pandas.


Objective: Show how to load, manipulate, and visualize data using Pandas.
Algorithm:

Source Code:

Output:

Problem Statement 21: Write a program to implement Linear Regression in a given dataset and also
calculate the test accuracy.
Objective: Train a Linear Regression model on a dataset and evaluate it using test accuracy (R² score).
Algorithm:

Source Code:

Output:

Figure 4 All Features → R²: 0.4773 1 Feature (BMI) → R²: 0.2803


Figure 3 All Features → R²: 0.47731 Feature
(BMI) → R²: 0.0643

You might also like