Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 6.py
- import numpy
- import numpy as np
- import time
- import math
- import random
- def randomCol(
- pHowManyEls:int=10
- ):
- col = list()
- walk = range(pHowManyEls)
- for step in walk:
- f = random.random() # [0, 1[
- f *= random.randint(10, 1000) # [10, 1000]
- col.append(f)
- # for
- return col
- # def randomCol
- """
- chiSquare rewritten to take advantage
- of broadcasting ops with numpy arrays
- """
- def chiSquare_v2(pO, pE):
- pO = numpy.array(pO)
- pE = numpy.array(pE)
- if(len(pO)==len(pE)):
- v = (pO-pE)**2/pE
- return v.sum()
- else:
- return False
- # if-else
- # def chiSquare_v2
- def chiSquare(
- pColObservedValues:list,
- pColExpectedValues:list
- )->float | bool:
- iHowManyObserved = len(pColObservedValues)
- iHowManyExpected = len(pColExpectedValues)
- bCanCalculate = iHowManyObserved == iHowManyExpected
- if (bCanCalculate):
- sum = 0
- for walk in range(iHowManyExpected):
- o = pColObservedValues[walk]
- e = pColExpectedValues[walk]
- dist = o-e # <0 if e>o
- #dist = dist**2
- dist = math.pow(dist, 2)
- parcel = dist/e
- sum += parcel
- # for
- return sum
- else:
- # collections do NOT have the same size, can NOT
- # calculate the chi-square statistic
- return False
- # if-else
- # def chiSquare
- """
- at some stage during class time,
- we changed the "feedback" function to be able
- to work with any implementation of the
- chiSquare function
- To achieve so, it began having a 3rd param
- to represent whatever function we wanted
- """
- def feedback(pC1:list, pC2:list, pDescription:str, pFChiSquare):
- strMsg = f"{pDescription} between\n"
- strMsg += f"observed:{pC1[:2]}...\nexpected:{pC2[:2]}...\n"
- #strMsg +=f"observed:{pC1}\nexpected:{pC2}\n"
- ti = time.time()
- chi_square = pFChiSquare(pC1, pC2)
- tf = time.time()
- timeItTook = tf - ti
- strMsg +=f"is {chi_square}\n"
- strMsg += f"Computation time: {timeItTook}\n"
- print(strMsg)
- # def feedback
- col1 = randomCol(55555) # 5 elements
- feedback(col1, col1, "via chiSquare", chiSquare)
- feedback(col1, col1, "via chiSquare_v2", chiSquare_v2)
- col2 = randomCol(55555)
- feedback(col1, col2, "via chiSquare", chiSquare)
- feedback(col1, col2, "via chiSquare_v2", chiSquare_v2)
- #*******************************+ Jaccard - index
- def jaccardIndex(pA:list, pB:list):
- sA:set = set(pA)
- sB:set = set(pB)
- intersection = sA.intersection(sB)
- reunion = sA.union(sB)
- cardinalityOfI = len(intersection)
- cardinalityOfR = len(reunion)
- j = cardinalityOfI/cardinalityOfR
- return j
- # def jaccardIndex
- feedback(
- [10, 20, 30],
- [10, 40, 30],
- "J-index",
- jaccardIndex
- )
- **********
- CC: Coordinating conjunction
- CD: Cardinal number
- DT: Determiner
- EX: Existential there
- FW: Foreign word
- IN: Preposition or subordinating conjunction
- JJ: Adjective
- VP: Verb Phrase
- JJR: Adjective, comparative
- JJS: Adjective, superlative
- LS: List item marker
- MD: Modal
- NN: Noun, singular or mass
- NNS: Noun, plural
- PP: Preposition Phrase
- NNP: Proper noun, singular Phrase
- NNPS: Proper noun, plural
- PDT: Pre determiner
- POS: Possessive ending
- PRP: Personal pronoun Phrase
- PRP: Possessive pronoun Phrase
- RB: Adverb
- RBR: Adverb, comparative
- RBS: Adverb, superlative
- RP: Particle
- S: Simple declarative clause
- SBAR: Clause introduced by a (possibly empty) subordinating conjunction
- SBARQ: Direct question introduced by a wh-word or a wh-phrase.
- SINV: Inverted declarative sentence, i.e. one in which the subject follows the tensed verb or modal.
- SQ: Inverted yes/no question, or main clause of a wh-question, following the wh-phrase in SBARQ.
- SYM: Symbol
- VBD: Verb, past tense
- VBG: Verb, gerund or present participle
- VBN: Verb, past participle
- VBP: Verb, non-3rd person singular present
- VBZ: Verb, 3rd person singular present
- WDT: Wh-determiner
- WP: Wh-pronoun
- WP: Possessive wh-pronoun
- WRB: Wh-adverb
Advertisement
Add Comment
Please, Sign In to add comment