Python | 資訊熵 Information Entropy

华小电發表於2024-03-09
def counter(list):
	c_dict = {}

	for i in list:
		if i in c_dict:
			c_dict[i] += 1
		else:
			c_dict[i] = 1
	return c_dict


def entropy(x):
	counts = counter(x) #每個變數出現的次數
	prob = [i/len(x) for i in counts.values()] # 每個變數發生的機率
	return -sum([i*math.log(i) for i in prob]) # 計算資訊熵


x = np.array([2,3,4,1,1,3,4,5,6,2,1,3,4,5,5,6,7,3,2,4,4,2])
print(entropy(x))

相關文章