python - 通过 API 请求高效发送稀疏矩阵 numpy

标签 python numpy scipy sparse-matrix

我需要通过http和JSON在多个服务器到服务器的通信中发送一些巨大的矩阵(全是0)。

我正在 Python 3.x 中使用 numpy 和 scipy。

有没有标准的工具来做到这一点?

我想我可以发送索引,并以某种方式在第二个服务器中重建矩阵以获得完整的矩阵,但我想避免使用自定义代码来重新发明轮子。

提前谢谢您。

最佳答案

最简单的方法就是 pickling ,但是专用函数可能更高效!

这里有一些使用 python3 和 scipy 专用的演示 save_npz函数(默认使用压缩),用 BytesIO 包裹(不使用文件;在内存中进行)。

我没有接触 JSON 部分,但这看起来微不足道(因为我们在这里准备了一个字符串),特别是对于从事网络工作的人来说。

代码:

import io
import scipy.sparse as sp

mat = sp.random(100, 100, density=0.001)
print(mat)

# mat to serialized-string
tmp = io.BytesIO()
sp.save_npz(tmp, mat)
tmp.seek(0)  # back to start
str_ = tmp.read()
print(str_)

# serialized-string to mat
tmp_ = io.BytesIO(str_)
mat_loaded = sp.load_npz(tmp_)
print(mat_loaded)

输出:

(59, 11)      0.137877385333
(7, 36)       0.137729960685
(94, 14)      0.0951372931412
(3, 80)       0.235640993271
(56, 54)      0.504472012678
(8, 14)       0.657124520803
(22, 92)      0.951629612278
(81, 18)      0.733232743418
(39, 16)      0.228000113182
(17, 15)      0.127198226805
b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xd5}gK\xc9\xb8\xd0xH\x00\x00\x00\\\x00\x00\x00\n\x00\x00\x00format.npy\x9b\xec\x17\xea\x1b\x10\xc9\xc8\xe0\xc6P\xad\x9e\x92Z\x9c\\\xa4n\xa5\xa0n\x13j\xac\xae\xa3\xa0\x9e\x96_TR\x94\x98\x17\x9f_\x94\x92\n\x12wK\xcc)N\x05\x8a\x17g$\x16\xa4\x02\xf9\x1a\x9a:\n\xb5\n(\x80+\x99\x81\x81!\x1f\x8a\x01PK\x03\x04\x14\x00\x00\x00\x08\x00\xd5}gKR\xab(\x82I\x00\x00\x00X\x00\x00\x00\t\x00\x00\x00shape.npy\x9b\xec\x17\xea\x1b\x10\xc9\xc8\xe0\xc6P\xad\x9e\x92Z\x9c\\\xa4n\xa5\xa0n\x93i\xa2\xae\xa3\xa0\x9e\x96_TR\x94\x98\x17\x9f_\x94\x92\n\x12wK\xcc)N\x05\x8a\x17g$\x16\xa4\x02\xf9\x1aF:\x9a:\n\xb5\nH\x80+\x85\x81\x81\x01\x84\x01PK\x03\x04\x14\x00\x00\x00\x08\x00\xd5}gKy\xea\xf44\x99\x00\x00\x00\xa0\x00\x00\x00\x08\x00\x00\x00data.npy\x9b\xec\x17\xea\x1b\x10\xc9\xc8\xe0\xc6P\xad\x9e\x92Z\x9c\\\xa4n\xa5\xa0n\x93f\xa1\xae\xa3\xa0\x9e\x96_TR\x94\x98\x17\x9f_\x94\x92\n\x12wK\xcc)N\x05\x8a\x17g$\x16\xa4\x02\xf9\x1a\x86\x06:\x9a:\n\xb5\n\x08\xc0%\xb3,/\xec\xfb\xd2\x83\xf6%/2\x96)-<h\xefp\x7f\xf5\xabWQ;\xec\x0f\x88\xdc|]\xady\xce\xbe)\xb2\xadv\x91\xca\x03\xfb\x97\x8f\x8f3h\xb1?\xb5?\xdat\xe5\xe3\xfe\xe2w\xf6\xe5\x87W5/){n/\xc1|v\x92\xb4\xfeY{\tY9\x01\x0e\x8f\x03\xf6\x00PK\x03\x04\x14\x00\x00\x00\x08\x00\xd5}gK\x96\xb0\xb4\xa3]\x00\x00\x00x\x00\x00\x00\x07\x00\x00\x00col.npy\x9b\xec\x17\xea\x1b\x10\xc9\xc8\xe0\xc6P\xad\x9e\x92Z\x9c\\\xa4n\xa5\xa0n\x93i\xa2\xae\xa3\xa0\x9e\x96_TR\x94\x98\x17\x9f_\x94\x92\n\x12wK\xcc)N\x05\x8a\x17g$\x16\xa4\x02\xf9\x1a\x86\x06:\x9a:\n\xb5\n\x08\xc0\xc5\xcd\xc0\xc0\xa0\x02\xc4|@\x1c\x00\xc4fPv\x0c\x10\x0b\x01\xb1\x00\x10\xf3\x031\x00PK\x03\x04\x14\x00\x00\x00\x08\x00\xd5}gK\r\xef\xd0@_\x00\x00\x00x\x00\x00\x00\x07\x00\x00\x00row.npy\x9b\xec\x17\xea\x1b\x10\xc9\xc8\xe0\xc6P\xad\x9e\x92Z\x9c\\\xa4n\xa5\xa0n\x93i\xa2\xae\xa3\xa0\x9e\x96_TR\x94\x98\x17\x9f_\x94\x92\n\x12wK\xcc)N\x05\x8a\x17g$\x16\xa4\x02\xf9\x1a\x86\x06:\x9a:\n\xb5\n\x08\xc0e\xcd\xc0\xc0\xc0\x0e\xc4q@\xcc\x0c\xc4\x16@\xcc\x01\xc4b@\x1c\x08\xc4\xea@,\x08\xc4\x00PK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xd5}gK\xc9\xb8\xd0xH\x00\x00\x00\\\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb6\x81\x00\x00\x00\x00format.npyPK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xd5}gKR\xab(\x82I\x00\x00\x00X\x00\x00\x00\t\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb6\x81p\x00\x00\x00shape.npyPK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xd5}gKy\xea\xf44\x99\x00\x00\x00\xa0\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb6\x81\xe0\x00\x00\x00data.npyPK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xd5}gK\x96\xb0\xb4\xa3]\x00\x00\x00x\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb6\x81\x9f\x01\x00\x00col.npyPK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xd5}gK\r\xef\xd0@_\x00\x00\x00x\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb6\x81!\x02\x00\x00row.npyPK\x05\x06\x00\x00\x00\x00\x05\x00\x05\x00\x0f\x01\x00\x00\xa5\x02\x00\x00\x00\x00'
(59, 11)      0.137877385333
(7, 36)       0.137729960685
(94, 14)      0.0951372931412
(3, 80)       0.235640993271
(56, 54)      0.504472012678
(8, 14)       0.657124520803
(22, 92)      0.951629612278
(81, 18)      0.733232743418
(39, 16)      0.228000113182
(17, 15)      0.127198226805

关于python - 通过 API 请求高效发送稀疏矩阵 numpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159199/

相关文章:

python - 用numpy乘以高阶矩阵

python - 使用 scrapy python 爬取 NodeJs 和 AngularJs 站点

python - 有人可以解释 Python 结构解包吗?

numpy - 5428 步后对象检测崩溃,类型错误 : 'numpy.float64' object cannot be interpreted as an integer

python - 如何为 np.random.uniform 生成种子?

Python SciPy 统计 percentilofscore

python - 导入已安装的模块时为 "ImportError: DLL load failed: The specified module could not be found"

python - Clojure 素数惰性序列

用于附加任意类对象列表属性的 Pythonic 语法

python - 如何加速 numpy 代码