python如何创建不同元素的矩阵_如何在python中创建一个大的矩阵矩阵?(How to create a large matrix of matrices in python?)...

如何在python中创建一个大的矩阵矩阵?(How to create a large matrix of matrices in python?)

我正在使用一个大小为m * n的矩阵,m,n> 100000。 由于我的数据很大,我想将矩阵存储在内存中,并使用HDF5和PyTables。

然而,我的矩阵的元素是尺寸为5 * 5的实数值的小矩阵。

我已经查看了以下帖子,但我想知道是否还有其他方法可以在表格中存储这种类型的数据?

先感谢您

I am working with a large matrix of size m * n for m,n>100000. Since my data is huge I want to store the matrix in memory and work with HDF5, and PyTables.

However, the elements of my matrix are small matrices of real values of dimension 5*5.

I have already looked at the following post, but I would like to know if there is any other way of storing this type of data in tables?

Thank you in advance

原文:https://stackoverflow.com/questions/41535275

2020-01-10 06:01

满意答案

在numpy有两个相关的结构。

一个是4维数组,例如np.zeros((100,100,5,5),int) 。 另一个是二维对象数组。 np.zeros((100,100),dtype=object) 。 使用对象数组,元素可以是任何东西 - 字符串,数字,列表,5x5数组,其他7x3数组, None等。

最简单的方法是在4d数组上进行数学运算,例如在所有5x5子阵列中取平均值,或找到所有5:5的[:,:,0,0]角。

如果你的子数组都是5x5,那么创建和填充该object数组可能会很棘手。 如果可能, np.array(...)尝试创建该4dim数组。

使用h5py您可以对文件进行分块,并访问较大阵列的部分内容。 但你仍然必须有一个可行的numpy表示来做任何事情。

In numpy there are two relevant structures.

One is a 4dimensional array, e.g. np.zeros((100,100,5,5),int). The other is an 2 dimensional array of objects. np.zeros((100,100),dtype=object). With object array, the elements can be anythings - strings, numbers, lists, your 5x5 arrays, other 7x3 array, None, etc).

It is easiest to do math on the 4d array, for example taking the mean across all the 5x5 subarrays, or finding the [:,:,0,0] corner of all.

If your subarrays are all 5x5, it can be tricky to create and fill that object array. np.array(...) tries to create that 4dim array if possible.

With h5py you can chunk the file, and access portions of the larger array. But you still have to have a workable numpy representation to do anything with them.

2017-01-08

相关问答

这是MATLAB语法,而不是numpy: A = [1 0 0 2; 3 3 3 2; 3 3 0 2; 3 4 4 4]

虽然np.matrix模仿它: In [172]: A = np.matrix('1 0 0 2; 3 3 3 2; 3 3 0 2; 3 4 4 4')

In [173]: A

Out[173]:

matrix([[1, 0, 0, 2],

[3, 3, 3, 2],

[3, 3, 0, 2],

[3, 4, 4, 4]]...

这听起来很像一个四舍五入的问题。 我假设例如100 * 0.29(作为浮点数)向下舍入(即截断),从而产生28而不是29.尝试将数字四舍五入(即上/下舍入),然后使用它们作为数组索引。 更新:通过测试验证我的猜想,即使数字如上所述 - 请参阅此处 。 This sound very much like a rounding issue. I'd suppose that e.g. 100*0.29 (as a floating point number) is rounded downwards ...

您可以使用列表清单: In [64]: matrix = [["username", "name", "password"],["username2","name2","password2"]]

In [65]: matrix

Out[65]: [['username', 'name', 'password'], ['username2', 'name2', 'password2']]

访问第一行(因为Python使用基于0的索引): In [66]: matrix[0]

Out[66]: [...

在没有深究问题的情况下,您应该确保在Linux平台上使用64位体系结构上的64位构建。 在那里,本机“长”数据类型是64位大小(相反Windows,我相信)。 供参考,请参阅以下表格: http://www.unix.org/whitepapers/64bit.html ( - > LP64上长64位) http://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models 编辑:也许我以前不够明确 - 在64位Windows上,经典的本地...

我发布了对scipy邮件列表的回复; 堆栈溢出更容易访问,所以我也会在这里发布,尽管版本略有改进。 诀窍是使用IJV存储格式。 这是三个阵列的三个,其中第一个包含行指示,第二个包含列指示,第三个具有该位置的矩阵值。 这是建立有限元素矩阵(或者我认为的任何稀疏矩阵)的最佳方法,因为对这种格式的访问非常快(只是填充一个数组)。 在scipy中,这称为coo_matrix ; 该类将三个数组作为参数。 它实际上只对转换为快速线性代数的另一种格式(CSR os CSC)很有用。 对于有限元,您可以通过类似...

这样的事情怎么样...... import numpy as np

# Create large arrays x and y.

# Note they are 1e4 not 1e6 b/c of memory issues creating random numpy matrices (CookieOfFortune)

# However, the same principles apply to larger arrays

x = np.random.randn(10000, 10000...

如果你想要一个rank-3 Numpy数组,并且你事先知道f(i)的形状,你可以预先分配数组: a = np.zeros((n,) + shape)

for i in range(n):

a[i] = f(i)

如果您只想要一个列表(而不是Numpy数组)的矩阵,请使用列表推导: a = [f(i) for i in range(n)]

获取Numpy数组的另一种方法是从上面的列表推导转换: a = np.array([f(i) for i in range(n)])

但是,这将比#...

编辑 :正如PM 2Ring所建议的, SymPy可能是解决方案。 其他答案 : 我认为TensorFlow在这种情况下可能会帮助你。 虽然,在这种情况下,写方程式可能略有不同。 import tensorflow as tf

# define your variables before hand.

# tf.placeholder(, )

arr1 = tf.placeholder(tf.float32, [2,2])

arr2 = t...

在numpy有两个相关的结构。 一个是4维数组,例如np.zeros((100,100,5,5),int) 。 另一个是二维对象数组。 np.zeros((100,100),dtype=object) 。 使用对象数组,元素可以是任何东西 - 字符串,数字,列表,5x5数组,其他7x3数组, None等。 最简单的方法是在4d数组上进行数学运算,例如在所有5x5子阵列中取平均值,或找到所有5:5的[:,:,0,0]角。 如果你的子数组都是5x5,那么创建和填充该object数组可能会很棘手。 如果...

这个: numpy.bmat([[numpy.zeros(appropriate_shape), A], [B, C]])

工作,但我不知道如何避免创建那个庞大,无用的零数组。 此外,它返回矩阵而不是数组,因此如果需要数组,请asarray在其上调用asarray 。 This: numpy.bmat([[numpy.zeros(appropriate_shape), A], [B, C]])

works, but I'm not sure how to avoid the creation ...

相关文章

Data Week: Becoming a data scientist Data Pointed,

...

Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重

...

abs(x) 说明:abs(x)返回x的绝对值,如果参数是复数,则返回复数的模; 参数x:整

...

原文地址:http://blog.chinaunix.net/uid-25525723-id-3630

...

http://gumstix.org/create-a-bootable-microsd-card.h

...

中文名: wordpress插件制作视频教程 英文名: How to create a word

...

pychseg - A Python Chinese Segment Project - Google

...

mod_python: the long story - Grisha Trubetskoy

...

原文地址:http://wiki.woodpecker.org.cn/moin/ObpLovelyPy

...

python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部