- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的出发点是 NumPy 的函数 loadtxt 有问题:
X = np.loadtxt(filename, delimiter=",")
在 np.loadtxt(..)
中给出了一个 MemoryError
。我用谷歌搜索并找到了this question on StackOverflow .这给出了以下解决方案:
def iter_loadtxt(filename, delimiter=',', skiprows=0, dtype=float):
def iter_func():
with open(filename, 'r') as infile:
for _ in range(skiprows):
next(infile)
for line in infile:
line = line.rstrip().split(delimiter)
for item in line:
yield dtype(item)
iter_loadtxt.rowlength = len(line)
data = np.fromiter(iter_func(), dtype=dtype)
data = data.reshape((-1, iter_loadtxt.rowlength))
return data
data = iter_loadtxt('your_file.ext')
所以我试过了,但是遇到了以下错误信息:
> data = data.reshape((-1, iter_loadtext.rowlength))
> ValueError: total size of new array must be unchanged
然后我尝试使用下面的代码片段将行数和最大列数添加到代码中,我部分得到了 from another question我自己也写了一部分:
num_rows = 0
max_cols = 0
with open(filename, 'r') as infile:
for line in infile:
num_rows += 1
tmp = line.split(",")
if len(tmp) > max_cols:
max_cols = len(tmp)
def iter_func():
#didn't change
data = np.fromiter(iter_func(), dtype=dtype, count=num_rows)
data = data.reshape((num_rows, max_cols))
但这仍然给出了相同的错误信息,虽然我认为它应该已经解决了。另一方面,我不确定我是否以正确的方式调用 data.reshape(..)
。
我评论了调用 date.reshape(..)
的规则以查看发生了什么。这给出了这个错误信息:
> ValueError: need more than 1 value to unpack
这发生在用 X
完成某些事情的第一点,这个问题就是关于这个变量的。
我知道这段代码可以处理我得到的输入文件,因为我看到它与它们一起使用。但是我找不到为什么我不能解决这个问题。我的推理之所以如此,是因为我使用的是 32 位 Python 版本(在 64 位 Windows 机器上),内存出现问题,而在其他计算机上不会发生。但我不确定。有关信息:我有 8GB 的 RAM 用于 1.2GB 的文件,但根据任务管理器,我的 RAM 未满。
我想解决的是,我正在使用需要读取和解析给定文件的开源代码,就像 np.loadtxt(filename, delimiter=",")
一样,但是随后在我的内存中。我知道代码最初适用于 MacOsx 和 Linux,更准确地说:“MacOsx 10.9.2 和 Linux(版本 2.6.18-194.26.1.el5 (brewbuilder@norob.fnal.gov)(gcc 版本 4.1.2 20080704(Red Hat 4.1.2-48))1 SMP 2010 年 11 月 9 日星期二 12:46:16 EST)。”
我不太在乎时间。我的文件包含 +-200.000 行,其中每行有 100 或 1000 个(取决于输入文件:一个总是 100,一个总是 1000)个项目,其中一个项目是一个 float ,有 3 位小数,取反或不取反,它们由 、
和一个空格分隔。 F.e.:[..] 0.194、-0.007、0.004、0.243、[..]
,以及您看到 4 的那些项目中的 100 或 100 个,用于 +-200.000 行。
我正在使用 Python 2.7,因为开源代码需要它。
你们有解决这个问题的方法吗?提前致谢。
最佳答案
在 Windows 上,32 位进程最多只能获得 2GB(或 GiB?)内存,而 numpy.loadtxt
因占用大量内存而臭名昭著,因此这解释了为什么第一种方法没有不工作。
您似乎面临的第二个问题是您正在测试的特定文件缺少数据,即并非所有行都具有相同数量的值。这很容易检查,例如:
import numpy as np
numbers_per_line = []
with open(filename) as infile:
for line in infile:
numbers_per_line.append(line.count(delimiter) + 1)
# Check where there might be problems
numbers_per_line = np.array(numbers_per_line)
expected_number = 100
print np.where(numbers_per_line != expected_number)
关于np.loadtxt 和 iter_loadtxt 中的 Python MemoryError 或 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26558278/
我试图在一个非 Django 特定的共享虚拟主机上部署 Django。 Host 提供旧版本的 python 安装,但由于我有 ssh 访问能力,我设法通过在我的主文件夹中本地安装它们来使用我需要的模
我是初学者。我最近看到 Mandelbrot set 太棒了,所以我决定用 python 绘制这个集合。但是有一个问题,当我运行这段代码时出现“内存错误”。 这条语句 num_set = gen_nu
假设以下情况:我得到了一个双模网络的邻接矩阵,其中一个维度代表一些项目(帖子)和每个项目下出现的其他标签。现在我想折叠那个双模式网络以获得一个单模式网络的项目到项目关系,其中每个链接的值代表两个项目的
我正在尝试通过从文件中读取数据来绘制具有多种颜色的热图。我可以很好地生成 2D 和法线热图,但无法像附加图像那样绘制。当使用随机数时,我可以绘制它但是在从文件中读取数据时它显示错误。 上面的热图是用随
我正在尝试从维基百科文本数据中训练word2vec模型,因为我正在使用以下代码。 import logging import os.path import sys import multiproces
处理.xml文件= 1,45 Gb时出现MemoryError错误。我试图在一个较小的文件上运行它,并且它可以工作,因此代码中不应有任何错误。该代码本身意味着打开一个xml文件,在其中做一些事情并将其
当我尝试使用以下代码在大型数据帧上删除重复的时间戳时,出现了 MemoryError。 import dask.dataframe as dd path = f's3://{container_nam
尝试分割非常大的字符串时出现内存错误。 data = load_data(file_name) # loads data string from file splited_data = data.sp
我需要扫描两个大的 txt 文件(都是大约 100GB,10 亿行,几列)并取出某一列(写入新文件)。文件看起来像这样 ID*DATE*provider 1111*201101*1234 1234*2
我在第 3 轮 nfind(while 循环)中执行的代码有什么问题,返回符合 CACHE[sha] = number 的 MemoryError?在系统上有足够的内存,并且在 while 循环的每一
我试图避免在我的 mp3 收藏中重复(非常大)。我想通过检查文件内容来检查重复项,而不是查找相同的文件名。我已经编写了下面的代码来执行此操作,但它会在大约一分钟后抛出 MemoryError。关于如何
我正在尝试编写一个程序来计算给定字符串 s 中字符 'a' 的出现次数,只考虑第一个 字符串的 n 个字符。如果字符串的长度小于数字 n,例如 s = "abca" 和 n = 10,则 s 应该变成
我的环境是 Python 3.6 64 位和 64 位 win 10 16GB 内存。 我有一个形状为 (260923,) 的 ndarray。我想找到大于阈值的所有元素的索引。我正在使用这段代码,但
我遇到了一个奇怪的MemoryError,我不明白它为什么会出现。代码示例: # some setup import numpy as np import pandas as pd import ra
我正在尝试处理一个 3GB 的 XML 文件,并且在读取文件并将一些数据存储在字典中的循环中间出现内存错误。 class Node(object): def __init__(self, os
我正在处理一个包含大数据的项目,在运行我的脚本时经常遇到 MemoryError。它在我的脚本读取的文件列表上包含一个循环,在 3 或 4 个文件之后,出现此错误。 我想写这样的东西: with op
我正在尝试对一堆图像(>40k)执行平均缩放。当我将大小为 (3,256,256) 的图像读入 np 数组时,内存使用率为 %40(60 GB 中,使用 htop 检查)。但是,当我运行 arr.st
我有一个小的 Python (2.7.10) 脚本,您可以在下面看到它。 def numbers_calc(max_num, num_step): """Returns every numbe
我正在编写一个 python 脚本来读取两个 csv 文件。代码片段如下。如果文件包含少量记录(8,000 条),则代码可以完美运行,但是如果文件包含大量记录(120,000 条),我会在线上遇到 M
我有一段 python 代码会在一段时间后生成一个 MemoryError。我知道它会消耗大量内存。因此,我决定将代码放在 try/except block 中,这样框架看起来如下所示: while
我是一名优秀的程序员,十分优秀!