GVKun编程网logo

使用 Numpy PYthon 在值之间进行检查(nums在python)

4

此处将为大家介绍关于使用NumpyPYthon在值之间进行检查的详细内容,并且为您解答有关nums在python的相关问题,此外,我们还将为您介绍关于AnacondaNumpy错误“Importing

此处将为大家介绍关于使用 Numpy PYthon 在值之间进行检查的详细内容,并且为您解答有关nums在python的相关问题,此外,我们还将为您介绍关于Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案、cvxpy 和 numpy 之间的版本冲突:“针对 API 版本 0xe 编译的模块,但此版本的 numpy 是 0xd”、Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable、numpy.random.randn () 和 numpy.random.rand ()的有用信息。

本文目录一览:

使用 Numpy PYthon 在值之间进行检查(nums在python)

使用 Numpy PYthon 在值之间进行检查(nums在python)

如何解决使用 Numpy PYthon 在值之间进行检查

我正在尝试将下面的代码转换为 Numpy 版本。 vanilla python 代码检查 Formating 的先前值和当前值,并检查是否有任何 Numbers 值在它们之间。此代码的 Numpy 版本有问题,我该如何修复?代码来自以下问题:issue link

值:

  1. Numbers = np.array([3,4,5,7,8,10,20])
  2. Formating = np.array([0,2,12,15,22])
  3. x = np.sort(Numbers);
  4. l = np.searchsorted(x,Formating,side=''left'')

香草蟒:

  1. for i in range(len(l)-1):
  2. if l[i] >= l[i+1]:
  3. print(''Numbers between %d,%d = _0_'' % (Formating[i],Formating[i+1]))
  4. else:
  5. print(''Numbers between %d,%d = %s'' % (Formating[i],Formating[i+1],'',''.join(map(str,list(x[l[i]:l[i+1]])))))

Numpy 版本:

  1. L_index = np.arange(0,len(l)-1,1)
  2. result= np.where(l[L_index] >= l[L_index+1],l )

预期输出:

  1. [0]
  2. [3 4]
  3. [5 7 8 10]
  4. [0]
  5. [20]

解决方法

上一个问题的答案:

  1. In [173]: Numbers = np.array([3,4,5,7,8,10,20])
  2. ...: Formating = np.array([0,2,12,15,22])
  3. ...: x = np.sort(Numbers);
  4. ...: l = np.searchsorted(x,Formating,side=''left'')
  5. ...:
  6. In [174]: l
  7. Out[174]: array([0,6,7])
  8. In [175]: for i in range(len(l)-1):
  9. ...: if l[i] >= l[i+1]:
  10. ...: print(''Numbers between %d,%d = _0_'' % (Formating[i],Formating[i+1]))
  11. ...: else:
  12. ...: print(''Numbers between %d,%d = %s'' % (Formating[i],Formating[i+1],'',''.jo
  13. ...: in(map(str,list(x[l[i]:l[i+1]])))))
  14. ...:
  15. Numbers between 0,2 = _0_
  16. Numbers between 2,5 = 3,4
  17. Numbers between 5,12 = 5,10
  18. Numbers between 12,15 = _0_
  19. Numbers between 15,22 = 20

一些可以很好地处理列表的东西 - 实际上列表比数组更快:

  1. In [182]: for i in range(len(Formating)-1):
  2. ...: print([x for x in Numbers if (Formating[i]<=x<Formating[i+1])])
  3. ...:
  4. []
  5. [3,4]
  6. [5,10]
  7. []
  8. [20]

Formating 上迭代但不是 Numbers 的版本。与使用 searchsorted 的版本非常相似。我不确定哪个会更快:

  1. In [177]: for i in range(len(Formating)-1):
  2. ...: idx = (Formating[i]<=Numbers)&(Numbers<Formating[i+1])
  3. ...: print(Numbers[idx])
  4. ...:
  5. []
  6. [3 4]
  7. [ 5 7 8 10]
  8. []
  9. [20]

我们可以一次性获得 idx 的所有值的 Formating 掩码:

  1. In [183]: mask=(Formating[:-1,None]<=Numbers)&(Numbers<Formating[1:,None])
  2. In [184]: mask
  3. Out[184]:
  4. array([[False,False,False],[ True,True,[False,True]])
  5. In [185]: N=Numbers[:,None].repeat(5,1).T # 5 = len(Formating)-1
  6. In [186]: N
  7. Out[186]:
  8. array([[ 3,20],[ 3,20]])
  9. In [187]: np.ma.masked_array(N,~mask)
  10. Out[187]:
  11. masked_array(
  12. data=[[--,--,--],[3,[--,20]],mask=[[ True,True],False]],fill_value=999999)

您的清单在那里很明显。但是列表显示还是需要迭代的:

  1. In [188]: for row in mask:
  2. ...: print(Numbers[row])
  3. []
  4. [3 4]
  5. [ 5 7 8 10]
  6. []
  7. [20]

我会让你用这个或更真实的数据来测试这些替代方案。我怀疑纯列表版本对于小问题最快,但我不确定其他版本将如何扩展。

编辑

以下问题是关于总和的。 np.ma.sum 或掩码数组自己的 sum 方法对未掩码值求和,有效地用 0 填充掩码值。

  1. In [253]: np.ma.masked_array(N,~mask).sum(axis=1)
  2. Out[253]:
  3. masked_array(data=[--,30,mask=[ True,fill_value=999999)
  4. In [256]: np.ma.masked_array(N,~mask).filled(0)
  5. Out[256]:
  6. array([[ 0,0],[ 0,20]])

实际上我们不需要使用掩码数组机制来到达这里(虽然它在视觉上可以很好):

  1. In [258]: N*mask
  2. Out[258]:
  3. array([[ 0,20]])
  4. In [259]: (N*mask).sum(axis=1)
  5. Out[259]: array([ 0,20])

Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案

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 (将#修改为@)

cvxpy 和 numpy 之间的版本冲突:“针对 API 版本 0xe 编译的模块,但此版本的 numpy 是 0xd”

cvxpy 和 numpy 之间的版本冲突:“针对 API 版本 0xe 编译的模块,但此版本的 numpy 是 0xd”

如何解决cvxpy 和 numpy 之间的版本冲突:“针对 API 版本 0xe 编译的模块,但此版本的 numpy 是 0xd”?

我正在尝试升级一些软件包并为现有的 Python 程序整合我的 requirements.txt,以便将其移至 docker 容器。

这个容器将基于 tensorflow docker 容器,这决定了我必须使用的一些包版本。我们在 windows 下工作,我们希望能够在我们的机器上本地运行该程序(至少在一段时间内)。所以我需要找到一个适用于 docker 和 Windows 10 的配置。

Tensorflow 2.4.1 需要 numpy~=1.19.2。使用 numpy 1.20 时,pip 会抱怨 numpy 1.20 是一个不兼容的版本。

但是在使用 numpy~=1.19.2 时,导入 cvxpy 时出现以下错误。 pip 安装所有软件包都很好:

RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
Traceback (most recent call last):
  File "test.py",line 1,in <module>
    import cvxpy
  File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\__init__.py",line 18,in <module>
    from cvxpy.atoms import *
  File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\__init__.py",line 20,in <module>
    from cvxpy.atoms.geo_mean import geo_mean
  File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\geo_mean.py",in <module>
    from cvxpy.utilities.power_tools import (fracify,decompose,approx_error,lower_bound,File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\utilities\power_tools.py",in <module>
    from cvxpy.atoms.affine.reshape import reshape
  File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\affine\reshape.py",in <module>
    from cvxpy.atoms.affine.hstack import hstack
  File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\affine\hstack.py",in <module>
    from cvxpy.atoms.affine.affine_atom import AffAtom
  File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\affine\affine_atom.py",line 22,in <module>
    from cvxpy.cvxcore.python import canonInterface
  File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\cvxcore\python\__init__.py",line 3,in <module>
    import _cvxcore
ImportError: numpy.core.multiarray Failed to import

重现步骤:

1.) 在 Windows 10 下创建一个新的 Python 3.8 venv 并激活它

2.) 通过 requirements.txt 安装以下 pip install -r requirements.txt

cvxpy 
numpy~=1.19.2 # tensorflow 2.4.1 requires this version

3.) 通过 test.py

执行以下 python test.py
import cvxpy

if __name__ == ''__main__'':
    pass

如果我想使用 tensorflow 2.3,也会发生同样的事情。在这种情况下需要 numpy~=1.18,错误完全相同。

搜索错误发现很少的命中,可悲的是没有帮助我。

我该怎么做才能解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable

Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable

如何解决Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: ''numpy.ndarray'' object is not callable?

晚安, 尝试打印以下内容时,我在 jupyter 中遇到了 numpy 问题,并且得到了一个 错误: 需要注意的是python版本是3.8.8。 我先用 spyder 测试它,它运行正确,它给了我预期的结果

使用 Spyder:

import numpy as np
    for i in range (5):
        n = np.random.rand ()
    print (n)
Results
0.6604903457995978
0.8236300859753154
0.16067650689842816
0.6967868357083673
0.4231597934445466

现在有了 jupyter

import numpy as np
    for i in range (5):
        n = np.random.rand ()
    print (n)
-------------------------------------------------- ------
TypeError Traceback (most recent call last)
<ipython-input-78-0c6a801b3ea9> in <module>
       2 for i in range (5):
       3 n = np.random.rand ()
---->  4 print (n)

       TypeError: ''numpy.ndarray'' object is not callable

感谢您对我如何在 Jupyter 中解决此问题的帮助。

非常感谢您抽出宝贵时间。

阿特,约翰”

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

numpy.random.randn () 和 numpy.random.rand ()

numpy.random.randn () 和 numpy.random.rand ()

1 numpy.random.rand()

  (1)numpy.random.rand(d0,d1,…,dn)

    rand 函数根据给定维度生成 [0,1) 之间的数据,包含 0,不包含 1

    dn 表格每个维度

    返回值为指定维度的 array

  (2)

print(np.random.rand(2,4))
生成一个2行4列的0到1之间的数组

[[0.16965512 0.97445517 0.51992353 0.73377611]
[0.91446815 0.65995296 0.67720307 0.34809015]]

print(np.random.rand(4,3,2))

[[[0.10401912 0.82232074]
[0.68653479 0.07301172]
[0.59939558 0.58055146]]
[[0.03088151 0.88140311]
[0.4033945 0.47251058]
[0.2284928 0.70175964]]
[[0.44053464 0.20180619]
[0.15514924 0.90906066]
[0.17861751 0.68839029]]
[[0.31387288 0.90869563]
[0.14992 0.60987398]
[0.63666834 0.73750431]]]



 

2 numpy.random.randn()

numpy.random.randn(d0,d1,…,dn)

  • randn 函数返回一个或一组样本,具有标准正态分布。
  • dn 表格每个维度
  • 返回值为指定维度的 array
print(np.random.randn(2,4))

[[-3.76215048e-04 8.66687229e-01 -2.38559669e-01 1.75060171e+00]
[ 1.57466855e+00 8.17036401e-01 -1.05382851e+00 -1.72285071e+00]]

 

关于使用 Numpy PYthon 在值之间进行检查nums在python的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案、cvxpy 和 numpy 之间的版本冲突:“针对 API 版本 0xe 编译的模块,但此版本的 numpy 是 0xd”、Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable、numpy.random.randn () 和 numpy.random.rand ()等相关内容,可以在本站寻找。

本文标签: