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读书笔记(六)正则化:文本回归的更多相关文章

  1. Machine Learning for hackers读书笔记(十二)模型比较

    library('ggplot2')df <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\12-Model_C ...

  2. Machine Learning for hackers读书笔记(七)优化:密码破译

    #凯撒密码:将每一个字母替换为字母表中下一位字母,比如a变成b. english.letters <- c('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ...

  3. Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤

    #定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...

  4. Machine Learning for hackers读书笔记_一句很重要的话

    为了培养一个机器学习领域专家那样的直觉,最好的办法就是,对你遇到的每一个机器学习问题,把所有的算法试个遍,直到有一天,你凭直觉就知道某些算法行不通.

  5. Machine Learning for hackers读书笔记(十)KNN:推荐系统

    #一,自己写KNN df<-read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\10-Recommendations\\ ...

  6. Machine Learning for hackers读书笔记(九)MDS:可视化地研究参议员相似性

    library('foreign') library('ggplot2') data.dir <- file.path('G:\\dataguru\\ML_for_Hackers\\ML_for ...

  7. Machine Learning for hackers读书笔记(八)PCA:构建股票市场指数

    library('ggplot2') prices <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\08-PC ...

  8. Machine Learning for hackers读书笔记(五)回归模型:预测网页访问量

    线性回归函数 model<-lm(Weight~Height,data=?) coef(model):得到回归直线的截距 predict(model):预测 residuals(model):残 ...

  9. Machine Learning for hackers读书笔记(四)排序:智能收件箱

    #数据集来源http://spamassassin.apache.org/publiccorpus/ #加载数据 library(tm)library(ggplot2)data.path<-'F ...

随机推荐

  1. 11gR2数据库日志报错:Fatal NI connect error 12170、

    11gR2数据库日志报错:Fatal NI connect error 12170.TNS-12535.TNS-00505 [问题点数:100分,结帖人MarkIII]             不显示 ...

  2. 查看w3wp进程占用的内存及.NET内存泄露,死锁分析--转载

    一 基础知识 在分析之前,先上一张图: 从上面可以看到,这个w3wp进程占用了376M内存,启动了54个线程. 在使用windbg查看之前,看到的进程含有 *32 字样,意思是在64位机器上已32位方 ...

  3. POJ 1742

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 27580   Accepted: 9335 Descriptio ...

  4. Activity学习(三)——跳转传值

    Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据.   Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...

  5. 15条规则解析JavaScript对象布局(__proto__、prototype、constructor)

    大家都说JavaScript的属性多,记不过来,各种结构复杂不易了解.确实JS是一门入门快提高难的语言,但是也有其他办法可以辅助记忆.下面就来讨论一下JS的一大难点-对象布局,究竟设计JS这门语言的人 ...

  6. Linux下TOmcat调试命令

    1.显示linux系统的环境变量:env命令,会显示JAVA_HOME,Catalina,CLASSPATH等系统变量 2.

  7. Haproxy均衡负载部署和配置文件详解

    HAproxy均衡负载部署和配置文件详解 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.根据官方数据,其最高极限支持10G ...

  8. 【web性能】页面呈现、重绘、回流

    在讨论页面重绘.回流之前.需要对页面的呈现流程有些了解,页面是怎么把html结合css等显示到浏览器上的,下面的流程图显示了浏览器对页面的呈现的处理流程.可能不同的浏览器略微会有些不同.但基本上都是类 ...

  9. Android AlarmManager类的应用(实现闹钟功能)

    1.AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用 ...

  10. js获取滚动条距离浏览器顶部,底部的高度,兼容ie和firefox

    做web开发经常会碰到需要获取浏览器的滚动条与顶部和底部的距离,然后做相应的处理动作.下面作者就如何通过js来获取浏览器滚动条距离浏览器顶部和底部的高度做一下分享,这个是同时兼容ie和firefox的 ...