小编Aug*_*bas的帖子

如何使用R和Rcpp删除NumericVector中的元素以进行递归

我试图了解更多关于如何使用Rcpp包的信息.所以我开始使用Rcpp测试基本的排序算法.我在这里开始了Hadley Wickham教程.

我以这种方式递归地成功实现了插入排序:

library(Rcpp)

vetor<-sample(100)
vetor
cppFunction("
    NumericVector insertionsortRC(NumericVector vetor, int n) {
        double aux;
        int i;

        if(n>1) {
            insertionsortRC(vetor,n-1);
            aux=vetor[n-1];
            i=n-1;
            while(vetor[i-1]>aux && i>=0 ) {
                vetor[i]=vetor[i-1];
                i--;
                }
            vetor[i]=aux;
            }

        return vetor;
        }
    ")
Run Code Online (Sandbox Code Playgroud)

但函数要求2个参数,然后我尝试这样:

cppFunction("
    NumericVector insertionsortRC(NumericVector vetor) {
        int n = vetor.size();

        double aux;
        int i;

        if(n>1) {
            vetor.erase(n-1);
            insertionsortRC(vetor);
            aux=vetor[n-1];
            i=n-1;
            while(vetor[i-1]>aux && i>=0 ) {
                vetor[i]=vetor[i-1];
                i--;
                }
            vetor[i]=aux;
            }

        return vetor;
        }
    ")
Run Code Online (Sandbox Code Playgroud)

我认为擦除在这里不是一个好主意,似乎我从内存中擦除了元素,并且在递归调用之后无法恢复它.我还想到问题可能在于vetor.erase(n-1); line,试过vetor.erase(n); 它编译了,但根本没用.

使用vetor.erase(n); 我在R中遇到以下错误:

insertionsortRC(vetor) …

c++ recursion r insertion-sort rcpp

5
推荐指数
1
解决办法
1861
查看次数

返回 inf 的 Pandas DataFrame 列的 mean():我该如何解决这个问题?

我正在尝试实现一些机器学习算法,但在将数据放在一起时遇到了一些困难。

在下面的示例中,我从 UCI 加载示例数据集,删除缺少数据的行(感谢上一个问题的帮助),现在我想尝试对数据进行标准化。

对于许多数据集,我只是使用了:

valores = (valores - valores.mean()) / (valores.std())
Run Code Online (Sandbox Code Playgroud)

但是对于这个特定的数据集,上面的方法不起作用。问题是 mean 函数正在返回inf,可能是由于精度问题。请参阅下面的示例:

bcw = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data', header=None)

for col in bcw.columns:
    if bcw[col].dtype != 'int64':
        print "Removendo possivel '?' na coluna %s..." % col
        bcw = bcw[bcw[col] != '?']

valores = bcw.iloc[:,1:10]
#mean return inf
print  valores.iloc[:,5].mean()
Run Code Online (Sandbox Code Playgroud)

我的问题是如何处理这个问题。看来我需要更改此列的类型,但我不知道该怎么做。

python precision numpy pandas

4
推荐指数
1
解决办法
1万
查看次数

ggplot2中两个美学和geom_abline的传说

我正在尝试为下面的情节准备一个图例,点缀后跟A和虚线后跟B.它没有正确显示.由于我有2种美学,它有时只显示一种线型,有时甚至没有.我用scale_linetype_manual()guides(),但它无法产生预期的效果.但

我创建的代码,我删除了显示我尝试制作图例的代码.

exemplo<-data.frame(Location = c("Tiburon Peninsula", "San Francisco","Santa Barbara area", "Santa Monica Mountains", "Marin County", 
                             "Santa Cruz Mountains", "Monterey County", "San Diego County","California Coast"),
                Area = c(5.9, 45, 110, 320, 529, 1386, 3324,4260, 24520),
                Species = c(370L, 640L, 680L, 640L, 1060L, 1200L,1400L, 1450L, 2525L))
modelo<-lm(log(Species,10)~log(Area,10),data=exemplo)

modelo_nls<-nls(Species~a*Area^z,start=list(a=1,z=0.25),data=exemplo)
linha<-data.frame(x=log10(seq(1,25000,100)),y=log10(predict(modelo_nls,newdata=data.frame(Area=seq(1,25000,100)))))
ggplot(data=exemplo,aes(x=log10(Area),y=log10(Species)) )  + geom_point() +
geom_abline(intercept = modelo$coefficients[1], slope = modelo$coefficients[2],linetype="dotted") +
geom_line(data=linha,aes(x=x,y=y),linetype="dashed")+        
xlab(label="Área(ha)") + ylab(label="Número de espécies")
Run Code Online (Sandbox Code Playgroud)

r ggplot2

2
推荐指数
1
解决办法
93
查看次数

标签 统计

r ×2

c++ ×1

ggplot2 ×1

insertion-sort ×1

numpy ×1

pandas ×1

precision ×1

python ×1

rcpp ×1

recursion ×1