Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从SQL高效地读取Python稀疏矩阵_Python_Mysql_Scipy_Sparse Matrix - Fatal编程技术网

从SQL高效地读取Python稀疏矩阵

从SQL高效地读取Python稀疏矩阵,python,mysql,scipy,sparse-matrix,Python,Mysql,Scipy,Sparse Matrix,我在MySQL中有一个包含三列的表:行索引、列索引和值,我想将它们读入一个scipy csr_矩阵。我使用Python MySQL连接器。共有112500个非零元素 尝试1: A = csr_matrix((N_rows, N_cols), dtype=float) show = 'SELECT * FROM my_table' cursor.execute(show) for (row, col, value) in cursor: A[row, col] = value 这太慢了

我在MySQL中有一个包含三列的表:行索引、列索引和值,我想将它们读入一个scipy csr_矩阵。我使用Python MySQL连接器。共有112500个非零元素

尝试1:

A = csr_matrix((N_rows, N_cols), dtype=float)
show = 'SELECT * FROM my_table'
cursor.execute(show)
for (row, col, value) in cursor:
     A[row, col] = value
这太慢了,我不得不在60秒后停止。它提到了效率警告,并建议使用lil矩阵

尝试2:

A = lil_matrix((N_rows, N_cols), dtype=float)
show = 'SELECT * FROM my_table'
cursor.execute(show)
for (row, col, value) in cursor:
     A[row, col] = value
A = csr_matrix(A)
这需要6.4秒(平均3秒)。这是最好的,还是有一种更快的方法,我可以不经过循环就创建csr_矩阵?如果执行cursor.fetchall(),数据如下所示:

[(row_0, col_0, value_0), (row_1, col_1, value_1), ...]

这不能用于csr_矩阵构造函数。

游标.fetchall()返回的数据几乎是相同的格式。 你能行

import numpy as np
from scipy.sparse import coo_matrix

data = cursor.fetchall()
#data = [(1, 2, 1.2), (3, 4, 7.1)]

arr = np.array(data, dtype=[('row', int), ('col', int), ('value', float)])
spmat = coo_matrix((arr['value'], (arr['row'], arr['col'])))
除了
np.array(cursor.fetchall(),…)

arr = np.fromiter(cursor, dtype=[('row', int), ('col', int), ('value', float)])

将数据从数据库直接加载到Numpy数组。

cursor.fetchall()返回的数据的格式几乎相同。 你能行

import numpy as np
from scipy.sparse import coo_matrix

data = cursor.fetchall()
#data = [(1, 2, 1.2), (3, 4, 7.1)]

arr = np.array(data, dtype=[('row', int), ('col', int), ('value', float)])
spmat = coo_matrix((arr['value'], (arr['row'], arr['col'])))
除了
np.array(cursor.fetchall(),…)

arr = np.fromiter(cursor, dtype=[('row', int), ('col', int), ('value', float)])

将数据从数据库直接加载到Numpy数组。

大多数稀疏格式接受
(值,(行,列))
样式的输入。实现可能首先生成一个
coo
,然后将其转换为
csr
(或其他任何形式)。感谢coo的提示。使用data=fetchall()和np.array(data,…)的解决方案将读取时间恢复到5.5秒。但是,使用np.fromiter的第二个解决方案将读取时间再次提高到6.9秒(所有平均值均为6倍)。然后,速度可能会受到Python DB接口从DB中提取数据的速度的限制,甚至受到DB引擎本身速度的限制。如果矩阵存储在DB中的格式可以更改(例如,将其存储为原始二进制数据),则可能会加快速度。不幸的是,现在我已从NumPy 1.8升级到NumPy 1.9,代码不再工作。使用
arr=np.array(…)
执行该行时,现在会出现以下错误消息:
ValueError:元组的大小必须与字段数匹配。
有人知道如何解决此问题吗?显然,数据应该是列表,而不是元组
data=list(cursor.fetchall())
就可以了。大多数稀疏格式接受
(value,(row,col))
样式的输入。实现可能首先生成一个
coo
,然后将其转换为
csr
(或其他任何形式)。感谢coo的提示。使用data=fetchall()和np.array(data,…)的解决方案将读取时间恢复到5.5秒。但是,使用np.fromiter的第二个解决方案将读取时间再次提高到6.9秒(所有平均值均为6倍)。然后,速度可能会受到Python DB接口从DB中提取数据的速度的限制,甚至受到DB引擎本身速度的限制。如果矩阵存储在DB中的格式可以更改(例如,将其存储为原始二进制数据),则可能会加快速度。不幸的是,现在我已从NumPy 1.8升级到NumPy 1.9,代码不再工作。使用
arr=np.array(…)
执行该行时,现在会出现以下错误消息:
ValueError:元组的大小必须与字段数匹配。
有人知道如何解决此问题吗?显然,数据应该是列表,而不是元组<代码>数据=列表(cursor.fetchall())就可以了。