am_dot_com

IA - 2022-11-09

Nov 9th, 2022 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. # 6.py
  2. import numpy
  3. import numpy as np
  4. import time
  5.  
  6. import math
  7. import random
  8.  
  9. def randomCol(
  10. pHowManyEls:int=10
  11. ):
  12. col = list()
  13. walk = range(pHowManyEls)
  14. for step in walk:
  15. f = random.random() # [0, 1[
  16. f *= random.randint(10, 1000) # [10, 1000]
  17. col.append(f)
  18. # for
  19. return col
  20. # def randomCol
  21.  
  22. """
  23. chiSquare rewritten to take advantage
  24. of broadcasting ops with numpy arrays
  25. """
  26. def chiSquare_v2(pO, pE):
  27. pO = numpy.array(pO)
  28. pE = numpy.array(pE)
  29.  
  30. if(len(pO)==len(pE)):
  31. v = (pO-pE)**2/pE
  32. return v.sum()
  33. else:
  34. return False
  35. # if-else
  36. # def chiSquare_v2
  37.  
  38. def chiSquare(
  39. pColObservedValues:list,
  40. pColExpectedValues:list
  41. )->float | bool:
  42. iHowManyObserved = len(pColObservedValues)
  43. iHowManyExpected = len(pColExpectedValues)
  44. bCanCalculate = iHowManyObserved == iHowManyExpected
  45. if (bCanCalculate):
  46. sum = 0
  47. for walk in range(iHowManyExpected):
  48. o = pColObservedValues[walk]
  49. e = pColExpectedValues[walk]
  50. dist = o-e # <0 if e>o
  51. #dist = dist**2
  52. dist = math.pow(dist, 2)
  53. parcel = dist/e
  54. sum += parcel
  55. # for
  56. return sum
  57. else:
  58. # collections do NOT have the same size, can NOT
  59. # calculate the chi-square statistic
  60. return False
  61. # if-else
  62. # def chiSquare
  63.  
  64. """
  65. at some stage during class time,
  66. we changed the "feedback" function to be able
  67. to work with any implementation of the
  68. chiSquare function
  69. To achieve so, it began having a 3rd param
  70. to represent whatever function we wanted
  71. """
  72. def feedback(pC1:list, pC2:list, pDescription:str, pFChiSquare):
  73. strMsg = f"{pDescription} between\n"
  74. strMsg += f"observed:{pC1[:2]}...\nexpected:{pC2[:2]}...\n"
  75. #strMsg +=f"observed:{pC1}\nexpected:{pC2}\n"
  76.  
  77. ti = time.time()
  78. chi_square = pFChiSquare(pC1, pC2)
  79. tf = time.time()
  80. timeItTook = tf - ti
  81.  
  82. strMsg +=f"is {chi_square}\n"
  83. strMsg += f"Computation time: {timeItTook}\n"
  84. print(strMsg)
  85. # def feedback
  86.  
  87. col1 = randomCol(55555) # 5 elements
  88. feedback(col1, col1, "via chiSquare", chiSquare)
  89. feedback(col1, col1, "via chiSquare_v2", chiSquare_v2)
  90.  
  91. col2 = randomCol(55555)
  92. feedback(col1, col2, "via chiSquare", chiSquare)
  93. feedback(col1, col2, "via chiSquare_v2", chiSquare_v2)
  94.  
  95. #*******************************+ Jaccard - index
  96.  
  97. def jaccardIndex(pA:list, pB:list):
  98. sA:set = set(pA)
  99. sB:set = set(pB)
  100. intersection = sA.intersection(sB)
  101. reunion = sA.union(sB)
  102. cardinalityOfI = len(intersection)
  103. cardinalityOfR = len(reunion)
  104. j = cardinalityOfI/cardinalityOfR
  105. return j
  106. # def jaccardIndex
  107.  
  108. feedback(
  109. [10, 20, 30],
  110. [10, 40, 30],
  111. "J-index",
  112. jaccardIndex
  113. )
  114.  
  115. **********
  116.  
  117. CC: Coordinating conjunction
  118. CD: Cardinal number
  119. DT: Determiner
  120. EX: Existential there
  121. FW: Foreign word
  122. IN: Preposition or subordinating conjunction
  123. JJ: Adjective
  124. VP: Verb Phrase
  125. JJR: Adjective, comparative
  126. JJS: Adjective, superlative
  127. LS: List item marker
  128. MD: Modal
  129. NN: Noun, singular or mass
  130. NNS: Noun, plural
  131. PP: Preposition Phrase
  132. NNP: Proper noun, singular Phrase
  133. NNPS: Proper noun, plural
  134. PDT: Pre determiner
  135. POS: Possessive ending
  136. PRP: Personal pronoun Phrase
  137. PRP: Possessive pronoun Phrase
  138. RB: Adverb
  139. RBR: Adverb, comparative
  140. RBS: Adverb, superlative
  141. RP: Particle
  142. S: Simple declarative clause
  143. SBAR: Clause introduced by a (possibly empty) subordinating conjunction
  144. SBARQ: Direct question introduced by a wh-word or a wh-phrase.
  145. SINV: Inverted declarative sentence, i.e. one in which the subject follows the tensed verb or modal.
  146. SQ: Inverted yes/no question, or main clause of a wh-question, following the wh-phrase in SBARQ.
  147. SYM: Symbol
  148. VBD: Verb, past tense
  149. VBG: Verb, gerund or present participle
  150. VBN: Verb, past participle
  151. VBP: Verb, non-3rd person singular present
  152. VBZ: Verb, 3rd person singular present
  153. WDT: Wh-determiner
  154. WP: Wh-pronoun
  155. WP: Possessive wh-pronoun
  156. WRB: Wh-adverb
Advertisement
Add Comment
Please, Sign In to add comment