Kartik ML Q1-8
Kartik ML Q1-8
Problem Statement 1 :- Create a Data Frame using dictionary containing students’ marks
details with columns Student_ID, Student_Name, Gender, Sub1, Sub2, Sub3 with the marks of
20 students.
Or you can create an excel file for the same and import it.
ALGORITHM:-
"S10", "S11", "S12", "S13", "S14", "S15", "S16", "S17", "S18", "S19", "S20"],
"Gender": ["M", "M", "M", "F", "F", "M", "M", "F", "F", "F", "M", "F", "M",
"Sub1": [85, 78, 92, 88, 75, 90, 84, 79, 95, 87, 76, 89, 91, 80, 83, 77, 86,
"Sub2": [80, 72, 90, 85, 70, 88, 82, 74, 92, 84, 73, 87, 89, 78, 81, 75, 83,
79, 93, 77],"Sub3": [82, 74, 91, 86, 72, 89, 83, 76, 93, 85, 74, 88, 90, 79, 82, 76, 84,
1
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
df = pd.DataFrame(students_data)
#print(df)
",sum_sub1) count_sub1 =
median_marks = df[["Sub1","Sub2","Sub3"]].median()
Output :-
2
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
Problem Statement 2 :- Define two matrices. Find their sum, difference, transpose and
product of two matrices.
3
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
ALGORITHM:-
Step 2: - Take first and second matrices details from users like row, column.
Code :-
[ ADDITION ] import
numpy as np def
input_matrix(rows, cols):
r1 == r2 and c1 == c2:
4
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
print_matrix(add) else:
Output : -
[ SUBTRACTION ]
in range(cols):
== c2: print("SUBTRACTION")
print_matrix(subtract) else:
print("Error: Matrices must have the same dimensions for subtraction.") Output
:-
6
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
[ MULTIPLICATION]
input_matrix(rows, cols):
range(cols):
print("MULTIPLICATION")
print_matrix(product)
7
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
else: print("Error: The number of columns in the first matrix must equal the number of
rows in the second matrix for multiplication.")
Output :-
8
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
[ TRANSPOSE ]
print_matrix(matrix)
print_matrix(transposed_matrix)
Output :-
9
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
Problem Statement 3 :- Construct the classification model by using K-NN algorithm using
Iris species data set. Take the value of k=3 , train the model by using 70 % data also print the
classification accuracy.
10
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
ALGORITHM:-
df = pd.read_csv("/content/Iris.csv")
df['Species'] data =
df.drop(['Species','Id'],axis=1)
x_train,x_test,y_train,y_test =
train_test_split(data,label,test_size=0.35,random_state=0)
Output :-
11
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
Problem Statement 4 :- Write a Python Program to read a file line by line and store it into a
list.
12
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
ALGORITHM:-
Step 3: - give file name and second variable is mode of file which is write -> “w”.
Step 4: - read file using “r” mode with with open so that automatically close the file. Code
:-
f = open("/content/file.txt", 'w')
f.write("BCA") print("Content
Output :-
13
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
b) Rename file
c) Delete file
ALGORITHM:-
Step 3: - give file name and second variable is mode of file which is write -> “r”.
Code :-
a) f = open('/content/file.txt','r')
f.read()
b) f = open('/content/file.txt','w')
f.write("Python is Important")
f.close()
c) import os os.remove('/content/file.txt')
Output :-
Problem Statement 6 :- Write a Python program to count the number of lines in a text file
using enumerate.
14
Name – Kartik Khatri Course - BCA (6th Sem)
Section - B1 Student Id - 22151859
Roll No - 25 Subject code - PBC-602 (Machine Learning Lab )
ALGORITHM:-
Step 1: - first create a file.
Step 2: - create read_file() function .
Step 3: - give file name and second variable is mode of file which is write -> “r”.
Step 4: - return file sum(1 for _, _ in enumerate(file, 1)).
f.write("BCA") print("Content
lines = file.readlines()
print(lines) def
count_line(filename): with
of lines is {line_count}")
15
Name – Ayush Ghildiyal Course - BCA (6th Sem)
Section - A1 Student Id - 22151811
Roll No - 28 (2221311) Subject code - PBC-602 (Machine Learning Lab )
16
Name – Ayush Ghildiyal Course - BCA (6th Sem)
Section - A1 Student Id - 22151811
Roll No - 28 (2221311) Subject code - PBC-602 (Machine Learning Lab )
Output :-
ALGORITHM:-
Step 4: - check first and last letter of the input and decreases to meet at middle way
// 2):
return False
return True
if isPalindrome(user): print("Yes! It is a
Palindrome")
17
Name – Ayush Ghildiyal Course - BCA (6th Sem)
Section - A1 Student Id - 22151811
Roll No - 28 (2221311) Subject code - PBC-602 (Machine Learning Lab )
18
Name – Ayush Ghildiyal Course - BCA (6th Sem)
Section - A1 Student Id - 22151811
Roll No - 28 (2221311) Subject code - PBC-602 (Machine Learning Lab )
Output :-
ALGORITHM:-
if n == 0 or n == 1:
return 1
else:
19
Name – Ayush Ghildiyal Course - BCA (6th Sem)
Section - A1 Student Id - 22151811
Roll No - 28 (2221311) Subject code - PBC-602 (Machine Learning Lab )
20