0% found this document useful (0 votes)
160 views1 page

D.jothi8@gmail - Com 2

The document discusses matrix multiplication and sampling based on probability. It contains code to: 1) Take user input for matrices A and B, multiply them using a function, and print the result. 2) Take user input for a list, sample from it randomly based on element frequencies, calculate probabilities, and print results.

Uploaded by

Jothi D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views1 page

D.jothi8@gmail - Com 2

The document discusses matrix multiplication and sampling based on probability. It contains code to: 1) Take user input for matrices A and B, multiply them using a function, and print the result. 2) Take user input for a list, sample from it randomly based on element frequencies, calculate probabilities, and print results.

Uploaded by

Jothi D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

In [1]: #Matrix multiplication

import sys
product=[]

def mat_mul(A,B):
# iterate through rows of A
for i in range(len(A)):
# iterate through columns of B
for j in range(len(B[0])):
# iterate through rows of B
product[i][j]=0
for k in range(len(B)):
product[i][j]+=A[i][k]*B[k][j]
return product

a=[]
b=[]
ar=input("Enter number of rows for matrix A:")
ar1=int(ar)
ac=input("Enter number of columns for matrix A:")
ac1=int(ac)
br=input("Enter number of rows for matrix B:")
br1=int(br)
bc=input("Enter number of columns for matrix B:")
bc1=int(bc)

if ac1!=br1 :
print("MATRIX MULTIPLICATION NOT POSSIBLE")
sys.exit();

print("***get element for first matrix A ***")


for i in range(0,ar1):
a.append([]) #output - [[], [], []]

for i in range(0,ar1):
for j in range(0,ac1):
a[i].append(j)
a[i][j]=0 #output - [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for i in range(0,ar1):
for j in range(0,ac1):
print('entey in row:',i+1,'entry in column:',j+1)
a[i][j]=int(input())

print("Matrix A is:",a)

print("get element for second matrix B")


for i in range(0,br1):
b.append([]) #output - [[], [], []]

for i in range(0,br1):
for j in range(0,bc1):
b[i].append(j)
b[i][j]=0 #output - [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for i in range(0,br1):
for j in range(0,bc1):
print('entey in row:',i+1,'entry in column:',j+1)
b[i][j]=int(input())

print("Matrix B is:",b)

for l in range(0,ar1):
product.append([])
for i in range(0,ar1):
for j in range(0,bc1):
product[i].append(j)
product[i][j]=0

print("Matrix multiplication of {0} and {1} is:{2}".format(a,b,mat_mul(a,b)))

Enter number of rows for matrix A:2


Enter number of columns for matrix A:2
Enter number of rows for matrix B:2
Enter number of columns for matrix B:5
***get element for first matrix A ***
entey in row: 1 entry in column: 1
1
entey in row: 1 entry in column: 2
2
entey in row: 2 entry in column: 1
3
entey in row: 2 entry in column: 2
4
Matrix A is: [[1, 2], [3, 4]]
get element for second matrix B
entey in row: 1 entry in column: 1
1
entey in row: 1 entry in column: 2
2
entey in row: 1 entry in column: 3
3
entey in row: 1 entry in column: 4
4
entey in row: 1 entry in column: 5
5
entey in row: 2 entry in column: 1
5
entey in row: 2 entry in column: 2
6
entey in row: 2 entry in column: 3
7
entey in row: 2 entry in column: 4
8
entey in row: 2 entry in column: 5
9
Matrix B is: [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]]
Matrix multiplication of [[1, 2], [3, 4]] and [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]] is:[[11, 14, 17, 20, 23], [23, 30, 37, 4
4, 51]]

In [158]: import sys


import secrets
import random
from collections import Counter
import math
B=[]
sam=[]
prob=[]
def pick_a_number_from_list(A):
#random_items=random.sample(A,1)[0] #to select single item from a list
random_items=random.choice(A)
return random_items

def sampling_based_on_magnitued():
for i in range(1,10):
number = pick_a_number_from_list(B)
sam.append(number)
print("random generated number is",sam)
F=Counter(sam)
print(F)
sorted(F,key=F.get,reverse=True) #sorting dict in descending order
freq=[] #it will hold value corresponding to each key
freqkey=[]
for key,value in F.items():
#if len(freq)>len(B):
freq.append(value)
freqkey.append(key)

sum=0
for l in range(len(freq)):
sum+=freq[l]

print("Number of occurance of array element:{0} is {1}".format(freqkey,freq))


print("summation of frequency of array is:",sum)

prob2=[]
prob3_sorted=[]
freq.sort(reverse=True)
freq_key_sorted=[]
for h in range(0,len(freq)):
prob=freq[h]/sum
prob2.append(prob)
#prob3_sorted[h]=sorted(prob2)
for k,v in F.items():
if v==freq[h]:
freq_key_sorted.append(k)
print("new key",freq_key_sorted)
print("value",freq)
print("Element {0} with probability:{1}".format(freq_key_sorted[h],prob2[h]))

#print("List should contain maximum 15 elements")


print("Enter the number of element that you want in the list - note, it should not be more than 15")
total_ele_list=int(input())
#B=[]
B1=0
if total_ele_list>=15:
print("List should contain maximun 15 elements")
sys.exit()
else:
for i in range(0,total_ele_list):
B1=int(input())
B.append(B1)

print(B)

sampling_based_on_magnitued()
#print("selected random number is:{0}".format(pick_a_number_from_list(B)))
#https://www.geeksforgeeks.org/random-number-generator-in-arbitrary-probability-distribution-fashion/

Enter the number of element that you want in the list - note, it should not be more than 15
2
1
[1]
2
[1, 2]
random generated number is [2]
Counter({2: 1})
Number of occurance of array element:[2] is [1]
summation of frequency of array is: 1
random generated number is [2, 1]
Counter({2: 1, 1: 1})
Number of occurance of array element:[2, 1] is [1, 1]
summation of frequency of array is: 2
random generated number is [2, 1, 1]
Counter({1: 2, 2: 1})
Number of occurance of array element:[2, 1] is [1, 2]
summation of frequency of array is: 3
random generated number is [2, 1, 1, 2]
Counter({2: 2, 1: 2})
Number of occurance of array element:[2, 1] is [2, 2]
summation of frequency of array is: 4
random generated number is [2, 1, 1, 2, 1]
Counter({1: 3, 2: 2})
Number of occurance of array element:[2, 1] is [2, 3]
summation of frequency of array is: 5
random generated number is [2, 1, 1, 2, 1, 2]
Counter({2: 3, 1: 3})
Number of occurance of array element:[2, 1] is [3, 3]
summation of frequency of array is: 6
random generated number is [2, 1, 1, 2, 1, 2, 1]
Counter({1: 4, 2: 3})
Number of occurance of array element:[2, 1] is [3, 4]
summation of frequency of array is: 7
random generated number is [2, 1, 1, 2, 1, 2, 1, 2]
Counter({2: 4, 1: 4})
Number of occurance of array element:[2, 1] is [4, 4]
summation of frequency of array is: 8
random generated number is [2, 1, 1, 2, 1, 2, 1, 2, 2]
Counter({2: 5, 1: 4})
Number of occurance of array element:[2, 1] is [5, 4]
summation of frequency of array is: 9
new key [2]
value [5, 4]
Element 2 with probability:0.5555555555555556
new key [2, 1]
value [5, 4]
Element 1 with probability:0.4444444444444444

In [7]: import re
digit=[]
def replace_digits(st):
for c in st:
if c.isdigit()==True:
digit.append(c)

for i in range(len(digit)):
digit[i]="#"

st1=''.join(digit) #converting list into string


print(st1)

print("Enter the string")


st3=input()
replace_digits(st3)

Enter the string


567
###

In [6]: #https://docs.python.org/3/howto/sorting.html
def display_dash_board(Students, Marks):
table={}
for i in Students:
table[i]=[]

for j in range(len(Marks)):
table[Students[j]]=Marks[j]

Sorted_dict=sorted(table.items(), key=lambda item: item[1])


print(Sorted_dict)

top_5_students = list(reversed(list(Sorted_dict)[-5:]))
least_5_students = Sorted_dict[0:5]
students_within_25_and_75 = Sorted_dict[int(0.25*len(table)):int(0.75*len(table))]
return top_5_students, least_5_students, students_within_25_and_75

students=[]
marks=[]
print("Enter number of students in a class should be more than 10:")
no_stud=int(input())
if no_stud<10:
print("Please enter details of more than 10 student")
sys.exit()

for i in range(0,no_stud):
name=input("Please enter all student name:")
students.append(name)
mark_de=int(input("Enter the mark of student:"))
marks.append(mark_de)

top_5_students, least_5_students, students_within_25_and_75 = display_dash_board(students, marks)


print("**student Who got top 5 ranks, in the descending order of marks**")
print(top_5_students)
print("**student Who got least 5 ranks, in the increasing order of marks**")
print(least_5_students)
print("**student Who got >25th percentile and <75th percentile**")
print(students_within_25_and_75)

Enter number of students in a class should be more than 10:


10
Please enter all student name:s1
Enter the mark of student:45
Please enter all student name:s2
Enter the mark of student:78
Please enter all student name:s3
Enter the mark of student:12
Please enter all student name:s4
Enter the mark of student:14
Please enter all student name:s5
Enter the mark of student:48
Please enter all student name:s6
Enter the mark of student:43
Please enter all student name:s7
Enter the mark of student:47
Please enter all student name:s8
Enter the mark of student:98
Please enter all student name:s9
Enter the mark of student:35
Please enter all student name:s10
Enter the mark of student:80
[('s3', 12), ('s4', 14), ('s9', 35), ('s6', 43), ('s1', 45), ('s7', 47), ('s5', 48), ('s2', 78), ('s10', 80), ('s8', 98)]
**student Who got top 5 ranks, in the descending order of marks**
[('s8', 98), ('s10', 80), ('s2', 78), ('s5', 48), ('s7', 47)]
**student Who got least 5 ranks, in the increasing order of marks**
[('s3', 12), ('s4', 14), ('s9', 35), ('s6', 43), ('s1', 45)]
**student Who got >25th percentile and <75th percentile**
[('s9', 35), ('s6', 43), ('s1', 45), ('s7', 47), ('s5', 48)]

In [2]: import ast


import math
import operator
#find 5 closest points(based on cosine distance) in S from P
def closest_points_to_p(List_tup, P):
x,y=list(zip(*List_tup))
x1=list(map(int,x))
y1=list(map(int,y))
p=list(map(int,P))
print("X value is:",x1)
print("Y value is:",y1)
a={}

for i in range(0,len(x1)):
for j in range(0,len(y1)):
xy2=math.sqrt((x1[i]*x1[i])+(y1[j]*y1[j])) #x2+y2
pq1=math.sqrt((p[0]*p[0])+(p[1]*p[1])) #p2+q2
XP=(x1[i]*p[0])+(y1[j]*p[1]) #xp+yp
cosine_dist=math.degrees(math.acos((XP)/(xy2*pq1)))
key=int(cosine_dist)
a.setdefault(key,[])
a[key].append(x1[i])
a[key].append(y1[j])
print("This is the cosine distance:{0} for correspending x:{1} y:{2} coordinate".format(key,x1[i],y1[j]))
y1.remove(y1[j])
break

#b=sorted(a.items(),reverse=False) # we will get list of tuples


#c=dict(a) #list of tuple to dic
#print("sorted value is:",a,type(a))

print("The nearest point in S from P is:",sep='\n')


i=0
for k,v in sorted(a.items(),reverse=False):
i=i+1
if i<=5:
print(tuple(v),sep='\n')

S=()
P=()
List_tup=[]
print("Enter the number of data points for S:")
n=int(input())
print("Number of data point in S is :",n)
for i in range(0,n):
S=tuple(int(k) for k in input("enter datapoint x,y for S:").split(','))
#S=ast.literal_eval(input("enter data points for S:")
List_tup.append(S)

print(List_tup)

print("Enter single data point for P")


for i in range(0,1):
P=tuple(int(i) for i in input("Datapoint for P is:").split(','))
print(P)

closest_points_to_p(List_tup, P)

Enter the number of data points for S:


9
Number of data point in S is : 9
enter datapoint x,y for S:1,2
enter datapoint x,y for S:3,4
enter datapoint x,y for S:-1,1
enter datapoint x,y for S:6,-7
enter datapoint x,y for S:-1,-1
enter datapoint x,y for S:6,0
enter datapoint x,y for S:1,-1
enter datapoint x,y for S:-5,6
enter datapoint x,y for S:9,-1
[(1, 2), (3, 4), (-1, 1), (6, -7), (-1, -1), (6, 0), (1, -1), (-5, 6), (9, -1)]
Enter single data point for P
Datapoint for P is:3,-9
(3, -9)
X value is: [1, 3, -1, 6, -1, 6, 1, -5, 9]
Y value is: [2, 4, 1, -7, -1, 0, -1, 6, -1]
This is the cosine distance:135 for correspending x:1 y:2 coordinate
This is the cosine distance:124 for correspending x:3 y:4 coordinate
This is the cosine distance:153 for correspending x:-1 y:1 coordinate
This is the cosine distance:22 for correspending x:6 y:-7 coordinate
This is the cosine distance:63 for correspending x:-1 y:-1 coordinate
This is the cosine distance:71 for correspending x:6 y:0 coordinate
This is the cosine distance:26 for correspending x:1 y:-1 coordinate
This is the cosine distance:158 for correspending x:-5 y:6 coordinate
This is the cosine distance:65 for correspending x:9 y:-1 coordinate
The nearest point in S from P is:
(6, -7)
(1, -1)
(-1, -1)
(9, -1)
(6, 0)

In [22]: #Calculate on which side of a straight line is a given point located - https://math.stackexchange.com/questions/274712/calcul
ate-on-which-side-of-a-straight-line-is-a-given-point-located
import sympy as S
import math
def i_am_the_one(red1,blue1,l1):
a1=[]
b1=[]
c1=[]
linstr=[]
linint=[]
l5=[]
for j in range(0,len(l1)):
l3=l1[j]
l4=l3.split('+')
l5.append(l4)

print("l5 is:",l5)

for i in range(0,len(l5)):
for l6 in l5[i]:
linstr.append(l6)

print("stringlist is:",linstr)

for k in linstr:
for k1 in range(len(k)):
if k1==0:
k2=int(k[k1])
linint.append(k2)

print("int of list:",linint)

#to find coefficient of A&B


coeffA=[]
coeffB=[]
f=0
f1=1
for k in range(len(linint)):
if f<len(linint) and f1<len(linint):
coeffA.append(linint[f])
f=f+3
coeffB.append(linint[f1])
f1=f1+3

print("coefficient of A is:",coeffA)
print("coefficient of B is:",coeffB)

redxcord=[]
redycord=[]

for q in range(0,r):#total number of data point for red -- putting x coordinate of red in one list and y in another list
for s in range(len(red1[q])):
if s==0:
f=red1[q][s]
redxcord.append(f)
else:
f1=red1[q][s]
redycord.append(f1)

print("x coordinate of red:",redxcord)


print("y coordinate of red:",redycord)

bluexcord=[]
blueycord=[]

for q in range(0,b):#total number of data point for blue -- putting x coordinate of blue in one list and y in another lis
t
for s in range(len(blue1[q])):
if s==0:
f=blue1[q][s]
bluexcord.append(f)
else:
f1=blue1[q][s]
blueycord.append(f1)

print("x coordinate of blue:",bluexcord)


print("y coordinate of blue:",blueycord)

#to find interception for red point


intercept=[]
for d in range(0,l):
inter1=0
for i in range(0,r):
#print("coeffA",coeffA[d])
#print("coeffB",coeffB[d])
#print("redx",redxcord[i])
#print("redy",redycord[i])
inter2=(coeffA[d]*redxcord[i])+(coeffB[d]*redycord[i])
inter1=inter1+inter2
intercept.append(inter1)

print("intercept for red:",intercept)

distr1=[]
for i in range(0,l):
distr1.append([])

print("distr1",distr1)

for i in range(0,l):
for j in range(0,r):
distr1[i].append(j)
distr1[i][j]=0

print("distr1",distr1)

dist=[]
for d in range(0,l):
dis=0
for i in range(0,r):
short_dis=(coeffA[d]*redxcord[i])+(coeffB[d]*redycord[i])+(intercept[d])/(math.sqrt((coeffA[d]*coeffA[d])+(coeffB
[d]*coeffB[d])))
dis=dis+short_dis
distr1[d][i]=dis
dist.append(dis)

print("distance of red point from line is:",distr1)

for j in range(0,l):
ctnegative=0
ctpositive=0
for k in range(len(distr1)):
if distr1[j][k]<0:
ctnegative=ctnegative+1
else:
ctpositive=ctpositive+1

if ctnegative==len(distr1):
print("all distance in -ve sign--Line {0} separates all red points".format(j))
elif ctpositive==len(distr1):
print("all distance in +ve sign--Line {0} separates all red points".format(j))
else:
print("Line {0} does not separate red points".format(j))

#to find interception for blue point


interceptb=[]
for d in range(0,l):
inter1=0
for i in range(0,b):
#print("coeffA",coeffA[d])
#print("coeffB",coeffB[d])
#print("bluex",bluexcord[i])
#print("bluey",blueycord[i])
inter2=(coeffA[d]*bluexcord[i])+(coeffB[d]*blueycord[i])
inter1=inter1+inter2
interceptb.append(inter1)

print("intercept for blue:",interceptb)

distb1=[]
for i in range(0,l):
distb1.append([])

print("distb1",distb1)

for i in range(0,l):
for j in range(0,b):
distb1[i].append(j)
distb1[i][j]=0

print("distb1",distb1)

distb=[]
for d in range(0,l):
dis=0
for i in range(0,b):
short_dis=((coeffA[d]*bluexcord[i])+(coeffB[d]*blueycord[i])+(interceptb[d]))/(math.sqrt((coeffA[d]*coeffA[d])+(c
oeffB[d]*coeffB[d])))
dis=dis+short_dis
distb1[d][i]=dis
distb.append(dis)

print("distance of blue point from line is:",distb1)

for j in range(0,l):
ctnegative=0
ctpositive=0
for k in range(len(distb1)):
if distb1[j][k]<0:
ctnegative=ctnegative+1
else:
ctpositive=ctpositive+1

if ctnegative==len(distb1):
print("all distance in -ve sign--Line {0} separates all blue points".format(j))
elif ctpositive==len(distb1):
print("all distance in +ve sign--Line {0} separates all blue points".format(j))
else:
print("Line {0} does not separate blue points".format(j))

Red=()
Blue=()
List_redtup=[]
List_bluetup=[]
line=[]
print("Enter the number of data points for Red:")
r=int(input())
print("Number of data point in Red is :",r)
for i in range(0,r):
r1=tuple(int(k) for k in input("Enter coordinate x,y for Red point:").split(','))
List_redtup.append(r1)

print("Data points of Red is:",List_redtup)


print("Enter the number of data points for Blue:")
b=int(input())
print("Number of data point in Blue is :",b)
for i in range(0,b):
b1=tuple(int(k) for k in input("Enter coordinate x,y for Blue point:").split(','))
List_bluetup.append(b1)

print("Data points of blue is:",List_bluetup)

print("******Enter number of lines that you want to check whether it separates both red and blue points******")
l=int(input())
print("Enter set of line equations and it should be in this format a1x+b1y+c1,a2x+b2y+c2..etc K lines")
for i in range(0,l):
#a,b=S.symbols("a,b")
l1=input()
line.append(l1)

print(line)

i_am_the_one(List_redtup,List_bluetup,line)

Enter the number of data points for Red:


3
Number of data point in Red is : 3
Enter coordinate x,y for Red point:1,2
Enter coordinate x,y for Red point:-1,-5
Enter coordinate x,y for Red point:2,3
Data points of Red is: [(1, 2), (-1, -5), (2, 3)]
Enter the number of data points for Blue:
3
Number of data point in Blue is : 3
Enter coordinate x,y for Blue point:-1,-2
Enter coordinate x,y for Blue point:2,5
Enter coordinate x,y for Blue point:-1,-5
Data points of blue is: [(-1, -2), (2, 5), (-1, -5)]
******Enter number of lines that you want to check whether it separates both red and blue points******
2
Enter set of line equations and it should be in this format a1x+b1y+c1,a2x+b2y+c2..etc K lines
1*a+1*b+0
3*a+7*b+0
['1*a+1*b+0', '3*a+7*b+0']
l5 is: [['1*a', '1*b', '0'], ['3*a', '7*b', '0']]
stringlist is: ['1*a', '1*b', '0', '3*a', '7*b', '0']
int of list: [1, 1, 0, 3, 7, 0]
coefficient of A is: [1, 3]
coefficient of B is: [1, 7]
x coordinate of red: [1, -1, 2]
y coordinate of red: [2, -5, 3]
x coordinate of blue: [-1, 2, -1]
y coordinate of blue: [-2, 5, -5]
intercept for red: [2, 6]
distr1 [[], []]
distr1 [[0, 0, 0], [0, 0, 0]]
distance of red point from line is: [[4.414213562373095, -0.17157287525381015, 6.242640687119285], [17.787838597158334, -
19.42432280568333, 8.363515791475002]]
Line 0 does not separate red points
Line 1 does not separate red points
intercept for blue: [-2, -14]
distb1 [[], []]
distb1 [[0, 0, 0], [0, 0, 0]]
distance of blue point from line is: [[-3.5355339059327373, 0.0, -5.65685424949238], [-4.070499418651399, -0.525225731438
8897, -7.3531602401444625]]
Line 0 does not separate blue points
all distance in -ve sign--Line 1 separates all blue points

In [1]: # Reffered this link for this program - https://stackoverflow.com/questions/57179618/filling-the-missing-values-in-the-specif


ied-format-python
li=[]
middle_store_value=[]
def replace_digit(in_str):
l=in_str.split(",")

for i in l:
if i=='_':
li.append(i)
else:
li.append(int(i))

print("Input is:",li)

count=0
middle_store=0

for k in range(len(li)):
if li[k]=='_':
count=count+1
else:
for j in range(0,k+1):
li[j]=str((float(li[k]))/(count+1))
middle_store=k
#print("middle store is:",middle_store)
middle_store_value=float(li[k])
break

#blanks in the middle


denominator=1
flag=0
k3=len(li)
for k1 in range(middle_store+1,len(li)):
if li[k1]!='_':
denominator=(k1+1-middle_store)
flag=k1
#print("flag is:",flag)
break

last=0
flag_value=float(li[flag])
for p in range(middle_store,flag+1):
li[p]=str((middle_store_value+flag_value)/(denominator))
last=p

#print("last is:",last)
#blanks at the right
last_value=float(li[last])
#print("last value is:",last_value)
for q in range(last,len(li)):
#print("q is:",q)
if li[q]!='_' or li[k3-1]=='_':
li[q]=str((last_value)/(len(li)-last))

print("output is:",li)

print("Enter string with comma separated values:")


s=input()

replace_digit(s)

Enter string with comma separated values:


_,_,30,_,_,_,50,_,_
Input is: ['_', '_', 30, '_', '_', '_', 50, '_', '_']
output is: ['10.0', '10.0', '12.0', '12.0', '12.0', '12.0', '4.0', '4.0', '4.0']

In [6]: # import random


def compute_conditional_probabilites(A):
F=[]
S=[]
cond_p=[]
for i in A:
F.append(i[0])
S.append(i[1])

F1=list(dict.fromkeys(F))
S1=list(dict.fromkeys(S))

cnt_s2=[]
for i in range(0,len(S1)):
num_s=S1[i]
cnt_s=0
cnt_s1=0
#print("nums:",num_s)
for j in range(0,len(S)):
if S[j]==num_s:
cnt_s=cnt_s+1
continue
cnt_s1=cnt_s
cnt_s2.append(cnt_s1)

cnt_f2=[]
for m in range(0,len(S1)):
num_s1=S1[m]
for n in range(0,len(F1)):
num_f1=F1[n]
cnt=0
for p in range(0,len(F)):
if S[p]==num_s1 and F[p]==num_f1:
cnt=cnt+1
continue

cnt_f2.append(cnt)

n=0
for w in range(0,len(S1)):
for s in range(0,len(F1)):
cond_p=(cnt_f2[n])/(cnt_s2[w])
print("probability of P(F={0} | S== {1}) = {2}".format(F1[s],S1[w],cond_p))
n=n+1

a=[]
print("Enter number of rows:")
row=int(input())
no_of_column=2
for i in range(0,row):
a.append([]) #output - [[], [], []]

for i in range(0,row):
for j in range(0,no_of_column):
a[i].append(j)
a[i][j]=0 #output - [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

print("*********************************************")
print("It is a NX2 matrix")
print("Condition is:1st column-F should contain 5 unique values and 2nd column-S should contain 3 unique values")
print("So be cautious while entering element in the array")
print("*********************************************",sep='\n')

for i in range(0,row):
for j in range(0,no_of_column):
print('entey in row:',i+1,'entry in column:',j+1)
a[i][j]=int(input())

print(a)

compute_conditional_probabilites(a)

Enter number of rows:


5
*********************************************
It is a NX2 matrix
Condition is:1st column-F should contain 5 unique values and 2nd column-S should contain 3 unique values
So be cautious while entering element in the array
*********************************************
entey in row: 1 entry in column: 1
1
entey in row: 1 entry in column: 2
6
entey in row: 2 entry in column: 1
2
entey in row: 2 entry in column: 2
7
entey in row: 3 entry in column: 1
3
entey in row: 3 entry in column: 2
8
entey in row: 4 entry in column: 1
4
entey in row: 4 entry in column: 2
6
entey in row: 5 entry in column: 1
5
entey in row: 5 entry in column: 2
7
[[1, 6], [2, 7], [3, 8], [4, 6], [5, 7]]
probability of P(F=1 | S== 6) = 0.5
probability of P(F=2 | S== 6) = 0.0
probability of P(F=3 | S== 6) = 0.0
probability of P(F=4 | S== 6) = 0.5
probability of P(F=5 | S== 6) = 0.0
probability of P(F=1 | S== 7) = 0.0
probability of P(F=2 | S== 7) = 0.5
probability of P(F=3 | S== 7) = 0.0
probability of P(F=4 | S== 7) = 0.0
probability of P(F=5 | S== 7) = 0.5
probability of P(F=1 | S== 8) = 0.0
probability of P(F=2 | S== 8) = 0.0
probability of P(F=3 | S== 8) = 1.0
probability of P(F=4 | S== 8) = 0.0
probability of P(F=5 | S== 8) = 0.0

In [14]: def string_features(S1, S2):


count=0
S1_l=[]
S1_l1=[]
S2_l=[]
S2_l2=[]
inS1_notS2=[]
inS2_notS1=[]
S1_l=S1.split()
S2_l=S2.split()
for word in S1_l:
S1_l1.append(word)

print("List of S1 is:",S1_l1)

for word in S2_l:


S2_l2.append(word)

print("List of S2 is:",S2_l2)

for common_word in S1_l1:


if common_word in S2_l2:
count=count+1

print("Number of common word between 2 sentence (S1,S2) is:",count)

for common_word in S1_l1:


if common_word not in S2_l2:
inS1_notS2.append(common_word)

print("Words in S1 but not in S2:",inS1_notS2)

for common_word in S2_l2:


if common_word not in S1_l1:
inS2_notS1.append(common_word)

print("Words in S2 but not in S1:",inS2_notS1)

print("Enter 1st sentence S1:")


S1=input()
print("Enter 2nd sentence S2:")
S2=input()

string_features(S1, S2)

Enter 1st sentence S1:


the first column F will contain only 5 unique values
Enter 2nd sentence S2:
the second column S will contain only 5 unique values
List of S1 is: ['the', 'first', 'column', 'F', 'will', 'contain', 'only', '5', 'unique', 'values']
List of S2 is: ['the', 'second', 'column', 'S', 'will', 'contain', 'only', '5', 'unique', 'values']
Number of common word between 2 sentence (S1,S2) is: 8
Words in S1 but not in S2: ['first', 'F']
Words in S2 but not in S1: ['second', 'S']

In [25]: import math


def compute_log_loss(a):
y=[]
yscore=[]
for i in range(len(a)):
for j in range(len(a[0])):
if j==0:
y.append(a[i][j])
else:
yscore.append(a[i][j])

print("y is:",y)
print("yscore is:",yscore)

equ=0
for k in range(0,row):
score=(1-(yscore[k]))
m=math.log10(yscore[k])
n=math.log10(score if score>0 else 1)
equ+=((y[k]*m)+((1-y[k])*n))

output=(-1/row)*equ
print("output is:",output)

a=[]
print("Enter number of rows:")
row=int(input())
no_of_column=2
for i in range(0,row):
a.append([]) #output - [[], [], []]

for i in range(0,row):
for j in range(0,no_of_column):
a[i].append(j)
a[i][j]=0 #output - [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

print("*********************************************")
print("It is a NX2 matrix")
print("Condition is:1st column-Y should contain i integer values and 2nd column-YScore should contain float values")
print("So be cautious while entering element in the array")
print("*********************************************",sep='\n')

for i in range(0,row):
for j in range(0,no_of_column):
print('entey in row:',i+1,'entry in column:',j+1)
if j==0:
a[i][j]=int(input())
else:
a[i][j]=float(input())

print(a)

compute_log_loss(a)

Enter number of rows:


8
*********************************************
It is a NX2 matrix
Condition is:1st column-Y should contain i integer values and 2nd column-YScore should contain float values
So be cautious while entering element in the array
*********************************************
entey in row: 1 entry in column: 1
1
entey in row: 1 entry in column: 2
0.4
entey in row: 2 entry in column: 1
0
entey in row: 2 entry in column: 2
0.5
entey in row: 3 entry in column: 1
0
entey in row: 3 entry in column: 2
0.9
entey in row: 4 entry in column: 1
0
entey in row: 4 entry in column: 2
0.3
entey in row: 5 entry in column: 1
0
entey in row: 5 entry in column: 2
0.6
entey in row: 6 entry in column: 1
1
entey in row: 6 entry in column: 2
0.1
entey in row: 7 entry in column: 1
1
entey in row: 7 entry in column: 2
0.9
entey in row: 8 entry in column: 1
1
entey in row: 8 entry in column: 2
0.8
[[1, 0.4], [0, 0.5], [0, 0.9], [0, 0.3], [0, 0.6], [1, 0.1], [1, 0.9], [1, 0.8]]
y is: [1, 0, 0, 0, 0, 1, 1, 1]
yscore is: [0.4, 0.5, 0.9, 0.3, 0.6, 0.1, 0.9, 0.8]
output is: 0.42430993457031635

In [ ]:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy