Building a Transition Matrix using words in Python/Numpy

后端 未结 6 1753
抹茶落季
抹茶落季 2020-12-09 22:32

Im trying to build a 3x3 transition matrix with this data

days=[\'rain\', \'rain\', \'rain\', \'clouds\', \'rain\', \'sun\', \'clouds\', \'clouds\', 
  \'rai         


        
6条回答
  •  一生所求
    2020-12-09 23:19

    It seems you want to create a matrix of the probability of rain coming after sun or clouds coming after sun (or etc). You can spit out the probability matrix (not a math term) like so:

    def probabilityMatrix():
        tomorrowsProbability=np.zeros((3,3))
        occurancesOfEach = Counter(data)
        myMatrix = Counter(zip(data, data[1:]))
        probabilityMatrix = {key : myMatrix[key] / occurancesOfEach[key[0]] for key in myMatrix}
        return probabilityMatrix
    
    print(probabilityMatrix())
    

    However, you probably want to spit out the probability for every type of weather based on today's weather:

    def getTomorrowsProbability(weather):
        probMatrix = probabilityMatrix()
        return {key[1] : probMatrix[key]  for key in probMatrix if key[0] == weather}
    
    print(getTomorrowsProbability('sun'))
    

提交回复
热议问题