Machine Learning for hackers读书笔记(六)正则化:文本回归
data<-'F:\\learning\\ML_for_Hackers\\ML_for_Hackers-master\\06-Regularization\\data\\'
ranks <- read.csv(file.path(data, 'oreilly.csv'),stringsAsFactors = FALSE)
library('tm')
documents <- data.frame(Text = ranks$Long.Desc.)
row.names(documents) <- 1:nrow(documents)
#获得语料库
corpus <- Corpus(DataframeSource(documents))
#R2版本用corpus <- tm_map(corpus, tolower)
corpus <- tm_map(corpus, content_transformer(tolower))
#R2版本用corpus <- tm_map(corpus, stripWhitespace)
corpus <- tm_map(corpus, content_transformer(stripWhitespace))
#去除英文停用词
corpus <- tm_map(corpus, removeWords, stopwords('english'))
#得到词项文档矩阵
dtm <- DocumentTermMatrix(corpus)
x <- as.matrix(dtm)
y <- rev(1:100) #反转1..100,结果是100..1
set.seed(1)
library('glmnet')
performance <- data.frame()
for (lambda in c(0.1, 0.25, 0.5, 1, 2, 5))
{
for (i in 1:50)
{
indices <- sample(1:100, 80)
training.x <- x[indices, ]
training.y <- y[indices]
test.x <- x[-indices, ]
test.y <- y[-indices]
glm.fit <- glmnet(training.x, training.y)
predicted.y <- predict(glm.fit, test.x, s = lambda)
rmse <- sqrt(mean((predicted.y - test.y) ^ 2))
performance <- rbind(performance,data.frame(Lambda = lambda,Iteration = i,RMSE = rmse))
}
}
ggplot(performance, aes(x = Lambda, y = RMSE)) +stat_summary(fun.data = 'mean_cl_boot', geom = 'errorbar') +
stat_summary(fun.data = 'mean_cl_boot', geom = 'point')
#从图上看,失败

#失败了作分类,判断一本书能不能进前50
y <- rep(c(1, 0), each = 50)
#作逻辑回归
regularized.fit <- glmnet(x, y, family = 'binomial')
#预测一下
predict(regularized.fit, newx = x, s = 0.001)
#出来的结果并不是分类,而是一堆数值,因此改一下
ifelse(predict(regularized.fit, newx = x, s = 0.001) > 0, 1, 0)
#第二种方法,把预测结果转成概率值
library('boot')
inv.logit(predict(regularized.fit, newx = x, s = 0.001))
#看效果
set.seed(1)
performance <- data.frame()
for (i in 1:250)
{
indices <- sample(1:100, 80)
training.x <- x[indices, ]
training.y <- y[indices]
test.x <- x[-indices, ]
test.y <- y[-indices]
for (lambda in c(0.0001, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.5, 0.1))
{
glm.fit <- glmnet(training.x, training.y, family = 'binomial')
predicted.y <- ifelse(predict(glm.fit, test.x, s = lambda) > 0, 1, 0)
error.rate <- mean(predicted.y != test.y)
performance <- rbind(performance,data.frame(Lambda = lambda,Iteration = i,ErrorRate = error.rate))
}
}
#画个图
ggplot(performance, aes(x = Lambda, y = ErrorRate)) +
stat_summary(fun.data = 'mean_cl_boot', geom = 'errorbar') +
stat_summary(fun.data = 'mean_cl_boot', geom = 'point') +scale_x_log10()

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读书笔记(七)优化:密码破译
#凯撒密码:将每一个字母替换为字母表中下一位字母,比如a变成b. english.letters <- c('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ...
- Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤
#定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...
- Machine Learning for hackers读书笔记_一句很重要的话
为了培养一个机器学习领域专家那样的直觉,最好的办法就是,对你遇到的每一个机器学习问题,把所有的算法试个遍,直到有一天,你凭直觉就知道某些算法行不通.
- Machine Learning for hackers读书笔记(十)KNN:推荐系统
#一,自己写KNN df<-read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\10-Recommendations\\ ...
- Machine Learning for hackers读书笔记(九)MDS:可视化地研究参议员相似性
library('foreign') library('ggplot2') data.dir <- file.path('G:\\dataguru\\ML_for_Hackers\\ML_for ...
- 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 ...
随机推荐
- 用于主题检测的临时日志(b42e98ba-eb4f-4099-a54c-7aee3f29c3dd - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
这是一个未删除的临时日志.请手动删除它.(184c28c9-c88e-48fe-9713-6891e2d15044 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
- 小圣求职记B:总集篇
1. 搜狐sohu 搜狐在正式招聘前邀请了部分应聘者到武汉研发中心开座谈会(因此简历尽量早投,机会多些),有研发的也有产品的,40人左右,座谈会期间介绍了搜狐汽车.北京研发中心.武汉研发中心和搜狐媒体 ...
- cf div2 237 D
D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- iOSpush过后返回多级界面
有导航控制器push过后pop可以反回上一个界面,然而我们需要返回多级界面有下面两种方法 调用API - (NSArray *)popToViewController:(UIViewControlle ...
- hdu 4759 Poker Shuffle 二进制
思路:主要是二进制的运用. 为了方便从0开始,首先看下右移一下,高位异或1的规律:(可以从右往左一列一列看) 000(0) -> 100(4) -> 110(6) -> 111(7) ...
- UVA 10780 Again Prime? No Time. 分解质因子
The problem statement is very easy. Given a number n you have to determine the largest power of m,no ...
- Task 使用 Task以及Task.Factory都是在.Net 4引用的。Task跟Thread很类似,通过下面例子可以看到。
static public void ThreadMain() { Thread t1 = new Thread(TaskWorker); t1.Start(3); } static public v ...
- Core Animation2-CABasicAnimation
CABasicAnimation是CAPropertyAnimation的子类,使用它可以实现一些基本的动画效果,它可以让CALayer的某个属性从某个值渐变到另一个值.下面就用CABasicAnim ...
- 纯CSS实现的右侧底部简洁悬浮效果
我们见过很多页面右侧浮动效果,最早有QQ联系面板,对联广告等,大多数都是基于Javascript实现的动态效果,今天我给大家分享一个只需要CSS结合DIV实现的右侧浮动效果. HTML 我们希望悬浮效 ...
- Qt之界面出现、消失动画效果(简单好用)
在学习Qt的这2.3个月里,对Qt越发感兴趣,从刚开始的盲目.无所适从到现在的学习.研究.熟练.掌握的过程中,我学到了很多东西,也学会了如何通过自学让自己更加成熟.强大起来,如何更有效地提高自己学习. ...