python numpy - 高效的密集矩阵创建

发布于 2024-12-02 11:28:02 字数 1147 浏览 0 评论 0原文

我目前正在编写函数来根据运输包的输入文件生成矩阵。它是一个 1850x1850 矩阵,表示网络中的区域。这 1850 个区域还分为 10 个不同的区域组。基本上,我需要为每个组合起始区域到目标区域分配一个时间因子(数字)。

我的主要循环是:

    for i,start in enumerate(starts):
        #Create the matrix
        mat,data = creatematrix(ID)     #1850x1850

        #Read in the time factors
        lines = picklines(range(start+1,starts+21))
        lines = [lines[i]+lines[i+1] for i in range(0,len(lines),2)]
        lines = [[float(v) for v in line] for line in lines]

        #Now Lines contains the time periods for each combination of origin to destination zone

        #Generated the matrix
        for i in xrange(10):
            for j in xrange(10):
                #Assign the time factor for each O-D pair
                I,J = PythonMatrixGroups[i,j]
                data[I,J] = lines[i,j]

        #Save matrix
        mat.raw_data = data

自然大部分时间都花在双循环上。这是生成此类密集矩阵的最快方法吗?我也尝试了该方法:

    PQ = PythonMatrixGroups
    output = np.sum([coo_matrix((lines[i,j]*ones(len(PQ[i][j][0])), PQ),shape=((1850,1850))])
    mat.raw_data = output.toarray()

但它的时钟速度几乎慢了一倍

非常感谢,

I am currently writing function to generate matrices based on a input file for a transportation package. It's an 1850x1850 matrix representing the zones in a network. These 1850 zones are also partitioned into 10 different zone groups. Basically I am required to assign a Time Factor (number) to each combination origin zone to destination zone.

My main loop is:

    for i,start in enumerate(starts):
        #Create the matrix
        mat,data = creatematrix(ID)     #1850x1850

        #Read in the time factors
        lines = picklines(range(start+1,starts+21))
        lines = [lines[i]+lines[i+1] for i in range(0,len(lines),2)]
        lines = [[float(v) for v in line] for line in lines]

        #Now Lines contains the time periods for each combination of origin to destination zone

        #Generated the matrix
        for i in xrange(10):
            for j in xrange(10):
                #Assign the time factor for each O-D pair
                I,J = PythonMatrixGroups[i,j]
                data[I,J] = lines[i,j]

        #Save matrix
        mat.raw_data = data

Naturally the bulk of the time is spent in the double loop. Is this the quickest way to generated a dense matrix of this sort? I also tried the method:

    PQ = PythonMatrixGroups
    output = np.sum([coo_matrix((lines[i,j]*ones(len(PQ[i][j][0])), PQ),shape=((1850,1850))])
    mat.raw_data = output.toarray()

But it clocked almost twice as slow

Many thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2024-12-09 11:28:02

应该可以在一个循环中完成它,如下所示:

    #Generated the matrix
    for i in xrange(10):
        #Assign the time factor for each O-D pair
        I = PythonMatrixGroups[i]
        data[I] = lines[i]

但我没有测试这一点。

It should be possible to do it in one loop as this :

    #Generated the matrix
    for i in xrange(10):
        #Assign the time factor for each O-D pair
        I = PythonMatrixGroups[i]
        data[I] = lines[i]

But I did not test this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文