简体   繁体   English

如何在大型numpy数组中分配列值?

[英]How to assign column values in large numpy array?

I am new to Python programming and I have a problem in assigning specific values to the first column of a very large numpy.array. 我是Python编程的新手,在将特定的值分配给非常大的numpy.array的第一列时遇到问题。

This is the code I use: 这是我使用的代码:

import numpy as np

a = np.zeros ((365343020, 9), dtype = np.float32)

for n in range (0, 36534302):
    a[n*10:(n+1)*10,0] = n

where the second row is where I create an array, of 365343020 rows and 9 columns, filled with zeros; 第二行是我创建数组的地方,其中包含365343020行和9列,并用零填充; while the successive “for” is meant to replace the first column of the array with a vector whose elements are 36534302 sequential integers repeated 10 times each (eg [0,0,…,0,1,1,…,1,2,2,…, 36534301, 36534301,…, 36534301]). 而连续的“ for”是要用一个向量替换数组的第一列,该向量的元素是36534302个连续整数,每个整数重复10次(例如[0,0,…,0,1,1,…,1,2, 2,...,36534301,36534301,...,36534301])。

The code seems to respond as desired till around row 168000000 or the array, then it substitute the 10 repetitions of numbers with the last digit odd with a second repetition of the (even) number before. 该代码似乎可以根据需要进行响应,直到大约168000000行或该数组为止,然后将10个重复的数字替换为最后一个数字为奇数,再替换为第二个(偶数)重复。

I have looked for explanations regarding the difference between views and copies. 我一直在寻找有关视图和副本之间差异的解释。 However, even trying to manually define the content of a specific cell of the array (where it is wrongly defined by the loop), it does not change. 但是,即使尝试手动定义数组特定单元的内容(循环错误地定义了该单元的内容),它也不会改变。

Could you please help me in solving this problem? 您能帮我解决这个问题吗? Thanks 谢谢

Maybe your program is consuming too much memory. 也许您的程序占用了太多的内存。 Here is some basic math for your code. 这是您的代码的一些基本数学。

Date type: float32
Bits used: 32 bits
Size of array: 3288087180 (365343020*9)
Total memory consumed: 105218789760 bits(13.15234872 GB)

1.Try using float8 bit if value being stored in array is not large. 1.如果数组中存储的值不大,请尝试使用float8位。
2.Try to decrease your array size. 2.尝试减小阵列大小。
3.Both 1 and 2 3、1和2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM