Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
Python 高效的列表操作_Python_List_Function_Append - Fatal编程技术网

Python 高效的列表操作

Python 高效的列表操作,python,list,function,append,Python,List,Function,Append,我有一个大矩阵(1017209行),我需要从中读取元素,对它们进行操作,并将结果收集到列表中。当我在10000行甚至100000行上完成时,它会在合理的时间内完成,但是1000000行不会。这是我的密码: import pandas as pd data = pd.read_csv('scaled_train.csv', index_col=False, header=0) new = data.as_matrix() def vectorized_id(j): """Return

我有一个大矩阵(1017209行),我需要从中读取元素,对它们进行操作,并将结果收集到列表中。当我在10000行甚至100000行上完成时,它会在合理的时间内完成,但是1000000行不会。这是我的密码:

import pandas as pd

data = pd.read_csv('scaled_train.csv', index_col=False, header=0)
new = data.as_matrix()

def vectorized_id(j):
    """Return a 1115-dimensional unit vector with a 1.0 in the j-1'th position
    and zeroes elsewhere.  This is used to convert the store ids (1...1115)
    into a corresponding desired input for the neural network.
    """
    j = j - 1    
    e = [0] * 1115
    e[j] = 1.0
    return e

def vectorized_day(j):
    """Return a 7-dimensional unit vector with a 1.0 in the j-1'th position
    and zeroes elsewhere.  This is used to convert the days (1...7)
    into a corresponding desired input for the neural network.
    """
    j = j - 1
    e = [0] * 7
    e[j] = 1.0
    return e

list_b = []
list_a = []

for x in xrange(0,1017209):
    a1 = vectorized_id(new[x][0])
    a2 = vectorized_day(new[x][1])
    a3 = [new[x][5]]
    a = a1 + a2 + a3
    b = new[x][3]
    list_a.append(a)
    list_b.append(b)
是什么让它在这种规模上变慢(瓶颈是什么)?有什么方法可以优化它吗?

有几点:

  • 不要一次读取整个文件,您似乎没有执行任何需要多行的操作
  • 查看如何使用加载数据
  • 真正停止在巨人
    列表中编制索引

  • 你查了四次
    new[x]
    。你有多少内存?您正在使用大量的内存。您一次只需要一行,但似乎要将整个文件读入内存。正如@user2357112所说,您正在使用大量的内存。
    list\u a
    的每个元素的长度为1115+7+1=1123,而
    list\u a
    的元素长度为1017209。所以,大约1k x 1m=1g您试图存储的数字。因此,内存消耗很容易在4 GB、8 GB左右。而且大部分都是零,所以你应该以某种方式利用这种稀疏性。@Rishi的确,我遇到了一个内存问题。我开始使用csv.reader,但它仍然不起作用,大约在第320000行,我的内存使用率为95%,基本上停止了。我认为向量化的_id()会导致问题,因为它为每一行提供1115长度的向量。因此,它将产生一个列表,其中包括Rishi提到的几个GBs,对吗?然后我想我应该把中间结果保存在硬盘上,并清空内存。关于如何做到这一点,有什么建议吗?正如@Rishi所提到的,您使用了大量内存。如果您确实需要所有正在生成的记录,则可能需要将它们写入文件,并在需要时重新加载这些文件。