Python math.log2() Examples

The following are code examples for showing how to use math.log2(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: chainer-began   Author: hvy   File: models.py    (license) View Source Project 6 votes vote down vote up
def __init__(self, n, h, in_size, in_channels, embed_size, block_size):
        super().__init__(
            l0=L.Convolution2D(in_channels, n, 3, stride=1, pad=1),
            ln=L.Linear(None, h))

        self.n_blocks = int(log2(in_size / embed_size)) + 1
        self.block_size = block_size

        for i in range(self.n_blocks):
            n_in = (i + 1) * n
            n_out = (i + 2) * n if i < self.n_blocks - 1 else n_in
            for j in range(block_size - 1):
                self.add_link('c{}'.format(i * block_size + j),
                              L.Convolution2D(n_in, n_in, 3, stride=1, pad=1))
            self.add_link('c{}'.format(i * block_size + block_size - 1),
                          L.Convolution2D(n_in, n_out, 3, stride=1, pad=1)) 
Example 2
Project: 20Qs   Author: Cypher1   File: qa_bot.py    (license) View Source Project 6 votes vote down vote up
def get_entropy(question, animals):
    """ Finds the entropy of the answers of a question for a set of animals """
    response_set = {answer:0.000001 for answer in ANSWERS}

    all_responses = query_all_responses(question=question, animals=animals)
    for animal in animals:
        responses = all_responses.get(animal.name, NO_RESPONSE)
        if responses is not None:
            responses = sorted(
                responses.items(),
                key=lambda resp: -resp[1]
            )
            (first, _) = responses[0]
            (last, _) = responses[-1]
            response_set[first] += animal.prob
            response_set[last] += (1-animal.prob)
        else:
            print("COULDN'T FIND ANIMAL \"{}\"".format(animal.name))

    total = sum(response_set.values())
    probs = [count/total for count in response_set.values()]

    entropy = sum([-(p)*log2(p) for p in probs])
    return entropy 
Example 3
Project: ecs   Author: ecs-org   File: models.py    (license) View Source Project 6 votes vote down vote up
def create_for_user(cls, pkcs12, user, cn=None, days=None):
        if not cn:
            cn = get_full_name(user)
        ec = EthicsCommission.objects.get(uuid=settings.ETHICS_COMMISSION_UUID)
        subject = '/CN={}/O={}/emailAddress={}'.format(cn, ec.name[:64], user.email)

        passphrase_len = math.ceil(
            PASSPHRASE_ENTROPY / math.log2(len(PASSPHRASE_CHARS)))
        passphrase = ''.join(
            SystemRandom().choice(PASSPHRASE_CHARS)
            for i in range(passphrase_len)
        )

        from ecs.pki import openssl
        data = openssl.make_cert(subject, pkcs12, passphrase=passphrase,
            days=days)
        cert = cls.objects.create(user=user, cn=cn, **data)

        return (cert, passphrase) 
Example 4
Project: CRYPTO   Author: ndiab   File: PKCS1.py    (license) View Source Project 6 votes vote down vote up
def PKCS1(message : int, size_block : int) -> int:
	"""
	PKCS1 padding function
	the format of this padding is :
	
	0x02 | 0x00 | [0xFF...0xFF] | 0x00 | [message]
	"""
	# compute the length in bytes of the message
	length = math.ceil(math.ceil(math.log2(message-1)) / 8)

	template = "0200"
	
	# generate a template 0xFFFFF.....FF of size_block bytes
	for i in range(size_block-2):
		template = template + 'FF'
	template = int(template,16)

	# Add the 00 of the end of the padding to the template
	for i in range(length+1) :
		template = template ^ (0xFF << i*8)

	# add the padding to the original message
	message = message | template
	
	return message 
Example 5
Project: web_ctp   Author: molebot   File: test_math.py    (license) View Source Project 6 votes vote down vote up
def testLog2(self):
        self.assertRaises(TypeError, math.log2)

        # Check some integer values
        self.assertEqual(math.log2(1), 0.0)
        self.assertEqual(math.log2(2), 1.0)
        self.assertEqual(math.log2(4), 2.0)

        # Large integer values
        #fixme brython (javascript chokes on these large values)
        #self.assertEqual(math.log2(2**1023), 1023.0)
        #self.assertEqual(math.log2(2**1024), 1024.0)
        #self.assertEqual(math.log2(2**2000), 2000.0)

        self.assertRaises(ValueError, math.log2, -1.5)
        self.assertRaises(ValueError, math.log2, NINF)
        self.assertTrue(math.isnan(math.log2(NAN)))

    #@unittest.skip("brython, skip for now") 
Example 6
Project: pylearning   Author: amstuta   File: trees.py    (license) View Source Project 6 votes vote down vote up
def set_number_features_evaluated_split(self, row):
        """
        Sets the number of considered features at each split depending on the
        max_split_features parameter.
        :param row: A single row of the features of shape (nb_features)
        """
        if isinstance(self.max_split_features, int):
            self.considered_features = self.max_split_features if \
                self.max_split_features <= len(row) else len(row)
        elif isinstance(self.max_split_features, str):
            if self.max_split_features in ['auto','sqrt']:
                self.considered_features = int(sqrt(len(row)))
            elif self.max_split_features == 'log2':
                self.considered_features = int(log2(len(row)))
        else:
            self.considered_features = len(row) 
Example 7
Project: ouroboros   Author: pybee   File: test_math.py    (license) View Source Project 6 votes vote down vote up
def testLog2(self):
        self.assertRaises(TypeError, math.log2)

        # Check some integer values
        self.assertEqual(math.log2(1), 0.0)
        self.assertEqual(math.log2(2), 1.0)
        self.assertEqual(math.log2(4), 2.0)

        # Large integer values
        self.assertEqual(math.log2(2**1023), 1023.0)
        self.assertEqual(math.log2(2**1024), 1024.0)
        self.assertEqual(math.log2(2**2000), 2000.0)

        self.assertRaises(ValueError, math.log2, -1.5)
        self.assertRaises(ValueError, math.log2, NINF)
        self.assertTrue(math.isnan(math.log2(NAN))) 
Example 8
Project: kbe_server   Author: xiaohaoppy   File: test_math.py    (license) View Source Project 6 votes vote down vote up
def testLog2(self):
        self.assertRaises(TypeError, math.log2)

        # Check some integer values
        self.assertEqual(math.log2(1), 0.0)
        self.assertEqual(math.log2(2), 1.0)
        self.assertEqual(math.log2(4), 2.0)

        # Large integer values
        self.assertEqual(math.log2(2**1023), 1023.0)
        self.assertEqual(math.log2(2**1024), 1024.0)
        self.assertEqual(math.log2(2**2000), 2000.0)

        self.assertRaises(ValueError, math.log2, -1.5)
        self.assertRaises(ValueError, math.log2, NINF)
        self.assertTrue(math.isnan(math.log2(NAN))) 
Example 9
Project: tinder-telegram-bot   Author: arthurdk   File: Bot.py    (license) View Source Project 6 votes vote down vote up
def dynamic_timeout_formular(min_votes, votes_fraction):
    """
    Formula used for dynamic timeout
    :param min_votes:
    :param votes_fraction:
    :return:
    """
    if votes_fraction >= 1:
        return 1 / votes_fraction  # Reduce timeout if more people than necessary voted

    result = 1
    result += (1 - votes_fraction) * math.log2(min_votes)  # Linear part makes timeout rise
    result += min_votes * (
        min_votes ** ((1 - votes_fraction) ** 3) - 1)  # Exponential part to punish really low vote counts
    result += (40 - 40 ** (votes_fraction)) / min_votes ** 2  # Punish missing votes harder if min_votes is low
    return result 
Example 10
Project: MakePass   Author: Lucretiel   File: makepass.py    (license) View Source Project 6 votes vote down vote up
def sampled_entropy(sample_size, success_size):
	'''
	Estimate the change in entropy given a sample of passwords from a set.

	Rationalle: Assume that we can produce passwords with a known entropy.
	However, not all of these passwords are at least 24 characters long. Rather
	than try to calculate the exact change in entropy, we estimate it by
	generating {sample_size} passwords, and seeing that {success_size} meet our
	constraints. We then assume that the ratio of these numbers is proportional
	to the overall ratio of possible_passwords to allowed_passwords. This
	allows us to do the following caluclation:

	original_entropy = log2(permutation_space)
	new_entropy = log2(permutation_space * ratio)
		= log2(permutation_space) + log2(ratio)
		= log2(permutation_space) + log2(success_size / sample_size)
		= log2(permutation_space) + log2(success_size) - log2(sample_size)
		= original_entropy + log2(success_size) - log2(sample_size)
	'''
	return log2(success_size) - log2(sample_size) 
Example 11
Project: pyMusicSync   Author: tuankiet65   File: cover_art.py    (license) View Source Project 6 votes vote down vote up
def Waifu2xResize(src, width, height, targetWidth, targetHeight):
    resize_factor = 2 ** math.ceil(max(math.log2(targetHeight / height), math.log2(targetWidth / targetWidth)))
    logging.debug("w: {} h: {} tw: {} th: {} rf: {}".format(width, height, targetWidth, targetHeight, resize_factor))
    tmp = tempfile.mkstemp(suffix=".png", prefix="pmsync_cover_")
    try:
        subprocess.run(["waifu2x-converter-cpp",
                        "--scale_ratio", str(resize_factor),
                        "-m", "scale",
                        "-i", src,
                        "-o", tmp[1]],
                       check=True,
                       stdin=subprocess.DEVNULL,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)
    except subprocess.CalledProcessError as e:
        logging.debug("=== CalledProcessError ===")
        logging.debug("cmd: {}".format(e.cmd))
        logging.debug("output: {}".format(e.stdout.decode()))
        logging.debug("stderr: {}".format(e.stderr.decode()))
        logging.debug("=== CalledProcessError ===")
        os.remove(tmp[1])
    return PILResize(tmp[1], width*resize_factor, height*resize_factor, targetWidth, targetHeight) 
Example 12
Project: Sentiment-Analysis   Author: Suraj-Yadav   File: Classifier.py    (license) View Source Project 6 votes vote down vote up
def getClass(self, string):
		maxProb = float('-inf')
		maxClass = ''
		
		for i in self.classCount:
			logProb = log2(self.classCount[i])-log2(sum(self.classCount.values()))
			# print(i)
			for word in string.split():
				if word in self.wordsFreq:
					wordLogProb = log2(self.wordsFreq[word][i]+1)-log2(len(self.classFreq[i])+len(self.wordsFreq)+1)
				else:
					wordLogProb = -log2(len(self.classFreq[i])+len(self.wordsFreq)+1)
				logProb += wordLogProb
				# print('\t',word, wordProb)
			
			# print(prob,'\n****************')
		
			if logProb > maxProb:
				maxProb = logProb
				maxClass = i
		
		# print(maxProb)
		
		return maxClass 
Example 13
Project: SLP-Annotator   Author: PhonologicalCorpusTools   File: functional_load.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def calculateEntropy(self, corpus=None):
        corpus_size = len(corpus) if corpus is not None else len(self.corpus)
        return corpus_size, sum([1 / corpus_size * log(1 / corpus_size) for n in range(corpus_size)]) * -1 
Example 14
Project: blemd   Author: niacdoial   File: BinaryWriter.py    (license) View Source Project 5 votes vote down vote up
def WriteFloat(self, v):
        if v < 0:
            neg = (1 << 31)
            v *= -1
        else:
            neg = 0
        if v == 0:
            e = 0
            m = 0
        else:
            e = int(floor(log2(v/0x1000000))+1 + 150)
        if e < 0 or e > 0xff:
            print("float off range, assuming zero", file=sys.stderr)
            e = 0
            m = 0

        elif e == 0:
            e = e << 23
            m = int(v / 2**(-150)) >> 1
        else:
            e = e << 23
            m = int(v/2**(floor(log2(v/0x1000000))+1))
            if m < 0x800000 or m >= 0x1000000:
                raise ValueError('float dump: bad m value')
            m &= 0x7fffff
        self.writeDword(neg | e | m) 
Example 15
Project: transpyler   Author: Transpyler   File: math.py    (license) View Source Project 5 votes vote down vote up
def log2(x: Real) -> Real:
    """
    Return the logarithm of x in base 2.
    """
    return _math.log2(x) 
Example 16
Project: HoeffdingTree   Author: vitords   File: infogainsplitmetric.py    (license) View Source Project 5 votes vote down vote up
def get_metric_range(self, pre_dist):
        num_classes = len(pre_dist)
        if num_classes < 2:
            num_classes = 2

        return math.log2(num_classes) 
Example 17
Project: hwtLib   Author: Nic30   File: storedBurst.py    (license) View Source Project 5 votes vote down vote up
def _impl(self):
        self.DATA_WIDTH = int(self.DATA_WIDTH)
        vldAll = mask(self.DATA_WIDTH // 8)
        dout = self.dataOut
        DATA_LEN = len(self.DATA)

        wordIndex_w = int(math.log2(DATA_LEN) + 1)
        wordIndex = self._reg("wordIndex", Bits(wordIndex_w), defVal=0)

        Switch(wordIndex)\
        .addCases([(i, dout.data(d))
                    for i, d in enumerate(self.DATA)])\
        .Default(dout.data(None))

        dout.last(wordIndex._eq(DATA_LEN - 1))
        If(wordIndex < DATA_LEN,
            dout.strb(vldAll),
            dout.valid(1)
        ).Else(
            dout.strb(None),
            dout.valid(0)
        )

        If(self.dataRd(),
            self.nextWordIndexLogic(wordIndex)
        ) 
Example 18
Project: gaft   Author: PytLab   File: binary_individual.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, ranges, eps=0.001):
        '''
        Class for individual in population. Random solution will be initialized
        by default.

        NOTE: The decrete precisions for different components in varants may be
              adjusted automatically (possible precision loss) if eps and ranges
              are not appropriate.

        :param ranges: value ranges for all entries in solution.
        :type ranges: list of range tuples. e.g. [(0, 1), (-1, 1)]

        :param eps: decrete precisions for binary encoding, default is 0.001.
        :type eps: float or float list with the same length with ranges.
        '''
        super(self.__class__, self).__init__(ranges, eps)

        # Lengths for all binary sequence in chromsome and adjusted decrete precisions.
        self.lengths = []

        for i, ((a, b), eps) in enumerate(zip(self.ranges, self.eps)):
            length = int(log2((b - a)/eps))
            precision = (b - a)/(2**length)
            self.lengths.append(length)
            self.precisions[i] = precision

        # The start and end indices for each gene segment for entries in solution.
        self.gene_indices = self._get_gene_indices()

        # Initialize individual randomly.
        self.init() 
Example 19
Project: PTTChatBot_DL2017   Author: thisray   File: bm25Matcher.py    (license) View Source Project 5 votes vote down vote up
def calculateIDF(self):

        # ???????????????
        if len(self.wordset) == 0:
            self.buildWordSet()
        if len(self.words_location_record) == 0:
            self.buildWordLocationRecord()

        # ?? idf
        for word in self.wordset:
            self.words_idf[word] = math.log2((self.D + .5)/(self.words_location_record[word] + .5)) 
Example 20
Project: copycat   Author: LSaldyt   File: temperature.py    (license) View Source Project 5 votes vote down vote up
def _entropy(temp, prob):
    if prob == 0 or prob == 0.5 or temp == 0:
        return prob
    if prob < 0.5:
        return 1.0 - _original(temp, 1.0 - prob)
    coldness = 100.0 - temp
    a = math.sqrt(coldness)
    c = (10 - a) / 100
    f = (c + 1) * prob
    return -f * math.log2(f) 
Example 21
Project: aioworkers   Author: aioworkers   File: humanize.py    (license) View Source Project 5 votes vote down vote up
def size(value, suffixes=None):
    """
    >>> size(1024)
    '1 KB'

    :param size: 
    :param suffixes: 
    :return: 
    """
    suffixes = suffixes or [
        'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    order = int(log2(value) / 10) if value else 0
    return '{:.4g} {}'.format(value / (1 << (order * 10)), suffixes[order]) 
Example 22
Project: chainer-began   Author: hvy   File: models.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, n, out_size, out_channels, embed_size, block_size):
        super().__init__(
                l0=L.Linear(None, n * embed_size * embed_size),
                ln=L.Convolution2D(n, out_channels, 3, stride=1, pad=1))

        self.embed_shape = (n, embed_size, embed_size)
        self.n_blocks = int(log2(out_size / embed_size)) + 1
        self.block_size = block_size

        for i in range(self.n_blocks * block_size):
            self.add_link('c{}'.format(i),
                          L.Convolution2D(n, n, 3, stride=1, pad=1)) 
Example 23
Project: information-bottleneck   Author: djstrouse   File: IB.py    (license) View Source Project 5 votes vote down vote up
def entropy_term(x):
    """Helper function for entropy_single: calculates one term in the sum."""
    if x==0: return 0.0
    else: return -x*math.log2(x) 
Example 24
Project: information-bottleneck   Author: djstrouse   File: IB.py    (license) View Source Project 5 votes vote down vote up
def kl_term(x,y):
    """Helper function for kl: calculates one term in the sum."""
    if x>0 and y>0: return x*math.log2(x/y)
    elif x==0: return 0.0
    else: return math.inf 
Example 25
Project: classifier   Author: 2014zhouyou   File: classifiers.py    (license) View Source Project 5 votes vote down vote up
def _calc_entropy(self, data_set):
        """
        compute the entropy of the data_set
        :param data_set: the data_set you want to calculator entropy
        :return: x, x is a numerical value represent the entropy
        """
        class_list = [example[-1] for example in data_set]
        unique_class = set(class_list)
        record_num = len(data_set)
        entropy = 0.0
        for item in unique_class:
            item_data = self._find_record_with_value(-1, data_set, item)
            probability = len(item_data) / record_num
            entropy -= probability * math.log2(probability)
        return entropy 
Example 26
Project: CRYPTO   Author: ndiab   File: encoding.py    (license) View Source Project 5 votes vote down vote up
def decode(message : int, pad_option : str) -> str :
	
	size_block = math.ceil(math.ceil(math.log2(message))/8)*8
	
	message = unpadding(message, size_block, pad_option)

	message = binascii.unhexlify(hex(message)[2:])

	return str(message)[2:-1] 
Example 27
Project: CRYPTO   Author: ndiab   File: PKCS1.py    (license) View Source Project 5 votes vote down vote up
def unpad_PKCS1(message : int, size_block : int) -> int:
	"""
	PKCS1 unpadding function
	"""

	pts = 0xff << (size_block - 24)

	while (pts & message != 0):
		pts = pts >> 8
	
	a = ~pts & ((1 << size_block) - 1)

	message = message & (~pts & ((1 << math.ceil(math.log2(pts))) - 1) )

	return message 
Example 28
Project: SPAWC2017   Author: Haoran-S   File: function_wmmse_powercontrol.py    (license) View Source Project 5 votes vote down vote up
def obj_IA_sum_rate(H, p, var_noise, K):
    y = 0.0
    for i in range(K):
        s = var_noise
        for j in range(K):
            if j!=i:
                s = s+H[i,j]**2*p[j]
        y = y+math.log2(1+H[i,i]**2*p[i]/s)
    return y

# Functions for WMMSE algorithm 
Example 29
Project: SPAWC2017   Author: Haoran-S   File: function_wmmse_powercontrol.py    (license) View Source Project 5 votes vote down vote up
def WMMSE_sum_rate(p_int, H, Pmax, var_noise):
    K = np.size(p_int)
    vnew = 0
    b = np.sqrt(p_int)
    f = np.zeros(K)
    w = np.zeros(K)
    for i in range(K):
        f[i] = H[i, i] * b[i] / (np.square(H[i, :]) @ np.square(b) + var_noise)
        w[i] = 1 / (1 - f[i] * b[i] * H[i, i])
        vnew = vnew + math.log2(w[i])

    VV = np.zeros(100)
    for iter in range(100):
        vold = vnew
        for i in range(K):
            btmp = w[i] * f[i] * H[i, i] / sum(w * np.square(f) * np.square(H[:, i]))
            b[i] = min(btmp, np.sqrt(Pmax)) + max(btmp, 0) - btmp

        vnew = 0
        for i in range(K):
            f[i] = H[i, i] * b[i] / ((np.square(H[i, :])) @ (np.square(b)) + var_noise)
            w[i] = 1 / (1 - f[i] * b[i] * H[i, i])
            vnew = vnew + math.log2(w[i])

        VV[iter] = vnew
        if vnew - vold <= 1e-3:
            break

    p_opt = np.square(b)
    return p_opt

# Functions for performance evaluation 
Example 30
Project: web_ctp   Author: molebot   File: test_math.py    (license) View Source Project 5 votes vote down vote up
def testLog2Exact(self):
        #fixme brython.   
        # Check that we get exact equality for log2 of powers of 2.
        actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)]
        expected = [float(n) for n in range(-1074, 1024)]
        self.assertEqual(actual, expected) 
Example 31
Project: reddit-crawler   Author: cdated   File: grapher.py    (license) View Source Project 5 votes vote down vote up
def calculate_edge_weight(subscriber_cnt):
    """ Keep weights relatively small despite large subscriber disparities """

    if subscriber_cnt == 0:
        log_cnt = 0
    else:
        log_cnt = math.log2(subscriber_cnt)

    return str(log_cnt) 
Example 32
Project: fatoptimizer   Author: vstinner   File: const_fold.py    (license) View Source Project 5 votes vote down vote up
def check_pow(config, num, exp, mod=None):
    if num == 0 and exp < 0:
        # 0 ** -1 raises a ZeroDivisionError
        return False

    if num < 0 and exp < 1.0 and exp != 0.0:
        # pow(-25, 0.5) raises a ValueError
        return False

    if mod is not None:
        # pow(a, b, m) only works if a and b are integers
        if not isinstance(num, int):
            return False
        if not isinstance(exp, int):
            return False

        if mod == 0:
            # pow(2, 1024, 0) raises a ValueError:
            # 'pow() 3rd argument cannot be 0'
            return False

    if (isinstance(num, int)
       and isinstance(exp, int)
       # don't call log2(0) (error)
       and num != 0
       # if exp < 0, the result is a float which has a fixed size
       and exp > 0):
        # bits(num ** exp) = log2(num) * exp
        if math.log2(abs(num)) * exp >= config.max_int_bits:
            # pow() result will be larger than max_constant_size.
            return False

    return True 
Example 33
Project: RTask   Author: HatBoy   File: filter.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, conn=None, capacity=1000000000, error_rate=0.00000001, key='BloomFilter'):
        self.m = math.ceil(capacity*math.log2(math.e)*math.log2(1/error_rate))      #????bit??
        self.k = math.ceil(math.log1p(2)*self.m/capacity)                           #?????hash??
        self.mem = math.ceil(self.m/8/1024/1024)                                    #?????M??
        self.blocknum = math.ceil(self.mem/512)                                     #?????512M????,value?????????ascii???????256????
        self.seeds = self.SEEDS[0:self.k]
        self.key = key
        self.N = 2**31-1
        self.redis = conn 
Example 34
Project: proteusisc   Author: diamondman   File: controller.py    (license) View Source Project 5 votes vote down vote up
def _handle_blk_SET_SPEED(self, params):
        if not self._jtag_on:
            self._blk_read_buffer.append(b'\x01\x04')
        else:
            req_speed = struct.unpack("<I", params)[0]
            self._speed = 62500*(2**min(6,max(0,math.floor(
                math.log2(req_speed/62500)
            ))))
            self._blk_read_buffer.append(b'\x05\x00'+\
                                         struct.pack("<I", self._speed)) 
Example 35
Project: pylearning   Author: amstuta   File: trees.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, max_depth=-1, min_leaf_examples=6, max_split_features="auto"):
        self.root_node = None
        self.max_depth = max_depth
        self.min_leaf_examples = min_leaf_examples

        if max_split_features in ["auto","sqrt","log2"] or \
            isinstance(max_split_features, int) or max_split_features is None:
            self.max_split_features = max_split_features
            self.considered_features = None
        else:
            raise ValueError("Argument max_split_features must be 'auto', \
                            'sqrt', 'log2', an int or None") 
Example 36
Project: pylearning   Author: amstuta   File: trees.py    (license) View Source Project 5 votes vote down vote up
def entropy(self, targets):
        """
        Returns the entropy in the given rows.
        :param targets:     1D array-like targets
        :return:            Float value of entropy
        """
        results = self.unique_counts(targets)
        ent = 0.0
        for val in results.values():
            p = float(val) / len(targets)
            ent -= p * log2(p)
        return ent 
Example 37
Project: SMT-PNR   Author: cdonovick   File: position.py    (license) View Source Project 5 votes vote down vote up
def get_coordinates(self):
        return (int(log2(self.solver.GetValue(self.x).as_int())), int(log2(self.solver.GetValue(self.y).as_int()))) 
Example 38
Project: SMT-PNR   Author: cdonovick   File: position.py    (license) View Source Project 5 votes vote down vote up
def get_coordinates(self):
        return (int(log2(self.solver.GetValue(self.x).as_int())), int(log2(self.solver.GetValue(self.y).as_int()))) 
Example 39
Project: ouroboros   Author: pybee   File: test_math.py    (license) View Source Project 5 votes vote down vote up
def testLog2Exact(self):
        # Check that we get exact equality for log2 of powers of 2.
        actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)]
        expected = [float(n) for n in range(-1074, 1024)]
        self.assertEqual(actual, expected) 
Example 40
Project: string_algorithms   Author: mhozza   File: utils.py    (license) View Source Project 5 votes vote down vote up
def greatest_pow2(n):
    return 2 ** floor(log2(n)) 
Example 41
Project: string_algorithms   Author: mhozza   File: rmq.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, array):
        self.array = array
        self.block_size = ceil(log2(len(array)) / 4)
        self.block_cnt = ceil(len(self.array) / self.block_size)
        self.block_mins = self._calculate_block_mins()
        self.processed_block_mins = self._process_block_mins()
        self.rmq_map = dict()
        self.signatures = self._compute_signatures() 
Example 42
Project: string_algorithms   Author: mhozza   File: rmq.py    (license) View Source Project 5 votes vote down vote up
def _process_block_mins(self):
        max_size = floor(log2(len(self.block_mins)))
        res = [[i for i in self.block_mins]]

        def global_argmin(*sub_blocks):
            return sub_blocks[argmin(self.array[i] for i in sub_blocks)]

        for si in range(max_size):
            t = [
                global_argmin(res[si][i], res[si][i + 2**si]) for i in range(len(self.block_mins) - 2**si)
            ] + [
                res[si][i] for i in range(len(self.block_mins) - 2**si, len(self.block_mins))
            ]
            res.append(t)
        return res 
Example 43
Project: string_algorithms   Author: mhozza   File: rmq.py    (license) View Source Project 5 votes vote down vote up
def _query_whole_blocks(self, bi, bj):
        cnt = floor(log2(bj - bi))
        sub_blocks = [
            self.processed_block_mins[cnt][bi],
            self.processed_block_mins[cnt][bj - 2 ** cnt],
        ]
        return sub_blocks[argmin(self.array[i] for i in sub_blocks)] 
Example 44
Project: scrapy-demo   Author: ParadeTo   File: bloomfilter.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, capacity=100, error_rate=0.01, conn=None, key='BloomFilter'):
        self.m = math.ceil(capacity * math.log2(math.e) * math.log2(1 / error_rate))  # ????bit??
        self.k = math.ceil(math.log1p(2) * self.m / capacity)  # ?????hash??
        self.mem = math.ceil(self.m / 8 / 1024 / 1024)  # ?????M??
        self.blocknum = math.ceil(self.mem / 512)  # ?????512M????,value?????????ascii???????256????
        self.seeds = self.SEEDS[0:self.k]
        self.key = key
        self.N = 2 ** 31 - 1
        self.redis = conn 
Example 45
Project: xentica   Author: a5kin   File: properties.py    (license) View Source Project 5 votes vote down vote up
def calc_bit_width(self):
        return int(math.log2(self.max_val)) + 1 
Example 46
Project: velox-syllabus   Author: greenfox-velox   File: fractal2.py    (license) View Source Project 5 votes vote down vote up
def rectriangle(x, y, s):
    th = s*S3/4
    cr = S3*s/12
    stepest = int(math.log2(s))+2
    triangle(x, y, s, colors[stepest%len(colors)])
    circle(x+s/2-cr, y+th-cr*2, cr*2, colors[(stepest+1)%len(colors)])
    if s >= SIZE_LIMIT:
        s /= 2
        root.after(DELAY, rectriangle, x, y, s)
        root.after(DELAY+100, rectriangle, x+s, y, s)
        root.after(DELAY+200, rectriangle, x+s/2, y+th, s)
        root.after(DELAY+300, rectriangle, x+3/4*s, y+S3/4*s, s/2) 
Example 47
Project: 100knock2017   Author: tmu-nlp   File: knock39.py    (license) View Source Project 5 votes vote down vote up
def print_zipf(data):
    print(len(data))
    
    
    plt.bar(range(len(data)),[math.log2(t[1]) for t in data])


# In[6]: 
Example 48
Project: kbe_server   Author: xiaohaoppy   File: test_math.py    (license) View Source Project 5 votes vote down vote up
def testLog2Exact(self):
        # Check that we get exact equality for log2 of powers of 2.
        actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)]
        expected = [float(n) for n in range(-1074, 1024)]
        self.assertEqual(actual, expected) 
Example 49
Project: aid   Author: cvjena   File: eval_metrics.py    (license) View Source Project 5 votes vote down vote up
def ndcg(relevant, retrieved, ignore = [], k = None):
    """ Computes the Normalized Discounted Cumulative Gain of a list of retrieval results with binary relevance levels for a particular query.
    
    relevant - Ground-Truth: Set of IDs relevant to the query.
    retrieved - Ranked list of retrieved IDs.
    ignore - Optionally, a set of IDs to be ignored, i.e., to be counted neither as true nor as false positive.
    k - Optionally, the maximum number of best-scoring results to be considered (will compute the NDCG @ k).
        If set to `None`, the NDCG of the entire list of retrieval results will be computed.
        Instead of a single number, a sorted list of values for various `k` may be given to evaluate NDCG at.
        In this case, this function will return a list of corresponding precisions as well.
    
    Returns: [email protected], given as a single number or a list, depending on the value of `k`
    """
    
    ignore = set(ignore)
    max_rank = len(set(retrieved) - ignore)
    
    ks = [k] if isinstance(k, int) or (k is None) else list(k)
    for i in range(len(ks)):
        if (ks[i] is None) or (ks[i] > max_rank):
            ks[i] = max_rank
    ks.sort()
    k_index = 0
    
    ndcgs = []
    
    rank, cgain, normalizer = 0, 0.0, 0.0
    for ret in retrieved:
        if ret not in ignore:
            rank += 1
            gain = 1.0 / math.log2(rank + 1)
            if ret in relevant:
                cgain += gain
            if rank <= len(relevant):
                normalizer += gain
            while (k_index < len(ks)) and (rank >= ks[k_index]):
                ndcgs.append(cgain / normalizer)
                k_index += 1
            if k_index >= len(ks):
                break
    return ndcgs[0] if isinstance(k, int) or (k is None) else ndcgs 
Example 50
Project: Algorithms   Author: gonewbee   File: heap_sort_image.py    (license) View Source Project 5 votes vote down vote up
def draw_heap(cls, draw, a, num, i=-1, j=-1, ck=-1, cw=-1):
        for n, v in enumerate(a):
            if n > num:
                break
            # ??????????
            heap_ny = int(math.log2(n + 1))
            # ??????????
            heap_nx_a = 2**heap_ny
            # ?????????
            nx = n + 1 - heap_nx_a
            # ?????x???????
            bx = (2**(cls.heap_hn - heap_ny) - 1) * cls.heap_xs / 2
            # ?????x?????
            xs = 2**(cls.heap_hn - heap_ny) * cls.heap_xs
            # ???x??
            x = cls.heap_bx + bx + xs * nx
            # ???y??
            y = cls.heap_by + cls.heap_ys * heap_ny
            cls.heap_xy[n] = [x, y]
            # print(x, y)
            draw.text((x, y), v, (0, 0, 0))
            if n > 0:
                k = (n - 1) // 2
                kx, ky = cls.heap_xy[k]
                draw.line((x, y, kx, ky + 10), (0, 0, 0))
        if i != -1 and j != -1:
            x1, y1 = cls.heap_xy[i]
            x2, y2 = cls.heap_xy[j]
            draw.line((x1, y1 + 10, x2, y2), (255, 0, 0), width=2)
        if ck != -1 and cw != -1:
            x1, y1 = cls.heap_xy[ck]
            x2, y2 = cls.heap_xy[cw]
            draw.line((x1, y1 + 10, x2, y2), (0, 255, 255))