Exemple #1
0
def _initializeGoodModels(data, numModels, states, obs):
    """Initialization technique for selecting a good pool of models by which 
    to start the expectation maximization algorithm for unsupervised learning
    """

    sigma = IntegerRange(0, obs)
    models = []
    distances = numpy.zeros((len(data)), float)

    # Select initial random sequence
    seq = data[int(random.random() * len(data))]
    models.append(hmmextra.obsToModel(seq, states, obs))

    for i in range(numModels - 1):
        for j in range(len(data)):
            tmp = hmmextra.obsToModel(data[j], states, obs)
            tmpDistances = hmmextra.hmmDistAll(data[j], models, sigma)
            tmpDistances.sort()
            distances[j] = tmpDistances[0]

        distances /= sum(distances)

        # Select a model with probability dist/sum(all distance)
        val = random.random()
        distSum = 0

        for j in range(len(distances)):
            distSum += distances[j]

            if distSum >= val:
                # Make a new model using this data element
                models.append(hmmextra.obsToModel(data[j], states, obs))
                break
    return models
Exemple #2
0
def _initializeGoodModels(data, numModels, states, obs):
    """Initialization technique for selecting a good pool of models by which 
    to start the expectation maximization algorithm for unsupervised learning
    """

    sigma = IntegerRange(0, obs)
    models = []
    distances = numpy.zeros((len(data)), float)

    #Select initial random sequence
    seq = data[int(random.random() * len(data))]
    models.append(hmmextra.obsToModel(seq, states, obs))

    for i in range(numModels - 1):
        for j in range(len(data)):
            tmp = hmmextra.obsToModel(data[j], states, obs)
            tmpDistances = hmmextra.hmmDistAll(data[j], models, sigma)
            tmpDistances.sort()
            distances[j] = tmpDistances[0]

        distances /= sum(distances)

        #Select a model with probability dist/sum(all distance)
        val = random.random()
        distSum = 0

        for j in range(len(distances)):
            distSum += distances[j]

            if distSum >= val:
                #Make a new model using this data element
                models.append(hmmextra.obsToModel(data[j], states, obs))
                break
    return models
Exemple #3
0
def drawHMMCluster(data, models, numSensors, \
                    writeLocation = "../output/out.png", \
                    spacing = 10, numberBase = 2, \
                    scaling = 5, \
                    cColor = (0, 255, 0), cCluster = (0, 0, 0)):
    """Draw all data associated with a given hidden markov model.  
    Space the data by the value spacing.
    """
    lenInstances = 0

    #Get length of each image.
    for d in data:
        lenInstances += len(d)

    #Make the image
    iLen = lenInstances * scaling + spacing * len(data)
    if iLen == 0:
        iLen = 1
    im = Image.new("RGB", ((numSensors + 1) * scaling + 50, iLen))
    d = ImageDraw.Draw(im)

    currentY = 0

    #Draw the instances
    for i in range(len(data)):
        #Invert the data to an array.
        tmpData = bbdata.uncompressData(data[i], numSensors, numberBase)

        #Caluclate values
        sigma = IntegerRange(0, numberBase**numSensors)
        values = hmmextra.hmmDistAll(data[i], models, sigma)
        values.sort()
        _drawArrayText(d,
                       tmpData,
                       0,
                       currentY,
                       values,
                       scale=scaling,
                       cCluster=cCluster)
        currentY += tmpData.shape[0] * scaling + spacing

    im.save(writeLocation, "PNG")
Exemple #4
0
def drawHMMCluster(data, models, numSensors, \
                    writeLocation = "../output/out.png", \
                    spacing = 10, numberBase = 2, \
                    scaling = 5, \
                    cColor = (0, 255, 0), cCluster = (0, 0, 0)):
    """Draw all data associated with a given hidden markov model.  
    Space the data by the value spacing.
    """
    lenInstances = 0

    #Get length of each image.
    for d in data:
        lenInstances += len(d)

    #Make the image
    iLen = lenInstances * scaling + spacing * len(data)
    if iLen == 0:
        iLen = 1
    im = Image.new("RGB", ((numSensors + 1) * scaling + 50, iLen))
    d = ImageDraw.Draw(im)

    currentY = 0

    #Draw the instances
    for i in range(len(data)):
        #Invert the data to an array.
        tmpData = bbdata.uncompressData(data[i], numSensors, numberBase)
        
        #Caluclate values
        sigma = IntegerRange(0, numberBase**numSensors)
        values = hmmextra.hmmDistAll(data[i], models, sigma)
        values.sort()
        _drawArrayText(d, tmpData, 0, currentY, values, scale = scaling, cCluster=cCluster)
        currentY += tmpData.shape[0] * scaling + spacing

    im.save(writeLocation, "PNG")