基于r语言的文本挖掘怎么进行特征选择

Python018

基于r语言的文本挖掘怎么进行特征选择,第1张

R语言特征:

1. type.convert()函数主要用在read.table()函数中,返回向量和因子类型,当输入为double型时会丢失精度。

>type.convert(c('abc','bcd')) # 返回因子类型

[1] abc bcd

Levels: abc bcd

>type.convert(c(as.double(1.12121221111),'1.121')) # double型丢失精度

[1] 1.121212 1.121000

2. 如果一个文件包含有小数位的数据,通过read.table()函数读取时,会指定为numeric类型。

新建一个文件num.csv包括小数

1,2,1.11

2.1,3,4.5

用read.table读取文件,并查看列的类型。

>num<-read.table(file="num.csv",sep=",") # 读文件

>num

V1 V2 V3

1 1.0 2 1.11

2 2.1 3 4.50

>class(num)

[1] "data.frame"

>class(num$V1) # 查看列的类型为numeric

[1] "numeric"

3. tools包用Rdiff()函数的参数useDiff为FALSE时,与POSIX系统的diff -b命令类似。

新建文件num2.csv

3,2,1.11

2.1,3,4.5

用Rdiff()比较两个文件num.csv和num2.csv。

>Rdiff('num.csv','num2.csv',useDiff = FALSE)

1c1

<1,2,1.11

---

>3,2,1.11

[1] 1

4. 新函数anyNA(),结果与 any(is.na(.))一致,性能更好。

>is.na(c(1, NA))

[1] FALSE TRUE

>any(is.na(c(1, NA)))

[1] TRUE

>anyNA(c(1, NA))

[1] TRUE

5. arrayInd()和which()函数增加useNames参数,用于列名的匹配。我在测试过程,不太理解这个参数的意义。

>which

function (x, arr.ind = FALSE, useNames = TRUE)

6. is.unsorted()函数支持处理原始数据的向量。

>is.unsorted(1:10) # 排序的向量

[1] FALSE

>is.unsorted(sample(1:10)) # 无序的向量

[1] TRUE

7. 用于处理table的as.data.frame()函数和as.data.frame.table()函数,支持向provideDimnames(sep,base)函数传参数。我在测试过程中,也不理解具体是什么更新。

8. uniroot()函数增加新的可选参数extendInt,允许自动扩展取值范围,并增加返回对象参数init.it。

>f1 <- function(x) (121 - x^2)/(x^2+1) # 函数f1

>f2 <- function(x) exp(-x)*(x - 12) # 函数f2

>try(uniroot(f1, c(0,10))) # 在(0,10)的区间求f1函数的根

Error in uniroot(f1, c(0, 10)) :

f() values at end points not of opposite sign

>try(uniroot(f2, c(0, 2))) # 在(0,2)的区间求f2函数的根

Error in uniroot(f2, c(0, 2)) :

f() values at end points not of opposite sign

>str(uniroot(f1, c(0,10),extendInt="yes")) # 通过extendInt参数扩大取值搜索范围

List of 5

$ root : num 11

$ f.root : num -3.63e-06

$ iter : int 12

$ init.it : int 4

$ estim.prec: num 6.1e-05

>str(uniroot(f2, c(0,2), extendInt="yes")) # 通过extendInt参数扩大取值搜索范围

List of 5

$ root : num 12

$ f.root : num 4.18e-11

$ iter : int 23

$ init.it : int 9

$ estim.prec: num 6.1e-05

9. switch(f,)函数,当参数f是因子类型时,会出警告提示,需要转换字符串参数。

>switch(ff[1], A = "I am A", B="Bb..", C=" is C")# ->"A" # 警告提示

[1] "I am A"

Warning message:

In switch(ff[1], A = "I am A", B = "Bb..", C = " is C") :

EXPR is a "factor", treated as integer.

Consider using 'switch(as.character( * ), ...)' instead.

>switch(as.character(ff[1]), A = "I am A", B="Bb..", C=" is C") # 转型为字符串处理

[1] " is C"

10. 解析器已经更新,使用更少的内存。

首先R是一种专业性很强的统计语言,如果想学得快一些的话,基本的统计学知识要懂,不然很多东西会掌握的比较慢。

掌握基本语法和操作,推荐国内的已经翻译的比如《R语言实战》《R语言编程艺术》,这个过程中最好结合一些小例子来做一些分析的东西。其他还有《R语言实例》《R语言核心技术手册》也都是很好的书!如果需要可视化的话,强烈不推荐学习R本身的作图系统,实在是太不友好了.....还是用ggplot2吧。

掌握了上面的,就可以深入一些了,如果是做数据分析和可视化,推荐《ggplot2:数据分析与图形艺术》,这个才是作图的神器啊.....如果是空间分析相关的,推荐《Applied Spatial Data Analysis with R》,这个如果可以的话看英文版,而且要有地学的一些知识背景,中文版翻译的太次了,尽量不要看。数据挖掘机器学习之类的,可以看看比如《数据挖掘与R语言》、《机器学习——实用案例解析》,不过我觉得这几本书没上面的那几本好,但是可以大概看看是咋回事,最好还是看看专门的相关书籍,熟悉各种算法和流程,到时候搜索R的package,照着文档和例子搞定,不是特别难。

最后,强烈推荐统计之都、R-bloggers,统计之都以及谢益辉、肖凯、刘思喆等人的博客(自行Google以及到上面的网站找链接),订阅一下,会很有帮助,RStudio是个很棒的IDE,用起来很爽,功能很强大。

总之,你可以从《R语言实战》开始出发吧!

如何使用R语言画出漂亮的图,颜色很重要,既要协调,又有有一定的分辨力。

可以拿到任意多个颜色,当然颜色越多,分辨力越低。

barplot(rep(1,8), col=rainbow(8),border=NA)

rainbow(8)

[1] "#FF0000FF" "#FFBF00FF" "#80FF00FF" "#00FF40FF" "#00FFFFFF" "#0040FFFF"

[7] "#8000FFFF" "#FF00BFFF"

barplot(rep(1,20), col=rainbow(20),border=NA) #分辨力降低

par(mfrow=c(4,1), mar=c(0,2,2,0) )

n=10

#heat.colors()从红色渐变到黄色,再变到白色

barplot(rep(1,n), col= heat.colors(n), border=NA, main="heat.colors") 

#terrain.colors() 从绿色渐变到黄色,再到棕色,最后到白色 

barplot(rep(1,n),col=terrain.colors(n), border=NA, main="terrain.colors")

#topo.colors() 从蓝色渐变到青色,再到黄色,最后到棕色 

barplot(rep(1,n),col=topo.colors(n), border=NA, main="topo.colors")

#cm.colors() 从青色渐变到白色,再到粉红色

barplot(rep(1,n),col=cm.colors(n), border=NA, main="cm.colors")

(1)

library(RColorBrewer)

display.brewer.all() #显示全部颜色集合

# 挑选某一个集合

#barplot(rep(1,8),col=brewer.pal(8,"Dark2")[1:8]) #基础语法

myColors=brewer.pal(8,"Dark2")[1:8] #Dark2主题有8种颜色

par(mfrow=c(4,1), mar=c(0,2,2,0) )

barplot(rep(1,8),col= myColors, main="Dark2"  )

#

n=15 #nrow(df)

barplot(rep(1, n ),col= colorRampPalette(colors = myColors)( n ),main="Default:linear") #则由8种生成15种颜色

barplot(rep(1, n ),col= colorRampPalette(colors = myColors, interpolate ="linear")( n ),main="linear" ) #插值方式

barplot(rep(1, n ),col= colorRampPalette(colors = myColors, interpolate ="spline")( n ),main="spline") #插值方式

colSet2 # 获取颜色16进制表示

# [1] "#1B9E77" "#D95F02" "#7570B3" "#E7298A" "#66A61E" "#E6AB02" "#A6761D" "#666666"

解释:

n=5barplot(rep(1,n), col= colorRampPalette (colors = c('red', 'white'))( n ))

colorRampPalette 函数可以混合任意两种及更多颜色,通过插值,生成更多色彩。

(2) 目测其他几个预制颜色集合

n=8barplot(rep(1,n),col= brewer.pal(n,"Set2")[1:n] ) # set2 共8种颜色

ref:

biomooc.com