python实现信息熵的计算代码
python实现信息熵的计算代码
1、什么是信息熵?
1948年香农提出了信息熵(Entropy)的概念。
信息理论:
1、从信息的完整性上进行的描述:
当系统的有序状态一致时,数据越集中的地方熵值越小,数据越分散的地方熵值越大。
2、从信息的有序性上进行的描述:
当数据量一致时,系统越有序,熵值越低;系统越混乱或者分散,熵值越高。
“信息熵”(informationentropy)是度量样本集合纯度最常用的一种指标。
二、python实现信息熵的计算代码
1、导入库
importnumpyasnp
importpandasaspd
2、准备数据
data=pd.DataFrame(
{'学历':['专科','专科','专科','专科','专科','本科','本科','本科','本科','本科',
'研究生','研究生','研究生','研究生','研究生'],
'婚否':['否','否','是','是','否','否','否','是','否','否','否','否','是','是','否'],
'是否有车':['否','否','否','是','否','否','否','是','是','是','是','是','否','否',
'否'],
'收入水平':['中','高','高','中','中','中','高','高','很高','很高','很高','高','高',
'很高','中'],
'类别':['否','否','是','是','否','否','否','是','是','是','是','是','是','是','否']})
3、定义信息熵函数
#定义计算信息熵的函数:计算Infor(D)
definfor(data):
a=pd.value_counts(data)/len(data)
returnsum(np.log2(a)*a*(-1))
4、数据测试
#print(infor(data["学历"]))#测试结果为:1.584962500721156
以上就是python实现信息熵的计算代码,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python中的filter函数功能是什么?
python中的filter函数功能是什么?在python中,面对众多的数据,我们要过滤筛选出我们需要的数据。python中的filter函数就是起到了过滤筛选的作...详情>>
2023-11-10 20:37:27
pythontime模块是什么
pythontime模块是什么在python中使用时间,就免不了和time模块打交道,另外两个模块这个暂时先不做介绍。做time模块的使用上,我们可以用它来对...详情>>
2023-11-10 15:53:16
python是什么编程语言
python是什么编程语言1、说明是一种面向对象、解释型计算机程序设计语言,由GuidovanRossum于1989年底发明,第一个公开发行版发行于1991年。Pyt...详情>>
2023-11-10 15:21:05
python异常处理的两种技巧
python异常处理的两种技巧1、传递异常有时我们会在捕捉到一个异常后重新引发它(传递异常),实现起来很简单,使用不带参数的raise语句即可。deff...详情>>
2023-11-10 14:49:39热门推荐
python中的filter函数功能是什么?
沸python delattr函数如何使用?
热python中pdb模块怎么用?
热Python如何截图保存?
新python中缺少module怎么办?
python strftime和strptime的不同分析
python time.strptime的格式化
python中@contextmanager是什么?
python对象的三要素是什么
pythonGIL在Python多线程的应用
python如何对多个CSV文件进行读取
pythonif嵌套命令如何理解?
python对列表进行永久性或临时排序的方法
python生成器调用方法引发异常
技术干货






