此处将为大家介绍关于在保存在磁盘上的numpy数组中的随机访问的详细内容,并且为您解答有关存放在磁盘上的文件,既可随机访问,又可顺序访问的相关问题,此外,我们还将为您介绍关于"importnumpya
此处将为大家介绍关于在保存在磁盘上的 numpy 数组中的随机访问的详细内容,并且为您解答有关存放在磁盘上的文件,既可随机访问,又可顺序访问的相关问题,此外,我们还将为您介绍关于"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 数组中的随机访问(存放在磁盘上的文件,既可随机访问,又可顺序访问)
- "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 数组中的随机访问(存放在磁盘上的文件,既可随机访问,又可顺序访问)
如何解决在保存在磁盘上的 numpy 数组中的随机访问
我有一个大小为 A
的 (2_000_000,2000)
dtype
形状的 numpy 数组 float64
,需要 32 GB。
(或者将相同的数据分成 10 个形状为 (200_000,2000) 的数组,可能更容易序列化?)。
我们如何将其序列化到磁盘,以便我们可以快速随机读取数据的任何部分?
更准确地说,我需要能够在随机起始索引 A
处从 i
读取数万个形状为 (16,2 000) 的窗口:
L = []
for i in range(10_000):
i = random.randint(0,2_000_000 - 16):
window = A[i:i+16,:] # window of A of shape (16,2000) starting at a random index i
L.append(window)
WINS = np.concatenate(L) # shape (10_000,16,2000) of float64,ie: ~ 2.4 GB
假设我只有 8 GB 的 RAM 可用于此任务;完全不可能在 RAM 中加载整个 32 GB 的 A
。
我们如何在磁盘序列化 numpy 数组中读取此类窗口?(.h5 格式或任何其他格式)
注意:读取是在随机起始索引处完成的这一事实很重要。
解决方法
此示例展示了如何将 HDF5 文件用于您描述的过程。
首先,使用 shape(2_000_000,2000)
和 dtype=float64
值的数据集创建一个 HDF5 文件。我为维度使用了变量,所以你可以修改它。
import numpy as np
import h5py
import random
h5_a0,h5_a1 = 2_000_000,2_000
with h5py.File(''SO_68206763.h5'',''w'') as h5f:
dset = h5f.create_dataset(''test'',shape=(h5_a0,h5_a1))
incr = 1_000
a0 = h5_a0//incr
for i in range(incr):
arr = np.random.random(a0*h5_a1).reshape(a0,h5_a1)
dset[i*a0:i*a0+a0,:] = arr
print(dset[-1,0:10]) # quick dataset check of values in last row
接下来,以读取模式打开文件,读取形状为 (16,2_000)
的 10_000 个随机数组切片并附加到列表 L
。最后,将列表转换为数组 WINS
。请注意,默认情况下,数组将有 2 个轴 - 如果您希望每个评论有 3 个轴,则需要使用 .reshape()
(也显示了重塑)。
with h5py.File(''SO_68206763.h5'',''r'') as h5f:
dset = h5f[''test'']
L = []
ds0,ds1 = dset.shape[0],dset.shape[1]
for i in range(10_000):
ir = random.randint(0,ds0 - 16)
window = dset[ir:ir+16,:] # window from dset of shape (16,2000) starting at a random index i
L.append(window)
WINS = np.concatenate(L) # shape (160_000,2_000) of float64,print(WINS.shape,WINS.dtype)
WINS = np.concatenate(L).reshape(10_0000,16,ds1) # reshaped to (10_000,2_000) of float64
print(WINS.shape,WINS.dtype)
上述过程的内存效率不高。您最终获得了随机切片数据的 2 个副本:在列表 L 和数组 WINS 中。如果内存有限,这可能是一个问题。为避免中间拷贝,将随机滑动的数据直接读取到数组中。这样做可以简化代码,并减少内存占用。该方法如下所示(WINS2 是一个 2 轴阵列,WINS3 是一个 3 轴阵列)。
,
with h5py.File(''SO_68206763.h5'',''r'') as h5f:
dset = h5f[''test'']
ds0,dset.shape[1]
WINS2 = np.empty((10_000*16,ds1))
WINS3 = np.empty((10_000,ds1))
for i in range(10_000):
ir = random.randint(0,ds0 - 16)
WINS2[i*16:(i+1)*16,:] = dset[ir:ir+16,:]
WINS3[i,:,:]
如@RyanPepper 的评论中所建议的那样,我尝试过且有效的 h5py
数据集的替代解决方案是使用 memmap
。
将数据写入二进制
import numpy as np
with open(''a.bin'',''wb'') as A:
for f in range(1000):
x = np.random.randn(10*2000).astype(''float32'').reshape(10,2000)
A.write(x.tobytes())
A.flush()
稍后打开为 memmap
A = np.memmap(''a.bin'',dtype=''float32'',mode=''r'').reshape((-1,2000))
print(A.shape) # (10000,2000)
print(A[1234:1234+16,:]) # window
"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 数组中的随机访问和存放在磁盘上的文件,既可随机访问,又可顺序访问的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于"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的相关信息,请在本站寻找。
本文标签: