Machine Learning for hackers读书笔记(九)MDS:可视化地研究参议员相似性
library('foreign')
library('ggplot2')
data.dir <- file.path('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\09-MDS\\data\\roll_call')
data.files <- list.files(data.dir)
rollcall.data <- lapply(data.files,function(f) { read.dta(file.path(data.dir, f), convert.factors = FALSE) })
#看一下数据情况,103行,647列
#每一行对应一个议员,包括个人信息及投票结果
dim(rollcall.data[[1]])
rollcall.simplified <- function(df)
{
#99的很少投票,干脆删掉
no.pres <- subset(df, state < 99)
#10列后才是投票数据
for(i in 10:ncol(no.pres))
{
#有10种投票类型,分为三组,赞成全放一起,反对全放一起,无效全放一起,>6的是无效,1~3是赞成,4~6是反对票
no.pres[,i] <- ifelse(no.pres[,i] > 6, 0, no.pres[,i])
no.pres[,i] <- ifelse(no.pres[,i] > 0 & no.pres[,i] < 4, 1, no.pres[,i])
no.pres[,i] <- ifelse(no.pres[,i] > 1, -1, no.pres[,i])
}
return(as.matrix(no.pres[,10:ncol(no.pres)]))
}
rollcall.simple <- lapply(rollcall.data, rollcall.simplified)
#来一个矩离矩阵
rollcall.dist <- lapply(rollcall.simple, function(m) dist(m %*% t(m)))
rollcall.mds <- lapply(rollcall.dist,function(d) as.data.frame((cmdscale(d, k = 2)) * -1))
congresses <- 101:111
for(i in 1:length(rollcall.mds))
{
names(rollcall.mds[[i]]) <- c("x", "y")
congress <- subset(rollcall.data[[i]], state < 99)
congress.names <- sapply(as.character(congress$name),function(n) strsplit(n, "[, ]")[[1]][1])
rollcall.mds[[i]] <- transform(rollcall.mds[[i]], name = congress.names,party = as.factor(congress$party),congress = congresses[i])
}
cong.110 <- rollcall.mds[[9]]
base.110 <- ggplot(cong.110, aes(x = x, y = y)) + scale_size(range = c(2,2), guide = 'none') + scale_alpha(guide = 'none') + theme_bw() +
theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), panel.grid.major = element_blank()) +
ggtitle("Roll Call Vote MDS Clustering for 110th U.S. Senate") + xlab("") + ylab("") + scale_shape(name = "Party", breaks = c("100", "200", "328"),
labels = c("Dem.", "Rep.", "Ind."), solid = FALSE) + scale_color_manual(name = "Party", values = c("100" = "black","200" = "dimgray","328"="grey"),
breaks = c("100", "200", "328"), labels = c("Dem.", "Rep.", "Ind."))
print(base.110 + geom_point(aes(shape = party, alpha = 0.75, size = 2)))
print(base.110 + geom_text(aes(color = party, alpha = 0.75, label = cong.110$name, size = 2)))

all.mds <- do.call(rbind, rollcall.mds)
all.plot <- ggplot(all.mds, aes(x = x, y = y)) +
geom_point(aes(shape = party, alpha = 0.75, size = 2)) +
scale_size(range = c(2, 2), guide = 'none') +
scale_alpha(guide = 'none') +
theme_bw() +
theme(axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
panel.grid.major = element_blank()) +
ggtitle("Roll Call Vote MDS Clustering for U.S. Senate (101st - 111th Congress)") +
xlab("") +
ylab("") +
scale_shape(name = "Party",
breaks = c("100", "200", "328"),
labels = c("Dem.", "Rep.", "Ind."),
solid = FALSE) +
facet_wrap(~ congress)
print(all.plot)

Machine Learning for hackers读书笔记(九)MDS:可视化地研究参议员相似性的更多相关文章
- Machine Learning for hackers读书笔记(七)优化:密码破译
#凯撒密码:将每一个字母替换为字母表中下一位字母,比如a变成b. english.letters <- c('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ...
- Machine Learning for hackers读书笔记(六)正则化:文本回归
data<-'F:\\learning\\ML_for_Hackers\\ML_for_Hackers-master\\06-Regularization\\data\\' ranks < ...
- Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤
#定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...
- Machine Learning for hackers读书笔记_一句很重要的话
为了培养一个机器学习领域专家那样的直觉,最好的办法就是,对你遇到的每一个机器学习问题,把所有的算法试个遍,直到有一天,你凭直觉就知道某些算法行不通.
- Machine Learning for hackers读书笔记(十二)模型比较
library('ggplot2')df <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\12-Model_C ...
- Machine Learning for hackers读书笔记(十)KNN:推荐系统
#一,自己写KNN df<-read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\10-Recommendations\\ ...
- Machine Learning for hackers读书笔记(八)PCA:构建股票市场指数
library('ggplot2') prices <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\08-PC ...
- Machine Learning for hackers读书笔记(五)回归模型:预测网页访问量
线性回归函数 model<-lm(Weight~Height,data=?) coef(model):得到回归直线的截距 predict(model):预测 residuals(model):残 ...
- Machine Learning for hackers读书笔记(四)排序:智能收件箱
#数据集来源http://spamassassin.apache.org/publiccorpus/ #加载数据 library(tm)library(ggplot2)data.path<-'F ...
随机推荐
- Sqli-labs less 26
Less-26 TIPS:本关可能有的朋友在windows下无法使用一些特殊的字符代替空格,此处是因为apache的解析的问题,这里请更换到linux平台下. 本关结合25关,将空格,or,and,/ ...
- 【译】Python中如何创建mock?
原文地址:http://engineroom.trackmaven.com/blog/making-a-mockery-of-python/ 今天我们来谈论下mock的使用.当然,请不要误会,这里的m ...
- UVA 562 Dividing coins (01背包)
题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum, A,B其中必有一人获得的钱小于等于sum/2 ...
- GCD的简单封装
扩展: dispatch_block_t :无参数block,使用起来很简单 下载链接:http://pan.baidu.com/s/1bndN6Yb ]; } //定时器 - (voi ...
- VMware 使用
1.客户操作系统被禁用: BIOS中开启VT(Virtual Technology)
- <?php $sql = <<<EOF 。。。。EOF;?>这种写法是什么意思
php里$sql = <<<EOF //有这样的语法??????//sql语句EOF;运行mysql_query($sql)?>这是什么语法?变量声明可以这样的结构?请解答,谢 ...
- CodeIgniter 错误: In order to use the Session class you are required to set an encryption key
CodeIgniter SESSION 第一次用 session 遇到这个错误 , 说是要加一个密钥才可以使用,加就加吧, 打开 config.php 找到以下代码 /*|------------- ...
- hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- mysql之视图
视图 视图是虚拟的表.与包含数据的表不一样,视图只包含使用时动态检索数据的查询. 理解视图最好的办法就是来看一下例子: SELECT cust_name , cust_contact FRO ...
- Dom新find
1.HTML标签和属性是不区分大小写的,但JS是区分大小写的:所以(1)HTML专有的接口的属性应该以小写字母开头,如果属性名由多个单词构成,第二个及接下来的每个单词的首字母都要大写.(2)有些HTM ...