python投票一致性指数(IVC)实现代码
毕业论文中用于计算联合国会员国间在联合国大会上的投票一致性(IVC)
- import pandas as pd
- import sqlite3
- import networkx as nx
- import time
- import numpy as np
- def gen_dict(sql):#建立投票数据字典
- votedict={}
- votedict['all']={}
- for c in cntylist:
- votedict['all'][c]={}
- votedict['all'][c]['yes']=[]
- votedict['all'][c]['noyes']=[]
- votedict['all'][c]['qq']=[]
- votedict['all'][c]['no']=[]
- votedict['all'][c]['all']=[]
- for y in [1992,2001,2010]:
- votedict[y]={}
- for c in cntylist:
- votedict[y][c]={}
- votedict[y][c]['yes']=[]
- votedict[y][c]['noyes']=[]
- votedict[y][c]['qq']=[]
- votedict[y][c]['no']=[]
- votedict[y][c]['all']=[]
- for c in cntylist:
- c1=c
- r1sql='select rcid, Country, vote, year from data where vote<4 and Country= \'%s\'' % c1 + sql
- rec1=pd.read_sql(r1sql ,conn)
- yeslist=rec1[rec1['vote']==1]['rcid'].tolist()
- noyeslist=rec1[(rec1['vote']==2)|(rec1['vote']==3)]['rcid'].tolist()
- nolist=rec1[rec1['vote']==3]['rcid'].tolist()
- qqlist=rec1[rec1['vote']==2]['rcid'].tolist()
- votedict['all'][c]['yes']=yeslist
- votedict['all'][c]['noyes']=noyeslist
- votedict['all'][c]['qq']=qqlist
- votedict['all'][c]['no']=nolist
- votedict['all'][c]['all']=rec1['rcid'].tolist()
- for y in [1992,2001,2010]:
- for c in cntylist:
- c1=c
- yearsql='and year>= %d and year< %d'%(y,y+8)
- r1sql='select rcid, Country, vote, year from data where vote<4 and Country= \'%s\'' % c1+yearsql+sql
- rec1=pd.read_sql(r1sql ,conn)
- yeslist=rec1[rec1['vote']==1]['rcid'].tolist()
- noyeslist=rec1[(rec1['vote']==2)|(rec1['vote']==3)]['rcid'].tolist()
- nolist=rec1[rec1['vote']==3]['rcid'].tolist()
- qqlist=rec1[rec1['vote']==2]['rcid'].tolist()
- votedict[y][c]['yes']=yeslist
- votedict[y][c]['noyes']=noyeslist
- votedict[y][c]['qq']=qqlist
- votedict[y][c]['no']=nolist
- votedict[y][c]['all']=rec1['rcid'].tolist()
- print(sql,y,'生成该年份数据')
- print(sql,'数据字典已经生成')
- return votedict
- def ivc_gen_new(thedict,c1,c2):#基于字典计算两国间IVC
- c1yes= thedict[c1]['yes']
- c1no= thedict[c1]['no']
- c1noyes=thedict[c1]['noyes']
- c1qq=thedict[c1]['qq']
- c2yes= thedict[c2]['yes']
- c2no=thedict[c2]['no']
- c2noyes=thedict[c2]['noyes']
- c2qq=thedict[c2]['qq']
- c1all=thedict[c1]['all']
- c2all=thedict[c2]['all']
- flist=[x for x in c1yes if x in c2yes]+[x for x in c1qq if x in c2qq]+[x for x in c1no if x in c2no]
- glist=[x for x in c1qq if x in c2yes+c2no]+[x for x in c2qq if x in c1yes+c1no]
- f=len(set(flist))
- g=len(set(glist))
- t=len(set([c for c in c1all if c in c2all]))
- try:
- ivc=(f+0.5*g)/t
- except:
- ivc=0
- return ivc
IVC方法参考
Turner J. Party and constituency: Pressures on congress[M]. Baltimore: The Johns Hopkins Press, 1952.
python投票一致性指数(IVC)实现代码的更多相关文章
- Python实现各种排序算法的代码示例总结
Python实现各种排序算法的代码示例总结 作者:Donald Knuth 字体:[增加 减小] 类型:转载 时间:2015-12-11我要评论 这篇文章主要介绍了Python实现各种排序算法的代码示 ...
- 【原创】用Python爬取LeetCode的AC代码到Github
在leetCode写了105道题高调膜科,考虑搬迁到自己的GitHub上,做成一个解题题库,面试的时候也可以秀一个 但是!但是! leetCode在线IDE的功能不要太舒服,我直接线上A了不少题,本地 ...
- Python绘制3d螺旋曲线图实例代码
Axes3D.plot(xs, ys, *args, **kwargs) 绘制2D或3D数据 参数 描述 xs, ys X轴,Y轴坐标定点 zs Z值,每一个点的值都是1 zdir 绘制2D集合时使用 ...
- Python爬虫 - 爬取百度html代码前200行
Python爬虫 - 爬取百度html代码前200行 - 改进版, 增加了对字符串的.strip()处理 源代码如下: # 改进版, 增加了 .strip()方法的使用 # coding=utf-8 ...
- 软件测试自动化…python学习到什么程度?代码好不好学!
软件测试自动化…python学习到什么程度?代码好不好学! 如下:
- python 类继承演示范例的代码
把做工程过程重要的代码片段备份一次,下面的资料是关于python 类继承演示范例的代码. # a simple example of a class inheritance # tested with ...
- 震惊!!!python可以用中文来写代码
python可以用中文来写代码 说明: 偶尔间试了一下,python可以用中文来写代码,除了一些python内置函数,和运算符不能用中文外,其它的比如新定义的类名.函数名.变量名,甚至是函数间传的参数 ...
- python爬取网页的通用代码框架
python爬取网页的通用代码框架: def getHTMLText(url):#参数code缺省值为‘utf-8’(编码方式) try: r=requests.get(url,timeout=30) ...
- Python 工匠:编写条件分支代码的技巧
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由鹅厂优文发表于云+社区专栏 作者:朱雷 | 腾讯IEG高级工程师 『Python 工匠』是什么? 我一直觉得编程某种意义是一门『手艺』 ...
- Python解决八皇后问题的代码【解读】
八皇后问题 来自于西方象棋(现在叫 国际象棋,英文chess),详情可见百度百科. 在西方象棋中,有一种叫做皇后的棋子,在棋盘上,如果双方的皇后在同一行.同一列或同一斜线上,就会互相攻击. 八皇后问题 ...
随机推荐
- FFmpeg转换直播流格式
mp4转rtsp ffmpeg -re -i 1671680590843.mp4 -vcodec copy -acodec copy -f rtsp rtsp://localhost:8554/liv ...
- ROS创建工作空间 Create your workspace
https://blog.csdn.net/baidu_38869387/article/details/119840120 http://wiki.ros.org/catkin/Tutorials/ ...
- ELKF搭建
logstash cat /etc/logstash/logstash.yml |grep -v '#'path.data: /data/logstash/datapipeline.ordered: ...
- 【RUNOOB】C语言学习之指针
资料来源: (1) runoob; (2) C语言程序设计; 注1:Runoob中对于指针的讲述比较清晰简单,摘录出来(后续补充指针与结构体,指针与函数参数); 1.指针与变量的内存位置 (1) 每个 ...
- 深入理解 epoll 原理
从网卡如何接收数据说起 CPU 如何知道接受了数据? 进程阻塞为什么不占用 CPU 资源? 工作队列 等待队列 唤醒进程 内核接收网络数据全过程 同时监视多个 socket 的方法 select 的监 ...
- python学习:sqlite3 文件型数据库
摘录:https://www.cnblogs.com/decwang/p/4565572.html SQLite 字段类型 一般数据采用的固定的静态数据类型,而SQLite采用的是动态数据类型,会根据 ...
- Spring Boot上传文件功能的开发
Spring Boot上传文件功能的开发 Spring Boot使用Servlet 3的API javax.servlet.http.Part来支持文件上传.Spring Boot在类Multipar ...
- composer 操作
composer list 显示所有命令 composer show 显示所有包信息 composer install 在 composer.json 配置中添加依赖库之后运行此命令安装 compos ...
- Jquery EasyUI dataGrid 修改默认分页大小 不起效果
pageSize 不能单独使用,必须和pageList联合使用. 如果pageSize的值不在pageList中时,会以pageList中最小的值显示,而设置的pageSize无效.
- 实践:带您用多种姿势将存储在腾讯云COS 上的视频播放
导语 随着5G时代的到来,短视频/直播行业开始流行,音视频逐渐成为信息传播中流量占比最大的部分.腾讯云对象存储(COS)作为可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务,早已不 ...