Ejemplo n.º 1
0
def MSE(sampleSize):
    totalSE = 0.0
    for i in range(sampleSize):
        in1 = random() * 6.0
        in2 = random() * 6.0
        error = targetFunction(in1, in2) - f(in1, in2)
        totalSE = totalSE + error * error
Ejemplo n.º 2
0
 def initialize(self):
     self.topic_prob = np.zeros(
         [self.no_of_docs, self.vocabulary_size, self.no_of_topics],
         dtype=np.float)
     self.document_topic_prob = random((self.no_of_docs, self.no_of_topics))
     # self.document_topic_prob = normalize(self.document_topic_prob)
     self.topic_word_prob = random(
         (self.no_of_topics, self.vocabulary_size))
Ejemplo n.º 3
0
def MSE(sampleSize):
    totalSE = 0.0
    for i in range(sampleSize):
        x = random() * 6.0
        y = random() * 6.0
        error = targetFunction(x, y) - f(x, y)
        totalSE = totalSE + error * error
    print 'The estimated MSE: ', (totalSE / sampleSize)
Ejemplo n.º 4
0
def MSE(sampleSize):
    totalSE = 0.0
    for i in range(sampleSize):
        in1 = random() * 6.0
        in2 = random() * 6.0
        error = targetFunction(in1,in2) - f(in1,in2)
        totalSE = totalSE + error * error
    print('The estimated MSE: ', (totalSE / sampleSize))
Ejemplo n.º 5
0
def MSE(sampleSize):
    totalSE = 0.0
    for i in range(sampleSize):
        x = random() * 6.0
        y = random() * 6.0
        error = targetFunction(x,y) - f(x,y)
        totalSE = totalSE + error * error
    print 'The estimated MSE: ', (totalSE / sampleSize)
Ejemplo n.º 6
0
def MSE(sampleSize):
    totalSE = 0.0
    for i in range(sampleSize):
        in1 = random() * 6.0
        in2 = random() * 6.0
        error = targetFunction(in1, in2) - f(in1, in2)
        totalSE = totalSE + error * error
    print('The estimated MSE: ', (totalSE / sampleSize))
Ejemplo n.º 7
0
    def __init__(self, corpus, stopWords):
        self._corpus = corpus
        self._stopWords = stopWords
        self._K = 10  # number of topic
        self._maxIteration = 20
        self._threshold = 10.0
        self._topicWordsNum = 10
        self._doc_topic = '/Users/ruslantagirov/Desktop/Univer/3course/data-repo/LabWorks/labs/LW3/results/' \
                          'doc-topic.txt'
        self._topic_word = '/Users/ruslantagirov/Desktop/Univer/3course/data-repo/LabWorks/labs/LW3/results' \
                           '/topic-word.txt'
        self._dic = '/Users/ruslantagirov/Desktop/Univer/3course/data-repo/LabWorks/labs/LW3/results/dic.dic'
        self._topics = '/Users/ruslantagirov/Desktop/Univer/3course/data-repo/LabWorks/labs/LW3/results/topics.txt'

        self._N = len(corpus.getDocuments())
        self._wordCounts = []
        self._word2id = {}
        self._id2word = {}
        self._currentId = 0

        for document in corpus.getDocuments():
            normalized_list = Preprocessing.toNormalForm(Preprocessing
                                                         .documentToList(document))
            self._segList = Preprocessing.removeStopWords(self._stopWords.getStopWords(),
                                                          normalized_list)
            self._wordCount = {}
            for word in self._segList:
                word = word.lower().strip()
                if len(word) > 1 and not re.search('[0-9]', word) and word not in self._stopWords.getStopWords():
                    if word not in self._word2id.keys():
                        self._word2id[word] = self._currentId
                        self._id2word[self._currentId] = word
                        self._currentId += 1
                    if word in self._wordCount:
                        self._wordCount[word] += 1
                    else:
                        self._wordCount[word] = 1
            self._wordCounts.append(self._wordCount)

        # length of dic
        self._M = len(self._word2id)

        # generate the document-word matrix
        self._X = zeros([self._N, self._M], int8)
        for word in self._word2id.keys():
            j = self._word2id[word]
            for i in range(0, self._N):
                if word in self._wordCounts[i]:
                    self._X[i, j] = self._wordCounts[i][word]

        # lambda: p(zj|di)
        self._lambda = random([self._N, self._K])

        # theta: p(wj|zi)
        self._theta = random([self._K, self._M])

        # p: p(zk|di,wj)
        self._p = zeros([self._N, self._M, self._K])
Ejemplo n.º 8
0
def update():
    global g
    a = rd.choice(list(g.nodes()))
    if g.node[a]['state'] == 0:
        b = rd.choice(list(g.neighbors(a)))
        if g.node[b]['state'] == 1:
            g.node[a]['state'] = 1 if pylab.random() < p_i else 0
    else:
        g.node[a]['state'] = 0 if pylab.random() < p_r else 1
Ejemplo n.º 9
0
def random_rot_angles(x=True, y=True, z=True):
    rotation = {}
    if x:
        rotation['x'] = pl.random() * 2 * pl.pi
    if y:
        rotation['y'] = pl.random() * 2 * pl.pi
    if z:
        rotation['z'] = pl.random() * 2 * pl.pi

    return rotation
Ejemplo n.º 10
0
def test_com_jacobian(dq_norm=1e-3, q=None):
    if q is None:
        q = hrp.dof_llim + random(56) * (hrp.dof_ulim - hrp.dof_llim)
    dq = random(56) * dq_norm
    com = hrp.compute_com(q)
    J_com = hrp.compute_com_jacobian(q)
    expected = com + dot(J_com, dq)
    actual = hrp.compute_com(q + dq)
    assert norm(actual - expected) < 2 * dq_norm ** 2
    return J_com
Ejemplo n.º 11
0
def init():
    global agents
    agents = []
    for i in range(n):
        ag = agent()
        ag.x = pylab.random()
        ag.y = pylab.random()
        ag.color = 1 if pylab.random() < 0.5 else 0
        agents.append(ag)
    print(len(agents))
Ejemplo n.º 12
0
    def __init__(self, Fs=16000, TinSec=10):
        '''
        Fs: 取樣頻率,預設值為 16000,
        TinSec: 保存語音長度,預設值為 10 sec
        '''
        print('RyAudio use %s' % pa.get_portaudio_version_text())

        self.Fs = Fs
        self.spBufferSize = 1024
        self.fftWindowSize = self.spBufferSize

        self.aP = pa.PyAudio()
        self.iS = pa.Stream(PA_manager=self.aP,
                            input=True,
                            rate=self.Fs,
                            channels=1,
                            format=pa.paInt16)
        self.oS = pa.Stream(PA_manager=self.aP,
                            output=True,
                            rate=self.Fs,
                            channels=1,
                            format=pa.paInt16)
        self.iTh = None
        self.oTh = None

        #self.sound=     None
        #self.soundTime= 0
        self.gettingSound = True
        self.playingSound = True

        self.t = 0
        self.b = None  # byte string
        self.x = None  # ndarray
        self.fft = None
        self.f0 = 0  #None
        self.en = 0  #None
        self.fm = 0  #None # frequency mean
        self.fv = 0  #None # frequency var
        self.fs = 0  #None # frequency std
        self.enP = 0  #None # AllPass
        self.enPL = 0  #None # LowPass
        self.enPH = 0  #None # HighPass

        self.entropy = 0  #None

        self.frameI = 0

        #self.frameN= self.spBufferSize/4  #1024/4 = 256
        self.TinSec = TinSec  #10 # sec
        self.frameN = self.Fs * self.TinSec / self.spBufferSize  #self.spBufferSize/4  #1024/4 = 256
        self.frameN = int(self.frameN)

        self.specgram = pl.random([self.frameN, self.spBufferSize / 2])

        self.xBuf = pl.random([self.frameN, self.spBufferSize])
Ejemplo n.º 13
0
def main():
    parser = ArgumentParser()
    parser.add_argument('-e', help = " if specified, use the dictionary you made to classify the document", action="store_true", default = False)
    parser.add_argument('-b', help = " best-result", action="store_true", default = False)
    parser.add_argument('-d', help = " The doc.csv", type = str)
    parser.add_argument('-g', help = " The group.csv", type = str)
    parser.add_argument('-o', help = " output path : *.csv", type = str)
    args = parser.parse_args()

    global N, M, X, K, lamda, theta, maxIteration, p, WordsInGroup, Vocab_List, Save_dir, G

    # Read_Doc(args.d)
    N, M, X , Vocab_List= Read_Doc(args.d)
    print('M = ',M)

    K = 50    # number of topic
    maxIteration = 100  # iter
    threshold = 10

    # lamda[i, j] : p(zj|di)
    lamda = random([N, K])

    # theta[i, j] : p(wj|zi)
    theta = random([K, M])

    # p[i, j, k] : p(zk|di,wj)
    p = zeros([N, M, K])
    lamda, theta = Initial(lamda, theta)
    # EM algo
    oldLoglikelihood = 1
    newLoglikelihood = 1
    print('===== EM Algorithm Start =====\n')
    for i in range(0, maxIteration):
        p = E_Step(theta, lamda, p)
        theta, lamda = M_Step(theta, lamda, p, X)
        newLoglikelihood = LogLikelihood(theta, lamda, X)
        print("[", time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), "] ", i+1, " iteration  ", str(newLoglikelihood))
        # if(oldLoglikelihood != 1 and newLoglikelihood - oldLoglikelihood < threshold):
        #     break
        oldLoglikelihood = newLoglikelihood
    
    print('\n===== EM Algorithm end =====')

    WordsInGroup ,G = Group(args.g, args.e)
    Save_dir = 'K_'+str(K)+'Iter_'+str(maxIteration)+'Thre_'+str(threshold)

    # OutPut() # save two matrix


    # word_count_classifer(args.o)
    plsa_classifer(args.o)

    print("\n[", time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), "] OutPut Done!!! " )
Ejemplo n.º 14
0
def update():
    global g
    i = rd.choice(list(g.nodes()))
    if g.node[i]['state'] == 0:
        for j in list(g.neighbors(i)):
            if g.node[j]['state'] == 1:
                if pylab.random() < p_s:
                    g.remove_edge(i, j)
                elif pylab.random() < p_i:
                    g.node[i]['state'] = 1
    else:
        if pylab.random() < p_r:
            g.node[i]['state'] = 0
Ejemplo n.º 15
0
def check_on_random_instance():
    """

    Check our criterion with an LP solver on a random instance of the problem.
    Returns the random point, the criterion's outcome on this point (true or
    false) and whether an LP solution was found (positive or negative). If the
    criterion is correct, true == positive and false == negative.

    """

    K1, K2, K3, C1, C2 = 2 * pylab.random(5) - 1
    px, py = X / (X + Y), Y / (X + Y)

    D4max = .5 * min(2, 1 + C1, 1 + C2, 2 + C1 + C2)
    D4min = .5 * max(-1 + C1, C1 + C2, -1 + C2, 0)
    D4 = D4min + (D4max - D4min) * pylab.random()
    D1, D2, D3 = .5 * (1 + C1) - D4, -.5 * (C1 + C2) + D4, .5 * (1 + C2) - D4

    c = cvxopt.matrix(pylab.array([[1.]] * 8))   # score vector
    G = cvxopt.matrix(pylab.array([
        [+1, 0., 0., 0., 0., 0., 0., 0.],
        [-1, 0., 0., 0., 0., 0., 0., 0.],
        [0., +1, 0., 0., 0., 0., 0., 0.],
        [0., -1, 0., 0., 0., 0., 0., 0.],
        [0., 0., +1, 0., 0., 0., 0., 0.],
        [0., 0., -1, 0., 0., 0., 0., 0.],
        [0., 0., 0., +1, 0., 0., 0., 0.],
        [0., 0., 0., -1, 0., 0., 0., 0.],
        [0., 0., 0., 0., +1, 0., 0., 0.],
        [0., 0., 0., 0., -1, 0., 0., 0.],
        [0., 0., 0., 0., 0., +1, 0., 0.],
        [0., 0., 0., 0., 0., -1, 0., 0.],
        [0., 0., 0., 0., 0., 0., +1, 0.],
        [0., 0., 0., 0., 0., 0., -1, 0.],
        [0., 0., 0., 0., 0., 0., 0., +1],
        [0., 0., 0., 0., 0., 0., 0., -1]]))
    h = cvxopt.matrix(pylab.array([[1.]] * 16))  # h - G x >= 0
    A = cvxopt.matrix(pylab.array([
        [D1, D2, D3, D4, 0, 0, 0, 0],
        [0, 0, 0, 0, D1, D2, D3, D4],
        [-py * D1, +py * D2, +py * D3, -py * D4,
         +px * D1, +px * D2, -px * D3, -px * D4]]))
    b = cvxopt.matrix(pylab.array([K1, K2, K3]))
    sol = cvxopt.solvers.lp(c, G, h, A, b)

    K3min = -1 + py * abs(K1 - C1) + px * abs(K2 - C2)
    K3max = 1 - py * abs(K1 + C1) - px * abs(K2 + C2)

    is_true = K3min <= K3 <= K3max
    is_positive = sol['x'] is not None
    return is_true, is_positive, (K1, K2, K3, C1, C2)
Ejemplo n.º 16
0
def scatter_2():
    count = 5
    for x in range(count):
        plb.cla()
        array = plb.random((80, 120))
        plb.imshow(array, cmap=plb.cm.gist_rainbow)
        plb.pause(1)
Ejemplo n.º 17
0
def main():
	shifts = [
		[-1,  1], [0,  1], [1,  1],
		[-1,  0],          [1,  0],
		[-1, -1], [0, -1], [1, -1]
	]

	num_atoms = 100
	num_dims = 2 # dimensions
	coords = pl.random((num_atoms, num_dims))
	chosen = pl.random_integers(num_atoms) # from 1 to num_atoms
	chosen -= 1 # from 0 to num_atoms - 1

	for i in range(len(shifts)):
		coords = pl.vstack((coords, coords[:num_atoms] + shifts[i]))
	num_atoms *= 9 # after 8 shifts added

	max_distance = 0.9
	for i in range(num_atoms):
		if i != chosen:
			dx = coords[chosen, 0] - coords[i, 0]
			dy = coords[chosen, 1] - coords[i, 1]
			distance = pl.sqrt(dx*dx + dy*dy)
			if distance < max_distance:
				pl.plot([coords[i, 0]], [coords[i, 1]], "bo")
			else:
				pl.plot([coords[i, 0]], [coords[i, 1]], "ko")

	# plot last for visibility
	pl.plot([coords[chosen, 0]], [coords[chosen, 1]], "ro")
	pl.grid(True)
	pl.show()
Ejemplo n.º 18
0
def main():
    num_atoms = 64
    num_dims = 2  # dimensions
    coords = pl.random((num_atoms, num_dims))

    axis_limits = [0.0, 1.0]
    points = plot_atoms(coords, num_atoms, axis_limits)

    update_limit = 16
    update_count = 0
    while True:
        chosen = pl.random_integers(num_atoms) - 1  # [0, num_atoms - 1]
        new_x, new_y = new_xy(coords, chosen, axis_limits)

        energy_old, energy_new = energies(coords, chosen, num_atoms, new_x,
                                          new_y)

        if energy_new < energy_old:
            coords[chosen, 0] = new_x
            coords[chosen, 1] = new_y
            points[chosen].set_data([new_x, new_y])
            update_count += 1

        if not update_count < update_limit:
            pl.draw()
            update_count = 0
    """
Ejemplo n.º 19
0
    def __init__(self, config=None):
        super().__init__()
        assert isinstance(config, Config)
        """
        Parameters in config:
        Name:                   Type:           Default:            Description: (Omitted when self-explanatory)
        num_tilings             int             32                  Number of tilings
        tiling_side_length      int             8                   The length of the tiling side
        num_actions             int             3                   Number of actions
        num_dims                int             2                   Number of dimensions
        alpha                   float           0.1                 Learning rate
        state_space_range       np.array        [1,1]               The range of the state space
        """
        self.num_tilings = check_attribute_else_default(config, 'num_tilings', 32)
        self.tiling_side_length = check_attribute_else_default(config, 'tiling_side_length', 8)
        self.num_actions = check_attribute_else_default(config, 'num_actions', 3)
        self.num_dims = check_attribute_else_default(config, 'num_dims', 2)
        self.alpha = check_attribute_else_default(config, 'alpha', 0.1)
        self.state_space_range = check_attribute_else_default(config, 'state_space_range', np.ones(self.num_dims))

        self.scale_factor = self.tiling_side_length * (1 / self.state_space_range)
        self.tiles_per_tiling = self.tiling_side_length ** self.num_dims
        self.num_tiles = (self.num_tilings * self.tiles_per_tiling)
        self.theta = 0.001 * random(self.num_tiles * self.num_actions)
        self.iht = IHT(self.num_tiles)
Ejemplo n.º 20
0
 def reset(self):
     # random() returns a random float in the half open interval [0,1)
     position = -0.6 + random() * 0.2
     velocity = 0.0
     self.current_state = np.array((position, velocity), dtype=np.float32)
     self.step_count = 0
     return self.current_state
 def get_reading (self):
     reading = []
     t = self.target
     for s in self.sensors:
         r = sqrt ((t[0] - s[0])**2 + (t[1] - s[1])**2) + (random () - 0.5)
         reading.append ([s[0], s[1], r])
     return reading
Ejemplo n.º 22
0
 def get_reading(self):
     reading = []
     t = self.target
     for s in self.sensors:
         r = sqrt((t[0] - s[0])**2 + (t[1] - s[1])**2) + (random() - 0.5)
         reading.append([s[0], s[1], r])
     return reading
Ejemplo n.º 23
0
def main():
	num_atoms = 64
	num_dims = 2 # dimensions
	coords = pl.random((num_atoms, num_dims))

	axis_limits = [0.0, 1.0]
	points = plot_atoms(coords, num_atoms, axis_limits)

	update_limit = 16
	update_count = 0
	while True:
		chosen = pl.random_integers(num_atoms) - 1 # [0, num_atoms - 1]
		new_x, new_y = new_xy(coords, chosen, axis_limits)

		energy_old, energy_new =  energies(coords, chosen, num_atoms, new_x, new_y)

		if energy_new < energy_old:
			coords[chosen, 0] = new_x
			coords[chosen, 1] = new_y
			points[chosen].set_data([new_x, new_y])
			update_count += 1

		if not update_count < update_limit:
			pl.draw()
			update_count = 0

	"""
Ejemplo n.º 24
0
 def init_lamda(self):
     lamda = random([self.N, self.K])
     for i in range(0, self.N):
         normalization = sum(lamda[i, :])
         for j in range(0, self.K):
             lamda[i, j] /= normalization
     return lamda
Ejemplo n.º 25
0
    def beeswarm(self, data, position, ratio=2.):
        r"""Naive plotting of the data points

        We assume gaussian distribution so we expect fewers dots
        far from the mean/median. We'd like those dots to be close to the
        axes. conversely, we expect lots of dots centered around the mean, in
        which case, we'd like them to be spread in the box. We uniformly
        distribute position using

        .. math::

            X = X + \dfrac{ U()-0.5 }{ratio} \times factor

        but the factor is based on an arctan function:

        .. math::

            factor = 1 - \arctan( \dfrac{X - \mu }{\pi/2})

        The farther the data is from the mean :math:`\mu`,
        the closest it is to the axes that goes through the box.

        """
        N = len(data)
        m = np.median(data)
        sd = np.std(data)
        # arctan function to have a tapering window
        factor = 1. - np.abs(np.arctan((data-m)/sd)/1.570796)  # pi/2

        newdata = position + (pylab.random(N) - 0.5)/float(ratio) * factor
        return newdata
Ejemplo n.º 26
0
    def beeswarm(self, data, position, ratio=2.):
        r"""Naive plotting of the data points

        We assume gaussian distribution so we expect fewers dots
        far from the mean/median. We'd like those dots to be close to the
        axes. conversely, we expect lots of dots centered around the mean, in
        which case, we'd like them to be spread in the box. We uniformly
        distribute position using

        .. math::

            X = X + \dfrac{ U()-0.5 }{ratio} \times factor

        but the factor is based on an arctan function:

        .. math::

            factor = 1 - \arctan( \dfrac{X - \mu }{\pi/2})

        The farther the data is from the mean :math:`\mu`,
        the closest it is to the axes that goes through the box.

        """
        N = len(data)
        m = np.median(data)
        sd = np.std(data)
        # arctan function to have a tapering window
        factor = 1. - np.abs(np.arctan((data - m) / sd) / 1.570796)  # pi/2

        newdata = position + (pylab.random(N) - 0.5) / float(ratio) * factor
        return newdata
Ejemplo n.º 27
0
def update():
    global agents
    ag = agents[pylab.randint(n)]
    neighbourhood = [
        nb for nb in agents
        if (nb.x - ag.x)**2 + (nb.y - ag.y)**2 < r**2 and nb != ag
    ]
    num_similar = 0
    for j in neighbourhood:
        if j.color == ag.color:
            num_similar += 1
    if len(neighbourhood) > 0:
        ratio = num_similar / float(len(neighbourhood))
        if ratio < th:
            ag.x = pylab.random()
            ag.y = pylab.random()
Ejemplo n.º 28
0
def init():
    global config, nextconfig
    config = pylab.zeros([n, n])
    for x in range(n):
        for y in range(n):
            config[x, y] = 1 if pylab.random() < p else 0
    nextconfig = pylab.zeros([n, n])
Ejemplo n.º 29
0
def init():
    global g, nextg
    g = nx.karate_club_graph()
    g.pos = nx.spring_layout(g)
    for i in g.nodes():
        g.node[i]['state'] = 1 if pylab.random() < 0.5 else 0
    nextg = g.copy()
Ejemplo n.º 30
0
 def init_theta(self):
     theta = random([self.K, self.M])
     for i in range(0, self.K):
         normalization = sum(theta[i, :])
         for j in range(0, self.M):
             theta[i, j] /= normalization
     return theta
def main():
    shifts = [[-1, 1], [0, 1], [1, 1], [-1, 0], [1, 0], [-1, -1], [0, -1],
              [1, -1]]

    num_atoms = 100
    num_dims = 2  # dimensions
    coords = pl.random((num_atoms, num_dims))
    chosen = pl.random_integers(num_atoms)  # from 1 to num_atoms
    chosen -= 1  # from 0 to num_atoms - 1

    for i in range(len(shifts)):
        coords = pl.vstack((coords, coords[:num_atoms] + shifts[i]))
    num_atoms *= 9  # after 8 shifts added

    max_distance = 0.9
    for i in range(num_atoms):
        if i != chosen:
            dx = coords[chosen, 0] - coords[i, 0]
            dy = coords[chosen, 1] - coords[i, 1]
            distance = pl.sqrt(dx * dx + dy * dy)
            if distance < max_distance:
                pl.plot([coords[i, 0]], [coords[i, 1]], "bo")
            else:
                pl.plot([coords[i, 0]], [coords[i, 1]], "ko")

    # plot last for visibility
    pl.plot([coords[chosen, 0]], [coords[chosen, 1]], "ro")
    pl.grid(True)
    pl.show()
Ejemplo n.º 32
0
    def __init__(self, Fs= 16000, TinSec= 10):
        '''
        Fs: 取樣頻率,預設值為 16000,
        TinSec: 保存語音長度,預設值為 10 sec
        '''
        print('RyAudio use %s'%pa.get_portaudio_version_text())
        
        self.Fs= Fs
        self.spBufferSize=           1024
        self.fftWindowSize= self.spBufferSize

        self.aP= pa.PyAudio()
        self.iS= pa.Stream(PA_manager= self.aP, input= True, rate= self.Fs, channels= 1, format= pa.paInt16)
        self.oS= pa.Stream(PA_manager= self.aP, output= True, rate= self.Fs, channels= 1, format= pa.paInt16)
        self.iTh= None
        self.oTh= None

        #self.sound=     None
        #self.soundTime= 0
        self.gettingSound= True
        self.playingSound= True

        self.t= 0
        self.b= None  # byte string
        self.x= None  # ndarray
        self.fft= None
        self.f0= 0#None
        self.en= 0#None
        self.fm= 0#None # frequency mean
        self.fv= 0#None # frequency var
        self.fs= 0#None # frequency std
        self.enP= 0#None # AllPass
        self.enPL= 0#None # LowPass
        self.enPH= 0#None # HighPass

        self.entropy= 0#None

        self.frameI=   0

        #self.frameN= self.spBufferSize/4  #1024/4 = 256
        self.TinSec= TinSec #10 # sec
        self.frameN= self.Fs*self.TinSec/self.spBufferSize #self.spBufferSize/4  #1024/4 = 256
        self.frameN= int(self.frameN)

        self.specgram= pl.random([self.frameN, self.spBufferSize/2])

        self.xBuf= pl.random([self.frameN, self.spBufferSize])
Ejemplo n.º 33
0
def initialize():
    global config, nextconfig, density
    density = []
    config = pl.zeros([n, n])
    for x in range(n):
        for y in range(n):
            config[x, y] = 1 if pl.random() < p else 0
            nextconfig = pl.zeros([n, n])
 def mc_counts2samples(self, counts):
     values = []
     bin = -180
     for value in counts:
         for i in arange(value):
             values.append(bin + random() * self.step)
         bin += self.step
     return array(values)
 def mc_counts2samples( self, counts ) :
     values=[]
     bin=-180
     for value in counts:
         for i in arange(value) :
             values.append(bin+random()*self.step)
         bin +=  self.step
     return array(values)
Ejemplo n.º 36
0
 def __init__(self, numTilings=8, numActions=3):
     self.actions = numActions
     self.numTilings = numTilings
     self.alpha_factor = 1 / self.numTilings
     self.numTiles = (self.numTilings**3) * 4
     self.iht = IHT(self.numTiles)
     self.theta = 0.001 * random(self.numTiles * self.actions)
     super().__init__()
Ejemplo n.º 37
0
 def next(self):
     Tnext = ((self.Konstant * self.t1) * 2) - self.t0
     if len(self.values) % 100 > 70:
         self.values.append(pylab.random() * 2 - 1)
     else:
         self.values.append(Tnext)
     self.t0 = self.t1
     self.t1 = Tnext
     return self.values[-1]
 def markov_trajectory(self,distribution,state=None):
     if state == None :
         state = self.start_point(distribution)
     trj = []
     for i in range(self.frames):
         state = self.markov_step(state,distribution)
         angle = self.mod2pi((state+random())*self.step-180)
         trj.append(angle)
     return array(trj)
 def markov_trajectory(self, distribution, state=None):
     if state == None:
         state = self.start_point(distribution)
     trj = []
     for i in range(self.frames):
         state = self.markov_step(state, distribution)
         angle = self.mod2pi((state + random()) * self.step - 180)
         trj.append(angle)
     return array(trj)
Ejemplo n.º 40
0
def sqr_cplx(z):
    """
    Fonction racine d'un complexe. Prend aléatoirement la racine
    positive ou négative
    """
    r, theta = cart2pol(z)
    r = pl.sqrt(r)
    theta = theta/2 + int(2*pl.random())*pl.pi
    return pol2cart(r, theta)
Ejemplo n.º 41
0
    def initialize(self, N, K, M, word2id, id2word, X):
        self.word2id, self.id2word, self.X = word2id, id2word, X
        self.N, self.K, self.M = N, K, M
        # theta[i, j] : p(zj|di): 2-D matrix
        self.theta = random([N, K])
        # beta[i, j] : p(wj|zi): 2-D matrix
        self.beta = random([K, M])
        # p[i, j, k] : p(zk|di,wj): 3-D tensor
        self.p = zeros([N, M, K])
        for i in range(0, N):
            normalization = sum(self.theta[i, :])
            for j in range(0, K):
                self.theta[i, j] /= normalization

        for i in range(0, K):
            normalization = sum(self.beta[i, :])
            for j in range(0, M):
                self.beta[i, j] /= normalization
Ejemplo n.º 42
0
def random_euler_angles():
    r1,r2,r3 = pylab.random(3)
    q1 = pylab.sqrt(1.0-r1)*pylab.sin(2.0*pylab.pi*r2)
    q2 = pylab.sqrt(1.0-r1)*pylab.cos(2.0*pylab.pi*r2)
    q3 = pylab.sqrt(r1)*pylab.sin(2.0*pylab.pi*r3)
    q4 = pylab.sqrt(r1)*pylab.cos(2.0*pylab.pi*r3)
    phi = math.atan2(2.0*(q1*q2+q3*q4), 1.0-2.0*(q2**2+q3**2))
    theta = math.asin(2.0*(q1*q3-q4*q2))
    psi = math.atan2(2.0*(q1*q4+q2*q3), 1.0-2.0*(q3**2+q4**2))
    return [phi,theta,psi]
Ejemplo n.º 43
0
def update():
    global g
    chosenedge = rd.choice(list(g.edges()))
    if (pylab.random() < 0.5):
        listener = chosenedge[0]
        speaker = chosenedge[1]
    else:
        listener = chosenedge[1]
        speaker = chosenedge[0]
    g.node[listener]['state'] = g.node[speaker]['state']
Ejemplo n.º 44
0
 def initialize(self):
     self.state = pylab.zeros([self.n, self.n])
     for x in xrange(self.n):
         for y in xrange(self.n):
             self.state[x, y] = 1 if pylab.random() < self.init_p else 0
     self.state[self.n/2, self.n/2] = 2
     self.next_state = pylab.zeros([self.n, self.n])
     for x in xrange(self.n):
         for y in xrange(self.n):
         	if self.state[x, y] == 1 or self.state[x, y] == 2:
         		self.total_num_trees += 1
Ejemplo n.º 45
0
def GenerateRandomTrajectory(ncurve, ndof, bound):
    def vector2string(v):
        ndof = len(v)
        s = str(ndof)
        for a in v:
            s += ' %f' % a
        return s

    p0a = vector2string(random(ndof) * 2 * bound - bound)
    p0b = vector2string(random(ndof) * 2 * bound - bound)
    p1a = vector2string(random(ndof) * 2 * bound - bound)
    p1b = vector2string(random(ndof) * 2 * bound - bound)
    s = '%d' % ncurve
    s += '\n1.0 ' + p0a + ' ' + p0b
    for k in range(ncurve - 1):
        a = random(ndof) * 2 * bound - bound
        b = random(ndof) * 2 * bound - bound
        c = 2 * b - a
        pa = vector2string(a)
        pb = vector2string(b)
        pc = vector2string(c)
        s += ' ' + pa + ' ' + pb + '\n1.0 ' + pb + ' ' + pc
    s += ' ' + p1a + ' ' + p1b
    Tv, p0v, p1v, p2v, p3v = string2p(s)
    return BezierToTrajectoryString(Tv, p0v, p1v, p2v, p3v)
Ejemplo n.º 46
0
    def __init__(self,n=100,state=0.5,J=1.,T=2.,):
        """Constructor..."""
        self.n = n
        self.spins = -1 * pl.ones((n,n))
        self.J = J
        self.T=T
        self.init = state
        self.config = "n=%d,init=%f,J=%f,T=%f" % (self.n,self.init,self.J,self.T)

        for i in xrange(n):
            for j in xrange(n):
                if pl.random() < (1 + state) / 2.:
                    self.spins[i,j] = 1
Ejemplo n.º 47
0
def packing(dataset, radius=4, nifti=False, randoffset=False):
	"""return a hexagonal close sphere packing grid for a PyMVPA fMRI dataset
    
    Keyword arguments:
    radius-- radius in voxels of the spheres to pack (default 4)
    nifti-- write out a seed voxel mask as a nifti
    randomoffset-- random jitter of the seed voxel grid

    """
    
	from pylab import find, random
	from numpy import ones, zeros, arange, sqrt, remainder
	from mvpa2.suite import fmri_dataset, Dataset
	import os

	if randoffset:
		ro = random(3)
	else:
		ro = zeros(3)
	
	minco = dataset.fa.voxel_indices.min(0)
	maxco = dataset.fa.voxel_indices.max(0)
	rect = ones(dataset.a.voxel_dim)
	
	fac = sqrt(6)*2*radius/3
	for iz,z in enumerate(arange(minco[2], maxco[2], fac)):
		for iy,y in enumerate(arange(minco[1], maxco[1], fac)):
			for x in arange(minco[0], maxco[0], 2*radius):
				hx = x + remainder(iy, 2)*radius + ro[0]*radius
				hy = y + remainder(iz, 2)*fac + ro[1]*radius
				hz = z + ro[2]*radius
				if hz <= maxco[2]:
					rect [hx, hy, hz] += 1

	maskedrect = dataset.mapper.forward1(rect)
	roiIndex = find((maskedrect == 2))
	print  'number of seed voxel: '+str(len(roiIndex))
	
	maskedrectds = Dataset([maskedrect])
	maskedrectds.a = dataset.a.copy()
	maskedrectds.fa = dataset.fa.copy()
    
	if nifti:
		from nibabel import Nifti1Image
		Nifti1Image(maskedrectds.O.squeeze(),
					dataset.a.imghdr.get_best_affine()
					).to_filename(os.path.join('sparse'+str(int(radius))+'.nii.gz'))

	return roiIndex, maskedrectds
Ejemplo n.º 48
0
def generate_waveforms(
    N_harmonics=[8,16,32,64],
    path='/home/ritz/mix/audio samples instruments/basic-waveforms/',
    name='square_rnd_phase-%03dh-G2-(i).wav'):
    '''Generate a series of basic additive waveforms with a varying number of harmonics.'''
    f0 = 49.0
    w0 = pl.round(sr / f0)
    f0 = sr / w0
    pl.clf()
    for ii, N in enumerate(N_harmonics):
        H = 1.0 + 2 * pl.arange(N)
        waev = beep(pl.c_[f0 * H, pl.random(N) * tau, 1 / H], 8 * w0)
        pl.subplot(3,4,ii+1)
        pl.plot(waev)
        write(waev, path + (name % N))
 def markov_step(self,state,distribution):
     state = int(state)
     for i in range(self.keep_every) :
         if state == len(distribution) :
             state = 0
         if state == 0 :
             im = len(distribution)-1
         else :
             im = state-1
         if state == len(distribution) - 1:
             ip = 0
         else :
             ip = state+1   
         pi = distribution[state]
         pim = distribution[im]
         pip = distribution[ip]
         attempt = randint(0,2)
         if attempt == 0 : 
             if pim > pi : 
                 state = im
             else : 
                 z = random()
                 if z < pim/pi :
                     state = im
                 else :
                     state = state
         elif attempt == 1 :
             if pip > pi:
                 state = ip
             else : 
                 z = random()
                 if z < pip/pi :
                     state = ip
                 else :
                     state = state
     return state
Ejemplo n.º 50
0
    def step(self):
        """Run one step of the Metropolis algorithm"""
        site = self.getsite()
        
        #Now need to work out the energy difference between the lattices.
        #This is used to determine the probability that we'll keep the
        #new configuration.
        Ediff = -2 * self.Hij(site)
        Sdiff = -2 * self.spins[site]

        if self.probaccept(Ediff) > pl.random():
            self.spinflip(site)
            return (Ediff, Sdiff)
        else:
            return (0.,0.)
Ejemplo n.º 51
0
def SampleUnitTriangle(N, D):
    """Randomly sample from the unit triangle.
    
    Args:
        N: the number of points to sample.
        D: the dimensionality.
        
    Returns:
        A matrix containing the sampled points.
    
    Stolen from Elad's code. I'm a thief, I know.
    """
    X = p.ones((N, D))
    for i in xrange(N):
        while (sum(X[i, :]) >= 1):
            X[i, :] = p.random(D)
    return X
Ejemplo n.º 52
0
def poisson_train(rate, duration):
  """Return time stamps from a simulated poisson process at given rate and over given duration. The granularity is 1ms.
  We break up duration into N bins of 1ms size. The probability of there being an event (spike) in a given bin is the
  same as the rate in kHz. We generate N uniformly distributed random numbers and use that to decide if a given bin has
  a spike or not.

  Inputs:
    rate - In Hz
    duration - in s
  Output:
    Array of time stamps in s
  """
  p = rate/1000.0 #Probablity is rate (s-1) * bin size (s)
  N = int(duration*1000) #One bin per ms
  r = pylab.random(N)
  idx = pylab.find(r < p)
  return idx/1000.
Ejemplo n.º 53
0
    def cstep(self):
        """Run one step of the Metropolis algorithm using weave"""
        i,j = (pl.randint(0,self.n),pl.randint(0,self.n))

        spins = self.spins
        n = self.n
        J = self.J
        T = self.T
        
        code = """
        #include <math.h>
        
        double neighbour_sum = 0;
        neighbour_sum += spins(i%n,(j-1+n)%n);
        neighbour_sum += spins(i%n,(j+1)%n);
        neighbour_sum += spins((i-1+n)%n,j%n);
        neighbour_sum += spins((i+1)%n,j%n);

        double Ediff = 2 * J * spins(i,j) * neighbour_sum;
        double Sdiff = -2 * spins(i,j);

        double PA = 1.;
        if(Ediff > 0) {
        if(T == 0) {
        PA = 0.;
        }
        else {
        PA = exp(-1/T*Ediff);
        }
        }

        py::tuple results(3);
        results[0] = PA;
        results[1] = Ediff;
        results[2] = Sdiff;
        return_val = results;
        """

        PA,Ediff,Sdiff = weave.inline(code, ['spins','J','T','n','i','j'], type_converters=weave.converters.blitz)
        
        if PA > pl.random():
                self.spinflip((i,j))
                return Ediff,Sdiff
        else:
                return 0.,0.
Ejemplo n.º 54
0
 def initLines(self, parameters):
     p = parameters
     p["imageObj"] = p["imageAx"].imshow(random([2, 2]), self.whiteCMap, interpolation="none", aspect="auto")
     p["lineFitH"], = p["imageAx"].plot(
         array([0, 1040]) * p["pixelSize"] * p["axScale"], [-1e5, -1e5], ":", lw=2, color="0.15"
     )
     p["lineFitV"], = p["imageAx"].plot(
         [-1e5, -1e5], array([0, 1392]) * p["pixelSize"] * p["axScale"], ":", lw=2, color="0.15"
     )
     p["lineMarker1H"], = p["imageAx"].plot(
         array([0, 1040]) * p["pixelSize"] * p["axScale"], [-1e5, -1e5], "k-", lw=1
     )
     p["lineMarker1V"], = p["imageAx"].plot(
         [-1e5, -1e5], array([0, 1382]) * p["pixelSize"] * p["axScale"], "k-", lw=1
     )
     self.drawCrosshair(p["lineMarker1H"], p["lineMarker1V"], p["Marker1Pos"])
     p["lineMarker2H"], = p["imageAx"].plot(
         array([0, 1040]) * p["pixelSize"] * p["axScale"], [-1e5, -1e5], "r-", lw=1
     )
     p["lineMarker2V"], = p["imageAx"].plot(
         [-1e5, -1e5], array([0, 1382]) * p["pixelSize"] * p["axScale"], "r-", lw=1
     )
     self.drawCrosshair(p["lineMarker2H"], p["lineMarker2V"], p["Marker2Pos"])
     p["imageAx"].set_xlim(p["xylim"][0])
     p["imageAx"].set_ylim(p["xylim"][1])
     p["imageAx"].set_xlabel(u"\u00b5m")
     p["imageAx"].set_ylabel(u"\u00b5m")
     if not self.roiOnly_check_button.get_active():
         lc = p["partLumCorr"] * p["pixelSize"] * p["axScale"]
         ac = p["partAtomCount"] * p["pixelSize"] * p["axScale"]
         rectLumCorr = plt.Rectangle((lc[2], lc[0]), lc[3] - lc[2], lc[1] - lc[0], ec="red", fc="none", ls="dotted")
         rectAtomCount = plt.Rectangle(
             (ac[2], ac[0]), ac[3] - ac[2], ac[1] - ac[0], ec="black", fc="none", ls="dashed"
         )
         p["imageAx"].add_patch(rectLumCorr)
         p["imageAx"].add_patch(rectAtomCount)
Ejemplo n.º 55
0
def init():
    position = -0.6 + random()*0.2
    return position, 0.0
Ejemplo n.º 56
0
    plt.show()
    return ax

def clean(dir, var):
    '''
    Remove masked values in the two arrays, where if a direction data is masked,
    the var data will also be removed in the cleaning process (and vice-versa)
    '''
    dirmask = dir.mask==False
    varmask = var.mask==False
    ind = dirmask*varmask
    return dir[ind], var[ind]

if __name__=='__main__':
    from pylab import figure, show, setp, random, grid, draw
    vv=random(500)*6
    dv=random(500)*360
    fig = figure(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='w')
    rect = [0.1, 0.1, 0.8, 0.8]
    ax = WindroseAxes(fig, rect, axisbg='w')
    fig.add_axes(ax)

#    ax.contourf(dv, vv, bins=np.arange(0,8,1), cmap=cm.hot)
#    ax.contour(dv, vv, bins=np.arange(0,8,1), colors='k')
#    ax.bar(dv, vv, normed=True, opening=0.8, edgecolor='white')
    ax.box(dv, vv, normed=True)
    l = ax.legend(axespad=-0.10)
    setp(l.get_texts(), fontsize=8)
    draw()
    #print ax._info
    show()
Ejemplo n.º 57
0
def train(numSteps):
    for i in range(numSteps):
        in1 = random() * 6.0
        in2 = random() * 6.0
        target = targetFunction(in1,in2)
        learn(in1,in2,target)
Ejemplo n.º 58
0
def testAB():
    A=20*P.random(50)
    B=30+10*P.random(52)
    plotAB(A,B)
Ejemplo n.º 59
0
    datasetFilePath = sys.argv[1]
    stopwordsFilePath = sys.argv[2]
    K = int(sys.argv[3])
    maxIteration = int(sys.argv[4])
    threshold = float(sys.argv[5])
    topicWordsNum = int(sys.argv[6])
    docTopicDist = sys.argv[7]
    topicWordDist = sys.argv[8]
    dictionary = sys.argv[9]
    topicWords = sys.argv[10]

# preprocessing
N, M, word2id, id2word, X = preprocessing(datasetFilePath, stopwordsFilePath)

# lamda[i, j] : p(zj|di)
lamda = random([N, K])

# theta[i, j] : p(wj|zi)
theta = random([K, M])

# p[i, j, k] : p(zk|di,wj)
p = zeros([N, M, K])

initializeParameters()

# EM algorithm
oldLoglikelihood = 1
newLoglikelihood = 1
for i in range(0, maxIteration):
    EStep()
    MStep()