在本文中,我们将给您介绍关于使用numpy.memmap的详细内容,并且为您解答使用时间的相关问题,此外,我们还将为您提供关于"importnumpyasnp"ImportError:Nomodule
在本文中,我们将给您介绍关于使用 numpy.memmap的详细内容,并且为您解答使用时间的相关问题,此外,我们还将为您提供关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案、Difference between import numpy and import numpy as np的知识。
本文目录一览:- 使用 numpy.memmap(使用时间)
- "import numpy as np" ImportError: No module named numpy
- 3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数
- Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案
- Difference between import numpy and import numpy as np
使用 numpy.memmap(使用时间)
如何解决使用 numpy.memmap?
我已经使用 numpy.memmap 实现了一个文件支持的 HashTable。它似乎运行正常,但是,我注意到在 Linux 上 KSysGuard 和 SMART 都报告了荒谬的 IO 写入量。大约是应该写入的数据量的 50 倍。我没有在其他操作系统上测试过。
这是创建内部存储器映射的代码
self.data = np.memmap(self.filename,shape=(self.nbuckets,self.bucket_size),dtype=[(''key'',''u8''),(''time'',''u2''),(''flags'',(''id'',''i4'')],mode=mode)
以下是应用哈希函数后将单个条目写入表中的代码:
def store(self,id_,time_,key,i):
bucket = self.data[i]
head = bucket[0]
if bucket[1][''flags''] & HashTable.FLAG_INUSE == 0:
free = bucket[1]
head[''id''] = 1 #we use the first entry in the bucket to count how full it is
self.written += 4 #4 bytes written to set that counter to 1
elif head[''id''] < self.bucket_size:
free = bucket[head[''id''] ]
else:
return False
free[''key''] = key
free[''time''] = time_
free[''flags''] |= HashTable.FLAG_INUSE
free[''id''] = id_
head[''id''] += 1
self.dirty = True
self.written += 20 #16 bytes for the entry,+4 bytes for updating bucket usage counter
return True
我添加了 self.written
变量来跟踪写入的字节数,并确定函数是否被调用了太多次。
对于大约 300 万个条目,self.written
最后报告了大约 60 MiB,根据我的计算,这是合理的,并且意味着 store 函数没有被过度调用。但是,KSysGuard 和 SMART (data_units_written) 报告该程序总共写入了 3 GiB。我使用的 HashTable 设置为 100 MiB 并且没有损坏或任何东西,所以我怀疑它只是一遍又一遍地将相同的数据写入相同的位置。但我无法弄清楚这可能发生在代码中的哪个位置。
我不是 100% 确定写入发生在 HashTable 文件中,但是当我将它放入 ramdisk (tmpfs) 时,KSysGuard 和 SMART 没有报告磁盘写入。
我在 Debian bullseye 上使用 Python 3.9.2 和 numpy 1.21.0。
如果有人能帮我解决这个问题,我将不胜感激。 谢谢。
解决方法
memmap
通过在虚拟内存中映射页面来工作(通常映射到物理内存页面或存储设备页面,如您的情况)。在大多数平台上,页面大小至少为 4 KiB。因此,页面中的任何写入都可能导致整个页面被更新。
SSD 和更普遍的闪存也使用块,但它们通常使用更大的块。事实上,闪存使用写入次数非常有限的单元(例如 1000)。当单元被覆盖太多时,它们会变得不稳定并且可能无法正确读取/写入。因此,闪存设备避免了对单元的任何直接写入访问,并将写入的数据块移动到新位置以保存单元,同时相对较快。 一旦写入,块就不能改变:需要分配和写入一个新块来替换旧块。因此,在闪存设备上仅随机写入几个字节会导致它分配大量新块并复制大量(未更改的)数据块。这也会显着影响目标存储设备的寿命。 这可以解释为什么 SMART 信息报告如此大量的 IO 写入。
请注意,HDD 没有此问题,但与 SSD 相比,随机写入非常慢(由于移动磁头的时间)。铁电 RAM 或磁阻 RAM 等替代非易失性 RAM 可以正确解决此问题。不幸的是,目前这种 RAM 还处于实验阶段。
一个可能的解决方法是将修改后的数据块存储在 RAM 中,按位置对块进行排序,然后一次性写入所有数据。如果数据集很大,写入的分布非常均匀,那么目前主流硬件上没有解决方案。
"import numpy as np" ImportError: No module named numpy
问题:没有安装 numpy
解决方法:
下载文件,安装
numpy-1.8.2-win32-superpack-python2.7
安装运行 import numpy,出现
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import numpy
File "C:\Python27\lib\site-packages\numpy\__init__.py", line 153, in <module>
from . import add_newdocs
File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Python27\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Python27\lib\site-packages\numpy\core\__init__.py", line 6, in <module>
from . import multiarray
ImportError: DLL load failed: %1 不是有效的 Win32 应用程序。
原因是:python 装的是 64 位的,numpy 装的是 32 位的
重新安装 numpy 为:numpy-1.8.0-win64-py2.7
3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数
目录
[TOC]
前言
具体我们来学 Numpy 的统计函数
(一)函数一览表
调用方式:np.*
.sum(a) | 对数组 a 求和 |
---|---|
.mean(a) | 求数学期望 |
.average(a) | 求平均值 |
.std(a) | 求标准差 |
.var(a) | 求方差 |
.ptp(a) | 求极差 |
.median(a) | 求中值,即中位数 |
.min(a) | 求最大值 |
.max(a) | 求最小值 |
.argmin(a) | 求最小值的下标,都处里为一维的下标 |
.argmax(a) | 求最大值的下标,都处里为一维的下标 |
.unravel_index(index, shape) | g 根据 shape, 由一维的下标生成多维的下标 |
(二)统计函数 1
(1)说明
(2)输出
.sum(a)
.mean(a)
.average(a)
.std(a)
.var(a)
(三)统计函数 2
(1)说明
(2)输出
.max(a) .min(a)
.ptp(a)
.median(a)
.argmin(a)
.argmax(a)
.unravel_index(index,shape)
作者:Mark
日期:2019/02/11 周一
Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案
如何解决Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案?
希望有人能在这里提供帮助。我一直在绕圈子一段时间。我只是想设置一个 python 脚本,它将一些 json 数据从 REST API 加载到云数据库中。我在 Anaconda 上设置了一个虚拟环境(因为 GCP 库推荐这样做),安装了依赖项,现在我只是尝试导入库并向端点发送请求。 我使用 Conda(和 conda-forge)来设置环境并安装依赖项,所以希望一切都干净。我正在使用带有 Python 扩展的 VS 编辑器作为编辑器。 每当我尝试运行脚本时,我都会收到以下消息。我已经尝试了其他人在 Google/StackOverflow 上找到的所有解决方案,但没有一个有效。我通常使用 IDLE 或 Jupyter 进行脚本编写,没有任何问题,但我对 Anaconda、VS 或环境变量(似乎是相关的)没有太多经验。 在此先感谢您的帮助!
\Traceback (most recent call last):
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\__init__.py",line 22,in <module>
from . import multiarray
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\multiarray.py",line 12,in <module>
from . import overrides
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\overrides.py",line 7,in <module>
from numpy.core._multiarray_umath import (
ImportError: DLL load Failed while importing _multiarray_umath: The specified module Could not be found.
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "c:\API\citi-bike.py",line 4,in <module>
import numpy as np
File "C:\Conda\envs\gcp\lib\site-packages\numpy\__init__.py",line 150,in <module>
from . import core
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\__init__.py",line 48,in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions Failed. This error can happen for
many reasons,often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: python3.9 from "C:\Conda\envs\gcp\python.exe"
* The NumPy version is: "1.21.1"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: DLL load Failed while importing _multiarray_umath: The specified module Could not be found.
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Difference between import numpy and import numpy as np
Difference between import numpy and import numpy as np
up vote 18 down vote favorite 5 |
I understand that when possible one should use This helps keep away any conflict due to namespaces. But I have noticed that while the command below works the following does not Can someone please explain this? python numpy
|
||||||||
add a comment |
4 Answers
active oldest votes
up vote 13 down vote |
numpy is the top package name, and doing When you do In your above code: Here is the difference between
|
|||
add a comment |
up vote 7 down vote |
The When you import a module via the numpy package is bound to the local variable Thus, is equivalent to, When trying to understand this mechanism, it''s worth remembering that When importing a submodule, you must refer to the full parent module name, since the importing mechanics happen at a higher level than the local variable scope. i.e. I also take issue with your assertion that "where possible one should [import numpy as np]". This is done for historical reasons, mostly because people get tired very quickly of prefixing every operation with Finally, to round out my exposé, here are 2 interesting uses of the 1. long subimports 2. compatible APIs
|
||
add a comment |
up vote 1 down vote |
when you call the statement
|
||
add a comment |
up vote 1 down vote |
This is a language feature. This feature allows:
Notice however that Said that, when you run You receive an
|
||||||||
add a comment |
今天关于使用 numpy.memmap和使用时间的讲解已经结束,谢谢您的阅读,如果想了解更多关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案、Difference between import numpy and import numpy as np的相关知识,请在本站搜索。
本文标签: