本文将分享从N个numpy数组生成相等大小的批次的详细内容,并且还将对使用numpy产生200个数的数组进行详尽解释,此外,我们还将为大家带来关于"importnumpyasnp"ImportErro
本文将分享从 N 个 numpy 数组生成相等大小的批次的详细内容,并且还将对使用numpy产生200个数的数组进行详尽解释,此外,我们还将为大家带来关于"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的相关知识,希望对你有所帮助。
本文目录一览:- 从 N 个 numpy 数组生成相等大小的批次(使用numpy产生200个数的数组)
- "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
从 N 个 numpy 数组生成相等大小的批次(使用numpy产生200个数的数组)
如何解决从 N 个 numpy 数组生成相等大小的批次
我有 N 个形状为 data[n,m,3]
的 NumPy 数组。我想将它们拟合/挤压/分割/切片/重塑为 N'' 个形状为 new_data_#[1000,3]
的数组,其中 # 是新数组的索引。问题是 n 可以更小,或大于 1000。当它以某种方式更小时,我应该用下一个数组填充 new_array 剩余的 1000 个容量,当它大于 1000 时,我应该创建一个 new_data_# 并添加休息到那个。我不知道如何管理这个。这是一个伪代码,但它不能以这种方式完成,例如, while 可能不是必需的。输出可以写入磁盘或以新的数据格式返回。
def array2blocks(array_files)
for each N in array_files:
N = data = np.random.rand(n,3)
new_data = np.zeros((1000,3),dtype=np.float32)
j=0
index = 0
while j <= new_data.shape[0]:
for i in range(data.shape[0]):
print("--->",data[i,:,:])
print (i)
if i <= new_data.shape[0]:
# here first we should check the left capacity of new_data and then insert data into it
# new_data[i,:] = data[i,:] #this overrides prevIoUs items so not correct
print(new_data)
else:
print(''n>1000'')
new_data_name = ''new_data'' + ''_'' + str(index)
# here fill rest of the data in the new_data
...
index += 1
#when capacity is full write it to the disk
print(new_data)
UPDATE 与 Aaron 的旧答案: 我用 batch_size = 5
替换了 1000 以简化操作。
def numpyarrays2blocks(array_files):
N1 = np.random.rand(7,4,3)
N2 = np.random.rand(7,3)
N3 = np.random.rand(4,3)
# array_files = []
array_files.append(N1)
array_files.append(N2)
array_files.append(N3)
for N in array_files:
n = N.shape[0]
m = N.shape[1]
batch_size = 5
# N = data = np.random.rand(n,3)
data = N
# print(data)
new_arrays = []
i = 0 # the current row index to insert
while i < n:
new_data = np.zeros((batch_size,dtype=np.float32)
j = min(i + batch_size,n) # the last row (exclusive) to copy to new_data
# j - i is the number of rows to copy
new_data[:j - i,:] = data[i:j,:]
print(''NEW DATA: '',new_data)
i = j # update the index
new_arrays.append(new_data)
print(new_arrays)
解决方法
data
用于存储临时结果,data_start
是向data
插入行的索引。- 如果是
data
则分配None
yield data
如果已满。
merge_and_split
是一个生成器,因此内存需求应该很低。
import random
from typing import Iterator
import numpy as np
def merge_and_split(arrays,batch_size) -> Iterator:
arrays = tuple(arrays)
dtype = arrays[0].dtype
data_shape = (batch_size,) + arrays[0].shape[1:]
assert all(a.shape[1:] == data_shape[1:] for a in arrays),"Shape mismatch"
data = None
data_start = 0
for src in arrays:
src_index = 0
src_avail = src.shape[0]
while src_avail >= 1:
if data is None:
# allocate if None
data = np.zeros(data_shape,dtype=dtype)
data_start = 0
num_moved = min(batch_size - data_start,src_avail)
data[data_start:data_start + num_moved,...] = src[src_index:src_index + num_moved,...]
data_start += num_moved
src_index += num_moved
src_avail -= num_moved
if data_start >= batch_size:
yield data
data = None
if data is not None:
yield data
def input_arrays():
number = 10
r = random.Random(13)
return [np.random.randint(0,10,size=(r.randint(1,5),4,3)) for _ in range(number)]
def main():
# Testing input and output
arrays = input_arrays()
# for i,item in enumerate(arrays):
# print(''input'',i,item.shape)
# print(item)
result = list(merge_and_split(arrays,5))
# for i,item in enumerate(result):
# print(''result'',item.shape)
# print(item)
src_concat = np.vstack(arrays)
row_number = sum(s.shape[0] for s in arrays)
print(''concatenated'',src_concat.shape,row_number)
out_concat = np.vstack(result)
print(out_concat.shape)
print((out_concat[0:row_number,...] == src_concat).all()) # They are indeed the same
if __name__ == ''__main__'':
main()
,
你可以concatenate
你所有的原始数组split
它们:
ars = ... # list of N arrays
ars = np.concatenate(ars,axis=0)
ars = np.split(ars,np.arange(1000,ars.shape[0],1000))
最后一行可以写成 ars = np.split(ars,1000)
,但前提是您确定元素总数是 1000 的倍数,否则 np.split
会导致呕吐。指定明确的分割点,就像 np.arange
一样,可以让您拥有更短的最后一段。
"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 |
今天关于从 N 个 numpy 数组生成相等大小的批次和使用numpy产生200个数的数组的介绍到此结束,谢谢您的阅读,有关"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等更多相关知识的信息可以在本站进行查询。
本文标签: