Esempio n. 1
0
def randomHypothesis(n, database):
    """
        Returns a set of weights in the form of
        a vector. These are to be used in the ANN.
        The idea is that this should return a random
        function. But a random ANN is the next best
        thing.

        It uses the Pearson r-correlation coefficient
        to find out which of the the attributes that
        might be connected.

        n is the number of inputs of the function.
    """
    #Generate the hidden layers!
    #The number of hidden layers is geometrically random
    numHiddenLayers = georand(0.8)
    hiddenLayers = [0]*numHiddenLayers
    #The number of nodes in each layer is geometrically random as well
    for i in range(numHiddenLayers):
        hiddenLayers[i] = georand(0.95)

    #How many weights are required in the ANN?
    numWeights = howManyWeightsInTheANN(hiddenLayers, numHiddenLayers, n)


    out = [0.0]*(numWeights+numHiddenLayers+1)
    out[0] = numHiddenLayers
    index = 1
    for element in hiddenLayers:
        out[index] = element
        index += 1

    #Decide which attributes that are important
    attributesInHypothesis = chooseAttributes(database, n)

    #Set the first row of the network
    numWeightsFirstRow = n*hiddenLayers[0]
    for i in range(hiddenLayers[0]):
        for j in range(n):
            if(attributesInHypothesis[j] == True):
                out[index] = random.gauss(0,10)
            index += 1

    #And then all of the other rows
    for i in range(numWeights-numWeightsFirstRow):
        if(random.random()<0.2):
            out[index] = 10.0*random.gauss(0.0,1.0)
        index += 1
    return out
Esempio n. 2
0
 def perturb(_scores):
     _valve = MyFuncs.SafetyValve(global_reps=10)
     while _scores.score.std() == 0:
         _valve.step("Failed to perturb:\n%s" % _scores)
         for _indx, _score in _scores.score.iteritems():
             _scores.set_value(_indx, "score", random.gauss(_score, (_score * 0.0000001)))
     return _scores
Esempio n. 3
0
def addRandomLittleJump(w):
    """
        Adds a small and random change to the
        weight vector.
    """
    out = list(w)

    counter = 1 + w[0]

    for index in range(len(w)-counter):
        #Try to keep the hypothesis sparse.
        if(w[index+counter] != 0 or random.random() < 0.05):
            out[index+counter] += random.gauss(0.0, 0.005)
    return out
def generatePolygon(ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts):
    '''Start with the centre of the polygon at ctrX, ctrY,
    then creates the polygon by sampling points on a circle around the centre.
    Randon noise is added by varying the angular spacing between sequential points,
    and by varying the radial distance of each point from the centre.

    https://github.com/the-mikedavis/randompolygons

    Params:
    ctrX, ctrY - coordinates of the "centre" of the polygon
    aveRadius - in px, the average radius of this polygon, this roughly controls how large the polygon is, really only useful for order of magnitude.
    irregularity - [0,1] indicating how much variance there is in the angular spacing of vertices. [0,1] will map to [0, 2pi/numberOfVerts]
    spikeyness - [0,1] indicating how much variance there is in each vertex from the circle of radius aveRadius. [0,1] will map to [0, aveRadius]
    numVerts - self-explanatory

    Returns a list of vertices, in CCW order.
    '''
    import math, random

    irregularity = clip(irregularity, 0, 1) * 2 * math.pi / numVerts
    spikeyness = clip(spikeyness, 0, 1) * aveRadius

    # generate n angle steps
    angleSteps = []
    lower = (2 * math.pi / numVerts) - irregularity
    upper = (2 * math.pi / numVerts) + irregularity
    sum = 0
    for i in range(numVerts):
        tmp = random.uniform(lower, upper)
        angleSteps.append(tmp)
        sum = sum + tmp

    # normalize the steps so that point 0 and point n+1 are the same
    k = sum / (2 * math.pi)
    for i in range(numVerts):
        angleSteps[i] = angleSteps[i] / k

    # now generate the points
    points = []
    angle = random.uniform(0, 2 * math.pi)
    for i in range(numVerts):
        r_i = clip(random.gauss(aveRadius, spikeyness), 0, 2 * aveRadius)
        x = ctrX + r_i * math.cos(angle)
        y = ctrY + r_i * math.sin(angle)
        points.append((int(x), int(y)))

        angle = angle + angleSteps[i]

    return points
Esempio n. 5
0
def GenBoundedRandomNormal(meanVal,stdDev,lowerBound,upperBound):
    aRand = gauss(meanVal,stdDev) # could also use: normalvariate()but gauss () is slightly faster.
    while (aRand < lowerBound or aRand > upperBound):
        aRand = random.gauss(meanVal,stdDev)
    return aRand