Py Assignment
Py Assignment
Py Assignment
Ex 1: A = [[1 3 4]
[2 5 7]
[5 9 6]]
B = [[1 0 0]
[0 1 0]
[0 0 1]]
A*B = [[1 3 4]
[2 5 7]
[5 9 6]]
Ex 2: A = [[1 2]
[3 4]]
B = [[1 2 3 4 5]
[5 6 7 8 9]]
A*B = [[11 14 17 20 23]
[23 30 36 42 51]]
Ex 3: A = [[1 2]
[3 4]]
B = [[1 4]
[5 6]
[7 8]
[9 6]]
A*B =Not possible
In [ ]: A = [[1,2],
[3,4]]
B = [[1,2,3,4,5],
[5,6,7,8,9]]
temp_row = len(B[0])*[0]
for r in range(len(A)):
result.append(temp_row[:])
for row_index in range(len(A)):
for col_index in range(len(B[0])):
sum=0
for k in range(len(A[0])):
sum=sum+A[row_index][k]*B[k][col_index]
result[row_index][col_index]=sum
return result
matrix_mul(A, B)
Out[ ]: [[11, 14, 17, 20, 23], [23, 30, 37, 44, 51]]
Q2: Select a number randomly with probability proportional to its magnitude from the given array of n
elements
consider an experiment, selecting an element from the list A randomly with probability proportional to its magnitude. assume we are doing
the same experiment for 100 times with replacement, in each experiment you will print a number that is selected randomly from A.
Ex 1: A = [0 5 27 6 13 28 100 45 10 79]
let f(x) denote the number of times x getting selected in 100 experiments.
f(100) > f(79) > f(45) > f(28) > f(27) > f(13) > f(10) > f(6) > f(5) > f(0)
def pick_a_number_from_list(A):
start_value = 0
scale = sum(A)
rand_value = uniform(0,scale)
for value in A:
def sampling_based_on_magnitued():
A=[0, 5, 27, 6, 13, 28, 100, 45, 10, 79]
for i in range(1,100):
number = pick_a_number_from_list (A)
print(number)
sampling_based_on_magnitued()
100
100
28
10
79
6
100
28
10
100
45
45
45
28
45
45
79
79
10
10
79
100
10
100
45
79
79
100
45
100
79
79
28
27
100
100
100
45
100
6
100
100
6
28
100
79
45
27
45
import re
#user input
string = str ('a2b3c4')
###
your task is to print the name of students a. Who got top 5 ranks, in the descending order of marks
b. Who got least 5 ranks, in the increasing order of marks
d. Who got marks between >25th percentile <75th percentile, in the increasing order of marks
Ex 1:
Students=['student1','student2','student3','student4','student5','student6','student7','student
8','student9','student10']
Marks = [45, 78, 12, 14, 48, 43, 47, 98, 35, 80]
a.
student8 98
student10 80
student2 78
student5 48
student7 47
b.
student3 12
student4 14
student9 35
student6 43
student1 45
c.
student9 35
student6 43
student1 45
student7 47
student5 48
In [ ]: Students=['student1','student2','student3','student4','student5',
'student6','student7','student8','student9','student10']
Marks = [45, 78, 12, 14, 48, 43, 47, 98, 35, 80]
max5=[]
min5=[]
avg=[]
#A. Top 5 rank in decending order
def top_5rank(score):
print('A.')
while len(max5)<=4:
max5.append(max(score))
print(Students[Marks.index(max(score))], max(score))
score=[i for i in score if i not in max5]
top_5rank(Marks)
last_5rank(Marks)
avg_rank(Marks)
A.
student8 98
student10 80
student2 78
student5 48
student7 47
B.
student3 12
student4 14
student9 35
student6 43
student1 45
C.
student9 35
student6 43
student1 45
student7 47
student5 48
Ex:
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1)(6,0),(1,-1)]
P= (3,-4)
Output:
(6,-7)
(1,-1)
(6,0)
(-5,-8)
(-1,-1)
In [ ]: import math
for point in S:
temp_closest_points_to_p = float('inf')
# cosine formula
den = math.sqrt((point[0]**2)*(P[0]**2))+math.sqrt((point[1]**2)*(P[1]**2))
num = point[0]*P[0]+point[1]*P[1]
if den !=0:
temp_closest_points_to_p = math.acos(num/den)
all_closest_points_to_p.append((temp_closest_points_to_p, point))
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
P= (3,-4)
points = closest_points_to_p(S, P)
print('output:',*[point for point in points], sep='\n') #print the returned values
output:
(6, -7)
(6, 0)
(1, -1)
(-5, -8)
(-1, -1)
Red =[(R11,R12),(R21,R22),(R31,R32),(R41,R42),(R51,R52),..,(Rn1,Rn2)]
Blue=[(B11,B12),(B21,B22),(B31,B32),(B41,B42),(B51,B52),..,(Bm1,Bm2)]
and set of line equations(in the string formate, i.e list of strings)
your task is to for each line that is given print "YES"/"NO", you will print yes, if all the red points are one side of the line and blue points are
other side of the line, otherwise no
Ex:
Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]
Output:
YES
NO
NO
YES
In [ ]: import math
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input strings
if (eval (line.replace('x','*%s'%red[0][0]).replace('y','*%s'%red[0][1]))>0):
red_side = 1
for i in Lines:
yes_or_no = i_am_the_one(Red, Blue, i)
print(yes_or_no) # the returned value
yes
no
no
yes
Ex 1: _, _, _, 24 ==> 24/4, 24/4, 24/4, 24/4 i.e we. have distributed the 24 equally to all 4 pl
aces
Ex 3: 80, _, _, _, _ ==> 80/5,80/5,80/5,80/5,80/5 ==> 16, 16, 16, 16, 16 i.e. the 80 is distrib
uted qually to all 5 missing values that are right to it
Ex 4: _, _, 30, _, _, _, 50, _, _
==> we will fill the missing values from left to right
a. first we will distribute the 30 to left two missing values (10, 10, 10, _, _, _, 50, _,
_)
b. now distribute the sum (10+50) missing values in between (10, 10, 12, 12, 12, 12, 12, _,
_)
c. now we will distribute 12 to right side missing values (10, 10, 12, 12, 12, 12, 4, 4, 4)
for a given string with comma seprate values, which will have both missing values numbers like ex: "_, _, x, _, _, _" you need fill the missing
values Q: your program reads a string like ex: "_, _, x, _, _, _" and returns the filled sequence Ex:
Input1: "_,_,_,24"
Output1: 6,6,6,6
Input2: "40,_,_,_,60"
Output2: 20,20,20,20,20
Input3: "80,_,_,_,_"
Output3: 16,16,16,16,16
Input4: "_,_,30,_,_,_,50,_,_"
Output4: 10,10,12,12,12,12,4,4,4
start = 0
for ele in x:
initial_value = int (str_x[ele]) if str_x[ele] != '_' else 0
initial_value += int (str_x[start]) if str_x[start] != '_' and start != ele else 0
dist = initial_value//(ele-start+1)
str_x = [dist if start<=z<=ele else str_x[z] for z in range(len(str_x))]
start = ele
return str_x
s1 = "_,_,30,_,_,_,50,_,_"
s2 = "_,_,_,24"
s3 = "40,_,_,_,60"
s4 = "80,_,_,_,_"
A=smoothed_values= curve_smoothing(s1)
B=smoothed_values= curve_smoothing(s2)
C=smoothed_values= curve_smoothing(s3)
D=smoothed_values= curve_smoothing(s4)
print(A,'\n',B,'\n',C,'\n',D)
1. the first column F will contain only 5 uniques values (F1, F2, F3, F4, F5)
2. the second column S will contain only 3 uniques values (S1, S2, S3)
Ex:
[[F1,S1],[F2,S2],[F3,S3],[F1,S2],[F2,S3],[F3,S2],[F2,S1],[F4,S1],[F4,S3],[F5,S1]]
A = [['F1','S1'],['F2','S2'],['F3','S3'],['F1','S2'],['F2','S3'],['F3','S2'],['F2','S1'],['F4','S1'],[
'F4','S3'],['F5','S1']]
compute_conditional_probabilites(A)
Ex:
a = len(set_s1.intersection(set_s2))
b = set_s1-set_s2
c = set_s2-set_s1
return a, b, c
7
{'F', '5', 'first'}
{'3', 'S', 'second'}
Ex:
[[1, 0.4], [0, 0.5], [0, 0.9], [0, 0.3], [0, 0.6], [1, 0.1], [1, 0.9], [1, 0.8]]
output:
0.4243099
−1
⋅ ((1 ⋅ log10 (0.4) + 0 ⋅ log10 (0.6)) + (0 ⋅ log10 (0.5) + 1 ⋅ log10 (0.5))+. . . +(1 ⋅ log10 (0.8) + 0 ⋅ log10 (0.2)))
8
return loss
A = [[1, 0.4], [0, 0.5], [0, 0.9], [0, 0.3], [0, 0.6], [1, 0.1], [1, 0.9], [1, 0.8]]
loss = compute_log_loss(A)
print(loss)
0.42430993457031635