小编Ste*_*rin的帖子

接受特征密集矩阵和稀疏矩阵的函数

我正在努力将稀疏矩阵支持添加到开源数学库中,并且不希望对矩阵类型DenseSparse矩阵类型都有重复的函数。

下面的示例显示了一个add函数。一个有两个函数的工作示例,然后两次尝试都失败了。下面提供了代码示例的 Godbolt 链接。

我看过了本征文档的编写采取征类型的功能,但其使用的答案Eigen::EigenBase不工作,因为两者MatrixBaseSparseMatrixBase没有提供有特别的方法存在EigenBase

https://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html

我们使用 C++14,非常感谢您的帮助和时间!!

#include <Eigen/Core>
#include <Eigen/Sparse>
#include <iostream>

// Sparse matrix helper
using triplet_d = Eigen::Triplet<double>;
using sparse_mat_d = Eigen::SparseMatrix<double>;
std::vector<triplet_d> tripletList;

// Returns plain object
template <typename Derived>
using eigen_return_t = typename Derived::PlainObject;

// Below two are the generics that work
template <class Derived>
eigen_return_t<Derived> add(const Eigen::MatrixBase<Derived>& A) {
    return A + A;
}

template <class Derived>
eigen_return_t<Derived> add(const Eigen::SparseMatrixBase<Derived>& …
Run Code Online (Sandbox Code Playgroud)

c++ templates multiple-inheritance eigen

8
推荐指数
1
解决办法
365
查看次数

从R中的多个网页上的表格中刮取数据(足球运动员)

我正在为学校开展一个项目,我需要收集个人NCAA足球运动员的职业统计数据.每个玩家的数据都采用这种格式.

http://www.sports-reference.com/cfb/​​players/ryan-aplin-1.html

我找不到所有玩家的总和,所以我需要逐页进行并拉出每个传球得分冲击和接收等html表的底行

每个玩家都按照姓氏进行归类,并在此处输入每个字母的链接.

http://www.sports-reference.com/cfb/​​players/

例如,在这里找到姓氏为A的每个玩家.

http://www.sports-reference.com/cfb/​​players/a-index.html

这是我第一次真正进入数据抓取,所以我试着用答案找到类似的问题.我找到的最接近的答案是这个问题

我相信我可以使用非常相似的东西,我用收集的玩家的名字切换页码.但是,我不确定如何更改它以查找播放器名称而不是页码.

塞缪尔·文图拉(Samuel L. Ventura)最近也发表了关于NFL数据数据搜集的讨论,可以在这里找到.

编辑:

Ben真的很有帮助,并提供了一些很棒的代码.第一部分非常有效,但是当我尝试运行第二部分时,我遇到了这个问题.

> # unlist into a single character vector
> links <- unlist(links)
> # Go to each URL in the list and scrape all the data from the tables
> # this will take some time... don't interrupt it! 
> all_tables <- lapply(links, readHTMLTable, stringsAsFactors = FALSE)
Error in UseMethod("xmlNamespaceDefinitions") : 
 no applicable method for 'xmlNamespaceDefinitions' applied to an object of class "NULL" …
Run Code Online (Sandbox Code Playgroud)

html xml r web-scraping rcurl

7
推荐指数
1
解决办法
7503
查看次数

执行矩阵的成对比较

我有一个n个变量的矩阵,我想创建一个新的矩阵,它是每个向量的成对差异,但不是自身.这是一个数据示例.

    Transportation.services Recreational.goods.and.vehicles Recreation.services Other.services
         2.958003                     -0.25983789            5.526694           2.8912009
         2.857370                     -0.03425164            5.312857           2.9698044
         2.352275                      0.30536569            4.596742           2.9190123
         2.093233                      0.65920773            4.192716           3.2567390
         1.991406                      0.92246531            3.963058           3.6298314
         2.065791                      1.06120930            3.692287           3.4422340
Run Code Online (Sandbox Code Playgroud)

我尝试在下面运行for循环,但我知道R循环非常慢.

Difference.Matrix<- function(data){
 n<-2
new.cols="New Columns"
list = list()
for (i in 1:ncol(data)){

    for (j in n:ncol(data)){

        name <- paste("diff",i,j,data[,i],data[,j],sep=".")
        new<- data[,i]-data[,j]
        list[[new.cols]]<-c(name)
        data<-merge(data,new)
        }
    n= n+1
    }
results<-list(data=data)
return(results)
}
Run Code Online (Sandbox Code Playgroud)

正如我之前所说,代码运行速度很慢,甚至还没有完成一次运行.我也为初学者级编码道歉.另外我知道这段代码会在矩阵上留下原始数据,但我可以稍后删除它.

我是否可以对此数据使用apply函数或foreach?

foreach for-loop r matrix apply

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