如果您对将numpy.frombuffer与JPyjava字节数组一起使用会导致随机输出和numpyfromstring感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解将numpy.frombu
如果您对将 numpy.frombuffer 与 JPy java 字节数组一起使用会导致随机输出和numpy fromstring感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解将 numpy.frombuffer 与 JPy java 字节数组一起使用会导致随机输出的各种细节,并对numpy fromstring进行深入的分析,此外还有关于"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.frombuffer 与 JPy java 字节数组一起使用会导致随机输出(numpy fromstring)
- "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.frombuffer 与 JPy java 字节数组一起使用会导致随机输出(numpy fromstring)
如何解决将 numpy.frombuffer 与 JPy java 字节数组一起使用会导致随机输出
我使用 JPy 作为 Java 和 Python 之间的桥梁。我从Java端启动程序。当我从 Java 端将字节数组 (byte[]
) 作为参数传递给 Python 函数并将其转换为 numpy 数组时,np.frombuffer
的结果将有前 16 个字节不正确(并且其他字节是正确的)。但是,只有当数组的长度超过 392 字节时才会出现此问题。下面是一些重现它的示例代码:
Java 代码 (JPyTest.java)
import java.util.Arrays;
import org.jpy.PyLib;
import org.jpy.PyModule;
import org.jpy.PyObject;
import com.tocaya.utils.TocayaUtils;
public class JPyTest {
public interface Processor {
byte[] byte_array(byte[] array);
}
public static void main(String[] args) {
//Setup JPy stuff
PyLib.startPython(TocayaUtils.substituteEnvVars("put path to proc_class.py here"));
PyModule procModule = PyModule.importModule("proc_class");
PyObject procObj = procModule.call("Processor");
Processor processor = procObj.createProxy(Processor.class);
//Define a byte array with length of 392
byte[] goodBytes = new byte[] {0,-93,-96,51,65,-48,118,-65,29,31,-92,-32,63,-87,-66,17,57,-63,-81,-35,-117,-102,-43,-101,-30,-24,81,-23,-16,6,1,-40,-1,111,119,79,-97,-28,-51,-8,-100,77,-72,-34,-9,11,47,-53,40,-58,125,8,-59,99,-13,-26,-75,-67,10,23,-38,-17,95,-5,-4,-126,-105,25,28,-36,53,-46,-44,64,96,-80,39,35,37,-55,30,120,-120,92,-45,19,22,-12,-113,-99,-127,-68,-89,-112,36,82,-78,-31,61,55,48,-33,-14,9,80,-18,-39,3,-54,-20,106,43,85,71,-95,-41,-73,113,105,-88,117,78,108,32,-103,21,-60,-25,41,-121,20,107,-10,94,-86,26,122,73,116,-110,69,68,-50,-6,-49,74,112,-47,-90,49,13,-37,-85,44,-111,-21,72,-27,-114,24,-74,60,-11,-124,-22,86,83,-3,100,90,75,16,-19,14,-69,63};
System.out.println("good bytes len = " + goodBytes.length);
//Call the python function via JPy
byte[] outBytes = processor.byte_array(goodBytes);
System.out.println("good bytes output = " + Arrays.toString(outBytes));
System.out.println("----------------------");
//Define an array of bytes that has length of 393
byte[] badBytes = Arrays.copyOf(goodBytes,goodBytes.length+1);
System.out.println("bad bytes len = " + badBytes.length);
//Call the python function via JPy
outBytes = processor.byte_array(badBytes);
System.out.println("bad bytes output = " + Arrays.toString(outBytes));
PyLib.stopPython();
}
}
Python 代码 (proc_class.py)
class Processor:
def __init__(self):
pass
def byte_array(self,array):
import numpy as np
print(f''first 24 bytes in python before np.frombuffer {",".join(str(array[i]) for i in range(24))}'')
out = np.frombuffer(array,dtype=np.dtype("b")).tobytes()
print(f''first 24 bytes in python after np.frombuffer {",".join(str(int.from_bytes(out[i:i+1],byteorder="little",signed=True)) for i in range(24))}'')
return out
Java 输出
E:\\Warez\\Development\\Javabin\\jpy-0.10.0\\jpy.cp37-win_amd64.pyd
good bytes len = 392
good bytes output = [0,63]
----------------------
bad bytes len = 393
bad bytes output = [-48,-122,0]
Python 输出
first 24 bytes in python before np.frombuffer 0,63
first 24 bytes in python after np.frombuffer 0,63
first 24 bytes in python before np.frombuffer 0,63
first 24 bytes in python after np.frombuffer -48,63
如您所见,对于第二个字节数组 badBytes
(长度为 393),np.frombuffer 的前 16 个字节的输出不正确。我已经用其他长度进行了测试,只有/总是当字节数组大于 392 字节时才会出现问题。
版本
- Python 3.7.4
- 操作系统 Windows 8.1
- Java 1.8.0_281
- JPy 0.10.0.dev1(截至 2021 年 1 月 26 日在 GitHub 上的最新主版本)
"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.frombuffer 与 JPy java 字节数组一起使用会导致随机输出和numpy fromstring的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于"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的相关信息,请在本站寻找。
本文标签: