本文将介绍NumPy教程的详细情况,特别是关于第11章:数组操作的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于AnacondaNumpy错误“Im
本文将介绍NumPy 教程的详细情况,特别是关于第 11 章:数组操作的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于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 - 数组操作的知识。
本文目录一览:- NumPy 教程(第 11 章):数组操作(numpy数组用法)
- 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 - 数组操作
NumPy 教程(第 11 章):数组操作(numpy数组用法)
Numpy 中包含了一些函数用于处理数组,大概可分为以下几类:
-
修改数组形状
-
翻转数组
-
修改数组维度
-
连接数组
-
分割数组
-
数组元素的添加与删除
修改数组形状
-
reshape
不改变数据的条件下修改形状 -
flat
数组元素迭代器 -
flatten
返回一份数组拷贝,对拷贝所做的修改不会影响原始数组 -
ravel
返回展开数组
numpy.reshape
函数
可以在不改变数据的条件下修改形状,格式如下:
numpy.reshape(arr,newshape,order='C')
参数说明:
-
arr:要修改形状的数组
-
newshape:整数或者整数数组,新的形状应当兼容原有形状
-
order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序
示例:
In [1]: import numpy as np
In [2]: sum = np.arange(8)
In [3]: sum
Out[3]: array([0,1,2,3,4,5,6,7])
In [4]: sum.reshape(4,2)
Out[4]:
array([[0,1],[2,3],[4,5],[6,7]])
numpy.ndarray.flat
函数
是一个数组元素迭代器
In [1]: import numpy as np
In [2]: sum = np.arange(9).reshape(3,3)
In [3]: for row in sum:
...: print(row)
...:
[0 1 2]
[3 4 5]
[6 7 8]
In [4]: for element in sum.flat:
...: print(element)
...:
0
1
2
3
4
5
6
7
8
numpy.ndarray.flatten
函数
返回一份数组拷贝,对拷贝所做的修改不会影响原始数组,格式如下:
ndarray.flatten(order='C')
参数说明:
- order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘K’ – 元素在内存中的出现顺序
示例:
In [1]: import numpy as np
In [2]: sum = np.arange(8).reshape(2,4)
In [3]: sum
Out[3]:
array([[0,7]])
In [4]: sum.flatten()
Out[4]: array([0,7])
In [5]: sum.flatten(order='F')
Out[5]: array([0,7])
numpy.ravel
函数
展平的数组元素,顺序通常是"C风格",返回的是数组视图(view,有点类似 C/C++引用reference的意味),修改会影响原始数组
numpy.ravel(num,order='C')
参数说明:
- order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘K’ – 元素在内存中的出现顺序
示例:
In [1]: import numpy as np
In [2]: num = np.arange(8).reshape(2,4)
In [3]: num
Out[3]:
array([[0,7]])
In [4]: num.ravel()
Out[4]: array([0,7])
In [5]: num.ravel(order='F')
Out[5]: array([0,7])
翻转数组
-
transpose 对换数组的维度
-
ndarray.T 和 self.transpose() 相同
-
rollaxis 向后滚动指定的轴
-
swapaxes 对换数组的两个轴
numpy.transpose
函数
用于对换数组的维度,格式如下:
numpy.transpose(arr,axes)
-
arr:要操作的数组
-
axes:整数列表,对应维度,通常所有维度都会对换
示例:
In [1]: import numpy as np
In [2]: num = np.arange(12).reshape(3,4)
In [3]: num
Out[3]:
array([[ 0,[ 4,7],[ 8,9,10,11]])
In [4]: np.transpose(num)
Out[4]:
array([[ 0,8],[ 1,9],[ 2,10],[ 3,7,11]])
numpy.ndarray.T
类似 numpy.transpose
In [5]: num.T
Out[5]:
array([[ 0,11]])
numpy.rollaxis
函数
向后滚动特定的轴到一个特定位置,格式如下:
numpy.rollaxis(arr,axis,start)
参数说明:
-
arr:数组
-
axis:要向后滚动的轴,其它轴的相对位置不会改变
-
start:默认为零,表示完整的滚动。会滚动到特定位置
示例:
In [1]: import numpy as np
In [2]: num = np.arange(8).reshape(2,2)
In [3]: num
Out[3]:
array([[[0,3]],[[4,7]]])
In [4]: np.rollaxis(num,2)
Out[4]:
array([[[0,2],6]],[[1,[5,7]]])
In [5]: np.rollaxis(num,1)
Out[5]:
array([[[0,[1,6],7]]])
numpy.swapaxes
函数
用于交换数组的两个轴,格式如下:
numpy.swapaxes(arr,axis1,axis2)
参数说明:
-
arr:输入的数组
-
axis1:对应第一个轴的整数
-
axis2:对应第二个轴的整数
示例:
In [1]: import numpy as np
In [2]: num = np.arange(8).reshape(2,7]]])
In [4]: np.swapaxes(num,0)
Out[4]:
array([[[0,4],[3,7]]])
修改数组维度
-
broadcast 产生模仿广播的对象
-
broadcast_to 将数组广播到新形状
-
expand_dims 扩展数组的形状
-
squeeze 从数组的形状中删除一维条目
numpy.broadcast
函数
用于模仿广播的对象,它返回一个对象,该对象封装了将一个数组广播到另一个数组的结果,该函数使用两个数组作为输入参数,如下实例:
In [1]: import numpy as np
In [2]: x = np.array([[1],[2],[3]])
In [3]: y = np.array([4,6])
In [4]: x
Out[4]:
array([[1],[3]])
In [5]: y
Out[5]: array([4,6])
In [6]: num = np.broadcast(x,y)
In [7]: num
Out[7]: <numpy.broadcast at 0x7fac788ebce0>
In [8]: a,b = num.iters
In [9]: a
Out[9]: <numpy.flatiter at 0x7fac779ede00>
In [10]: b
Out[10]: <numpy.flatiter at 0x7fac779c9e00>
In [11]: print(next(a),next(b))
1 4
In [12]: print(next(a),next(b))
1 5
In [13]: num.shape
Out[13]: (3,3)
In [14]: num = np.broadcast(x,y)
In [15]: ape = np.empty(num.shape)
In [16]: ape
Out[16]:
array([[0.,0.,0.],[0.,0.]])
In [17]: ape.shape
Out[17]: (3,3)
In [18]: ape.flat = [i + j for i,j in num]
In [19]: ape
Out[19]:
array([[5.,6.,7.],[6.,7.,8.],[7.,8.,9.]])
In [20]: x + y
Out[20]:
array([[5,[7,8,9]])
numpy.broadcast_to
函数
将数组广播到新形状,它在原始数组上返回只读视图,它通常不连续。 如果新形状不符合 NumPy 的广播规则,该函数可能会抛出 ValueError
numpy.broadcast_to(array,shape,subok)
示例:
In [1]: import numpy as np
In [2]: num = np.arange(4).reshape(1,4)
In [3]: num
Out[3]: array([[0,3]])
In [4]: np.broadcast_to(num,(4,4))
Out[4]:
array([[0,
[0,3]])
numpy.expand_dims
函数
通过在指定位置插入新的轴来扩展数组形状,函数格式如下:
numpy.expand_dims(arr,axis)
参数说明:
-
arr:输入数组
-
axis:新轴插入的位置
示例:
In [1]: import numpy as np
In [2]: x = np.array(([1,4]))
In [3]: x
Out[3]:
array([[1,4]])
In [4]: y = np.expand_dims(x,axis=0)
In [5]: y
Out[5]:
array([[[1,4]]])
In [6]: x.shape,y.shape
Out[6]: ((2,2),(1,2))
In [7]: y = np.expand_dims(x,axis=1)
In [8]: y
Out[8]:
array([[[1,2]],[[3,4]]])
In [9]: x.ndim,y.ndim
Out[9]: (2,3)
In [10]: x.shape,y.shape
Out[10]: ((2,(2,2))
numpy.squeeze
函数
从给定数组的形状中删除一维的条目,函数格式如下:
numpy.squeeze(arr,axis)
参数说明:
-
arr:输入数组
-
axis:整数或整数元组,用于选择形状中一维条目的子集
示例:
In [1]: import numpy as np
In [2]: x = np.arange(9).reshape(1,3)
In [3]: x
Out[3]:
array([[[0,8]]])
In [4]: y = np.squeeze(x)
In [5]: y
Out[5]:
array([[0,8]])
In [6]: x.shape,y.shape
Out[6]: ((1,3),(3,3))
连接数组
-
concatenate 连接沿现有轴的数组序列
-
stack 沿着新的轴加入一系列数组
-
hstack 水平堆叠序列中的数组(列方向)
-
vstack 竖直堆叠序列中的数组(行方向)
numpy.concatenate
函数
用于沿指定轴连接相同形状的两个或多个数组,格式如下:
numpy.concatenate((a1,a2,...),axis)
参数说明:
-
a1,…:相同类型的数组
-
axis:沿着它连接数组的轴,默认为 0
示例:
In [1]: import numpy as np
In [2]: x = np.array([[1,4]])
In [3]: x
Out[3]:
array([[1,4]])
In [4]: y = np.array([[5,8]])
In [5]: y
Out[5]:
array([[5,8]])
In [6]: np.concatenate((x,y))
Out[6]:
array([[1,8]])
In [7]: np.concatenate((x,y),axis=0)
Out[7]:
array([[1,8]])
In [8]: np.concatenate((x,axis=1)
Out[8]:
array([[1,8]])
numpy.stack
函数
用于沿新轴连接数组序列,格式如下:
numpy.stack(arrays,axis)
参数说明:
-
arrays相同形状的数组序列
-
axis:返回数组中的轴,输入数组沿着它来堆叠
示例:
In [1]: import numpy as np
In [2]: x = np.array([[1,8]])
In [6]: np.stack((x,0)
Out[6]:
array([[[1,4]],[[5,8]]])
In [7]: np.stack((x,1)
Out[7]:
array([[[1,8]]])
numpy.hstack
函数
numpy.hstack 函数是 numpy.stack 函数的变体,它通过水平堆叠来生成数组
In [1]: import numpy as np
In [2]: x = np.array([[1,8]])
In [6]: np.hstack((x,8]])
numpy.vstack
函数
numpy.vstack 函数是 numpy.stack 函数的变体,它通过垂直堆叠来生成数组
In [1]: import numpy as np
In [2]: x = np.array([[1,8]])
In [6]: np.vstack((x,8]])
分割数组
-
split 将一个数组分割为多个子数组
-
hsplit 将一个数组水平分割为多个子数组(按列)
-
vsplit 将一个数组垂直分割为多个子数组(按行)
numpy.split
函数
沿特定的轴将数组分割为子数组,格式如下:
numpy.split(ary,indices_or_sections,axis)
参数说明:
-
ary:被分割的数组
-
indices_or_sections:果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭)
-
axis:沿着哪个维度进行切向,默认为0,横向切分。为1时,纵向切分
示例:
In [1]: import numpy as np
In [2]: num = np.arange(9)
In [3]: num
Out[3]: array([0,8])
In [4]: np.split(num,3)
Out[4]: [array([0,2]),array([3,5]),array([6,8])]
In [5]: np.split(num,7])
Out[5]: [array([0,3]),array([4,6]),array([7,8])]
numpy.hsplit
函数
用于水平分割数组,通过指定要返回的相同形状的数组数量来拆分原数组
In [1]: import numpy as np
In [2]: num = np.floor(10 * np.random.random((2,6)))
In [3]: num
Out[3]:
array([[8.,1.,2.],[3.,9.,5.,3.,5.]])
In [4]: np.hsplit(num,3)
Out[4]:
[array([[8.,9.]]),array([[6.,[5.,3.]]),array([[1.,[8.,5.]])]
numpy.vsplit
函数
沿着垂直轴分割,其分割方式与 hsplit 用法相同
In [1]: import numpy as np
In [2]: num = np.arange(16).reshape(4,11],[12,13,14,15]])
In [4]: np.vsplit(num,2)
Out[4]:
[array([[0,7]]),array([[ 8,15]])]
数组元素的添加与删除
-
resize 返回指定形状的新数组
-
append 将值添加到数组末尾
-
insert 沿指定轴将值插入到指定下标之前
-
delete 删掉某个轴的子数组,并返回删除后的新数组
-
unique 查找数组内的唯一元素
numpy.resize
函数
返回指定大小的新数组,如果新数组大小大于原始大小,则包含原始数组中的元素的副本
numpy.resize(arr,shape)
参数说明:
-
arr:要修改大小的数组
-
shape:返回数组的新形状
示例:
In [1]: import numpy as np
In [2]: x = np.array([[1,6]])
In [3]: x
Out[3]:
array([[1,6]])
In [4]: x.shape
Out[4]: (2,3)
In [5]: y = np.resize(x,2))
In [6]: y
Out[6]:
array([[1,6]])
In [7]: y.shape
Out[7]: (3,2)
In [8]: y = np.resize(x,3))
In [9]: y
Out[9]:
array([[1,3]])
numpy.append
函数
在数组的末尾添加值。 追加操作会分配整个数组,并把原来的数组复制到新数组中。 此外,输入数组的维度必须匹配否则将生成ValueError,append 函数返回的始终是一个一维数组
numpy.append(arr,values,axis=None)
参数说明:
arr:输入数组
-
values:要向arr添加的值,需要和arr形状相同(除了要添加的轴)
-
axis:默认为 None。当axis无定义时,是横向加成,返回总是为一维数组!当axis有定义的时候,分别为0和1的时候。当axis有定义的时候,分别为0和1的时候(列数要相同)。当axis为1时,数组是加在右边(行数要相同)
示例:
In [1]: import numpy as np
In [2]: num = np.array([[1,6]])
In [3]: num
Out[3]:
array([[1,6]])
In [4]: np.append(num,9])
Out[4]: array([1,9])
In [5]: np.append(num,[[7,9]],axis=0)
Out[5]:
array([[1,9]])
In [6]: np.append(num,axis=1)
Out[6]:
array([[1,9]])
numpy.insert
函数
在给定索引之前,沿给定轴在输入数组中插入值,如果值的类型转换为要插入,则它与输入数组不同。 插入没有原地的,函数会返回一个新数组。 此外,如果未提供轴,则输入数组会被展开
numpy.insert(arr,obj,axis)
参数说明:
-
arr:输入数组
-
obj:在其之前插入值的索引
-
values:要插入的值
-
axis:沿着它插入的轴,如果未提供,则输入数组会被展开
示例:
In [1]: import numpy as np
In [2]: num = np.array([[1,6]])
In [3]: num
Out[3]:
array([[1,
[3,
[5,6]])
In [4]: np.insert(num,[11,12])
Out[4]: array([ 1,11,12,6])
In [5]: np.insert(num,[11],axis=0)
Out[5]:
array([[ 1,
[11,
[ 3,
[ 5,6]])
In [6]: np.insert(num,axis=1)
Out[6]:
array([[ 1,6]])
numpy.delete
函数
返回从输入数组中删除指定子数组的新数组。 与 insert() 函数的情况一样,如果未提供轴参数,则输入数组将展开
Numpy.delete(arr,axis)
参数说明:
-
arr:输入数组
-
obj:可以被切片,整数或者整数数组,表明要从输入数组删除的子数组
-
axis:沿着它删除给定子数组的轴,如果未提供,则输入数组会被展开
示例:
In [1]: import numpy as np
In [2]: num = np.arange(12).reshape(3,11]])
In [4]: np.delete(num,5)
Out[4]: array([ 0,11])
In [5]: np.delete(num,axis=1)
Out[5]:
array([[ 0,11]])
In [6]: num = np.array([1,10])
In [7]: num
Out[7]: array([ 1,10])
In [8]: np.delete(num,np.s_[::2])
Out[8]: array([ 2,10])
numpy.unique
函数
用于去除数组中的重复元素
numpy.unique(arr,return_index,return_inverse,return_counts)
-
arr:输入数组,如果不是一维数组则会展开
-
return_index:如果为true,返回新列表元素在旧列表中的位置(下标),并以列表形式储
-
return_inverse:如果为true,返回旧列表元素在新列表中的位置(下标),并以列表形式储
-
return_counts:如果为true,返回去重数组中的元素在原数组中的出现次数
示例:
In [1]: import numpy as np
In [2]: num = np.array([5,9])
In [3]: num
Out[3]: array([5,9])
In [4]: np.unique(num)
Out[4]: array([2,9])
In [5]: np.unique(num,return_index=True)
Out[5]: (array([2,9]),array([1,9]))
In [6]: x,y = np.unique(num,return_index=True)
In [7]: y
Out[7]: array([1,9])
In [8]: np.unique(num,return_inverse=True)
Out[8]: (array([2,5]))
In [9]: x,return_inverse=True)
In [10]: x
Out[10]: array([2,9])
In [11]: y
Out[11]: array([1,5])
In [12]: x[y]
Out[12]: array([5,9])
In [13]: x,return_counts=True)
In [14]: x
Out[14]: array([2,9])
In [15]: y
Out[15]: array([3,1])
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”?
我正在尝试升级一些软件包并为现有的 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。 我先用 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 - 数组操作
NumPy - 数组操作
NumPy包中有几个例程用于处理ndarray
对象中的元素。 它们可以分为以下类型:
修改形状
序号 | 形状及描述 |
---|---|
1. | reshape 不改变数据的条件下修改形状 |
2. | flat 数组上的一维迭代器 |
3. | flatten 返回折叠为一维的数组副本 |
4. | ravel 返回连续的展开数组 |
numpy.reshape" rel="nofollow">numpy.reshape
这个函数在不改变数据的条件下修改形状,它接受如下参数:
numpy.reshape(arr, newshape, order'')
其中:
arr
:要修改形状的数组newshape
:整数或者整数数组,新的形状应当兼容原有形状order
:''C''
为 C 风格顺序,''F''
为 F 风格顺序,''A''
为保留原顺序。
例子
import numpy as np
a = np.arange(8) print ''原始数组:'' print a print ''\n'' b = a.reshape(4,2) print ''修改后的数组:'' print b
输出如下:
原始数组:
[0 1 2 3 4 5 6 7]
修改后的数组:
[[0 1]
[2 3]
[4 5]
[6 7]]
numpy.ndarray.flat" rel="nofollow">numpy.ndarray.flat
该函数返回数组上的一维迭代器,行为类似 Python 内建的迭代器。
例子
import numpy as np
a = np.arange(8).reshape(2,4) print ''原始数组:'' print a print ''\n'' print ''调用 flat 函数之后:'' # 返回展开数组中的下标的对应元素 print a.flat[5]
输出如下:
原始数组:
[[0 1 2 3]
[4 5 6 7]]
调用 flat 函数之后:
5
numpy.ndarray.flatten" rel="nofollow">numpy.ndarray.flatten
该函数返回折叠为一维的数组副本,函数接受下列参数:
ndarray.flatten(order)
其中:
order
:''C''
— 按行,''F''
— 按列,''A''
— 原顺序,''k''
— 元素在内存中的出现顺序。
例子
import numpy as np
a = np.arange(8).reshape(2,4) print ''原数组:'' print a print ''\n'' # default is column-major print ''展开的数组:'' print a.flatten() print ''\n'' print ''以 F 风格顺序展开的数组:'' print a.flatten(order = ''F'')
输出如下:
原数组:
[[0 1 2 3]
[4 5 6 7]]
展开的数组:
[0 1 2 3 4 5 6 7]
以 F 风格顺序展开的数组:
[0 4 1 5 2 6 3 7]
numpy.ravel" rel="nofollow">numpy.ravel
这个函数返回展开的一维数组,并且按需生成副本。返回的数组和输入数组拥有相同数据类型。这个函数接受两个参数。
numpy.ravel(a, order)
构造器接受下列参数:
order
:''C''
— 按行,''F''
— 按列,''A''
— 原顺序,''k''
— 元素在内存中的出现顺序。
例子
import numpy as np
a = np.arange(8).reshape(2,4) print ''原数组:'' print a print ''\n'' print ''调用 ravel 函数之后:'' print a.ravel() print ''\n'' print ''以 F 风格顺序调用 ravel 函数之后:'' print a.ravel(order = ''F'')
原数组:
[[0 1 2 3]
[4 5 6 7]]
调用 ravel 函数之后:
[0 1 2 3 4 5 6 7]
以 F 风格顺序调用 ravel 函数之后:
[0 4 1 5 2 6 3 7]
翻转操作
序号 | 操作及描述 |
---|---|
1. | transpose 翻转数组的维度 |
2. | ndarray.T 和self.transpose() 相同 |
3. | rollaxis 向后滚动指定的轴 |
4. | swapaxes 互换数组的两个轴 |
numpy.transpose" rel="nofollow">numpy.transpose
这个函数翻转给定数组的维度。如果可能的话它会返回一个视图。函数接受下列参数:
numpy.transpose(arr, axes)
其中:
arr
:要转置的数组axes
:整数的列表,对应维度,通常所有维度都会翻转。
例子
import numpy as np
a = np.arange(12).reshape(3,4) print ''原数组:'' print a print ''\n'' print ''转置数组:'' print np.transpose(a)
输出如下:
原数组:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
转置数组:
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
numpy.ndarray.T" rel="nofollow">numpy.ndarray.T
该函数属于ndarray
类,行为类似于numpy.transpose
。
例子
import numpy as np
a = np.arange(12).reshape(3,4) print ''原数组:'' print a print ''\n'' print ''转置数组:'' print a.T
输出如下:
原数组:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
转置数组:
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
numpy.rollaxis" rel="nofollow">numpy.rollaxis
该函数向后滚动特定的轴,直到一个特定位置。这个函数接受三个参数:
numpy.rollaxis(arr, axis, start)
其中:
arr
:输入数组axis
:要向后滚动的轴,其它轴的相对位置不会改变start
:默认为零,表示完整的滚动。会滚动到特定位置。
例子
# 创建了三维的 ndarray
import numpy as np
a = np.arange(8).reshape(2,2,2) print ''原数组:'' print a print ''\n'' # 将轴 2 滚动到轴 0(宽度到深度) print ''调用 rollaxis 函数:'' print np.rollaxis(a,2) # 将轴 0 滚动到轴 1:(宽度到高度) print ''\n'' print ''调用 rollaxis 函数:'' print np.rollaxis(a,2,1)
输出如下:
原数组:
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
调用 rollaxis 函数:
[[[0 2]
[4 6]]
[[1 3]
[5 7]]]
调用 rollaxis 函数:
[[[0 2]
[1 3]]
[[4 6]
[5 7]]]
numpy.swapaxes" rel="nofollow">numpy.swapaxes
该函数交换数组的两个轴。对于 1.10 之前的 NumPy 版本,会返回交换后数组的试图。这个函数接受下列参数:
numpy.swapaxes(arr, axis1, axis2)
arr
:要交换其轴的输入数组axis1
:对应第一个轴的整数axis2
:对应第二个轴的整数
# 创建了三维的 ndarray
import numpy as np
a = np.arange(8).reshape(2,2,2) print ''原数组:'' print a print ''\n'' # 现在交换轴 0(深度方向)到轴 2(宽度方向) print ''调用 swapaxes 函数后的数组:'' print np.swapaxes(a, 2, 0)
输出如下:
原数组:
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
调用 swapaxes 函数后的数组:
[[[0 4]
[2 6]]
[[1 5]
[3 7]]]
修改维度
序号 | 维度和描述 |
---|---|
1. | broadcast 产生模仿广播的对象 |
2. | broadcast_to 将数组广播到新形状 |
3. | expand_dims 扩展数组的形状 |
4. | squeeze 从数组的形状中删除单维条目 |
broadcast" rel="nofollow">broadcast
如前所述,NumPy 已经内置了对广播的支持。 此功能模仿广播机制。 它返回一个对象,该对象封装了将一个数组广播到另一个数组的结果。
该函数使用两个数组作为输入参数。 下面的例子说明了它的用法。
import numpy as np
x = np.array([[1], [2], [3]]) y = np.array([4, 5, 6]) # 对 y 广播 x b = np.broadcast(x,y) # 它拥有 iterator 属性,基于自身组件的迭代器元组 print ''对 y 广播 x:'' r,c = b.iters print r.next(), c.next() print r.next(), c.next() print ''\n'' # shape 属性返回广播对象的形状 print ''广播对象的形状:'' print b.shape print ''\n'' # 手动使用 broadcast 将 x 与 y 相加 b = np.broadcast(x,y) c = np.empty(b.shape) print ''手动使用 broadcast 将 x 与 y 相加:'' print c.shape print ''\n'' c.flat = [u + v for (u,v) in b] print ''调用 flat 函数:'' print c print ''\n'' # 获得了和 NumPy 内建的广播支持相同的结果 print ''x 与 y 的和:'' print x + y
输出如下:
对 y 广播 x:
1 4
1 5
广播对象的形状:
(3, 3)
手动使用 broadcast 将 x 与 y 相加:
(3, 3)
调用 flat 函数:
[[ 5. 6. 7.]
[ 6. 7. 8.]
[ 7. 8. 9.]]
x 与 y 的和:
[[5 6 7]
[6 7 8]
[7 8 9]]
numpy.broadcast_to" rel="nofollow">numpy.broadcast_to
此函数将数组广播到新形状。 它在原始数组上返回只读视图。 它通常不连续。 如果新形状不符合 NumPy 的广播规则,该函数可能会抛出ValueError
。
注意 - 此功能可用于 1.10.0 及以后的版本。
该函数接受以下参数。
numpy.broadcast_to(array, shape, subok)
例子
import numpy as np
a = np.arange(4).reshape(1,4) print ''原数组:'' print a print ''\n'' print ''调用 broadcast_to 函数之后:'' print np.broadcast_to(a,(4,4))
输出如下:
[[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]]
numpy.expand_dims" rel="nofollow">numpy.expand_dims
函数通过在指定位置插入新的轴来扩展数组形状。该函数需要两个参数:
numpy.expand_dims(arr, axis)
其中:
arr
:输入数组axis
:新轴插入的位置
例子
import numpy as np
x = np.array(([1,2],[3,4])) print ''数组 x:'' print x print ''\n'' y = np.expand_dims(x, axis = 0) print ''数组 y:'' print y print ''\n'' print ''数组 x 和 y 的形状:'' print x.shape, y.shape print ''\n'' # 在位置 1 插入轴 y = np.expand_dims(x, axis = 1) print ''在位置 1 插入轴之后的数组 y:'' print y print ''\n'' print ''x.ndim 和 y.ndim:'' print x.ndim,y.ndim print ''\n'' print ''x.shape 和 y.shape:'' print x.shape, y.shape
输出如下:
数组 x:
[[1 2]
[3 4]]
数组 y:
[[[1 2]
[3 4]]]
数组 x 和 y 的形状:
(2, 2) (1, 2, 2)
在位置 1 插入轴之后的数组 y:
[[[1 2]]
[[3 4]]]
x.shape 和 y.shape:
2 3
x.shape and y.shape:
(2, 2) (2, 1, 2)
numpy.squeeze" rel="nofollow">numpy.squeeze
函数从给定数组的形状中删除一维条目。 此函数需要两个参数。
numpy.squeeze(arr, axis)
其中:
arr
:输入数组axis
:整数或整数元组,用于选择形状中单一维度条目的子集
例子
import numpy as np
x = np.arange(9).reshape(1,3,3) print ''数组 x:'' print x print ''\n'' y = np.squeeze(x) print ''数组 y:'' print y print ''\n'' print ''数组 x 和 y 的形状:'' print x.shape, y.shape
输出如下:
数组 x:
[[[0 1 2]
[3 4 5]
[6 7 8]]]
数组 y:
[[0 1 2]
[3 4 5]
[6 7 8]]
数组 x 和 y 的形状:
(1, 3, 3) (3, 3)
数组的连接
序号 | 数组及描述 |
---|---|
1. | concatenate 沿着现存的轴连接数据序列 |
2. | stack 沿着新轴连接数组序列 |
3. | hstack 水平堆叠序列中的数组(列方向) |
4. | vstack 竖直堆叠序列中的数组(行方向) |
numpy.concatenate" rel="nofollow">numpy.concatenate
数组的连接是指连接。 此函数用于沿指定轴连接相同形状的两个或多个数组。 该函数接受以下参数。
numpy.concatenate((a1, a2, ...), axis)
其中:
a1, a2, ...
:相同类型的数组序列axis
:沿着它连接数组的轴,默认为 0
例子
import numpy as np
a = np.array([[1,2],[3,4]]) print ''第一个数组:'' print a print ''\n'' b = np.array([[5,6],[7,8]]) print ''第二个数组:'' print b print ''\n'' # 两个数组的维度相同 print ''沿轴 0 连接两个数组:'' print np.concatenate((a,b)) print ''\n'' print ''沿轴 1 连接两个数组:'' print np.concatenate((a,b),axis = 1)
输出如下:
第一个数组:
[[1 2]
[3 4]]
第二个数组:
[[5 6]
[7 8]]
沿轴 0 连接两个数组:
[[1 2]
[3 4]
[5 6]
[7 8]]
沿轴 1 连接两个数组:
[[1 2 5 6]
[3 4 7 8]]
numpy.stack" rel="nofollow">numpy.stack
此函数沿新轴连接数组序列。 此功能添加自 NumPy 版本 1.10.0。 需要提供以下参数。
numpy.stack(arrays, axis)
其中:
arrays
:相同形状的数组序列axis
:返回数组中的轴,输入数组沿着它来堆叠
import numpy as np
a = np.array([[1,2],[3,4]]) print ''第一个数组:'' print a print ''\n'' b = np.array([[5,6],[7,8]]) print ''第二个数组:'' print b print ''\n'' print ''沿轴 0 堆叠两个数组:'' print np.stack((a,b),0) print ''\n'' print ''沿轴 1 堆叠两个数组:'' print np.stack((a,b),1)
输出如下:
第一个数组:
[[1 2]
[3 4]]
第二个数组:
[[5 6]
[7 8]]
沿轴 0 堆叠两个数组:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
沿轴 1 堆叠两个数组:
[[[1 2]
[5 6]]
[[3 4]
[7 8]]]
numpy.hstack" rel="nofollow">numpy.hstack
numpy.stack
函数的变体,通过堆叠来生成水平的单个数组。
例子
import numpy as np
a = np.array([[1,2],[3,4]]) print ''第一个数组:'' print a print ''\n'' b = np.array([[5,6],[7,8]]) print ''第二个数组:'' print b print ''\n'' print ''水平堆叠:'' c = np.hstack((a,b)) print c print ''\n''
输出如下:
第一个数组:
[[1 2]
[3 4]]
第二个数组:
[[5 6]
[7 8]]
水平堆叠:
[[1 2 5 6]
[3 4 7 8]]
numpy.vstack" rel="nofollow">numpy.vstack
numpy.stack
函数的变体,通过堆叠来生成竖直的单个数组。
import numpy as np
a = np.array([[1,2],[3,4]]) print ''第一个数组:'' print a print ''\n'' b = np.array([[5,6],[7,8]]) print ''第二个数组:'' print b print ''\n'' print ''竖直堆叠:'' c = np.vstack((a,b)) print c
输出如下:
第一个数组:
[[1 2]
[3 4]]
第二个数组:
[[5 6]
[7 8]]
竖直堆叠:
[[1 2]
[3 4]
[5 6]
[7 8]]
数组分割
序号 | 数组及操作 |
---|---|
1. | split 将一个数组分割为多个子数组 |
2. | hsplit 将一个数组水平分割为多个子数组(按列) |
3. | vsplit 将一个数组竖直分割为多个子数组(按行) |
numpy.split" rel="nofollow">numpy.split
该函数沿特定的轴将数组分割为子数组。函数接受三个参数:
numpy.split(ary, indices_or_sections, axis)
其中:
ary
:被分割的输入数组indices_or_sections
:可以是整数,表明要从输入数组创建的,等大小的子数组的数量。 如果此参数是一维数组,则其元素表明要创建新子数组的点。axis
:默认为 0
例子
import numpy as np
a = np.arange(9) print ''第一个数组:'' print a print ''\n'' print ''将数组分为三个大小相等的子数组:'' b = np.split(a,3) print b print ''\n'' print ''将数组在一维数组中表明的位置分割:'' b = np.split(a,[4,7]) print b
输出如下:
第一个数组:
[0 1 2 3 4 5 6 7 8]
将数组分为三个大小相等的子数组:
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
将数组在一维数组中表明的位置分割:
[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]
numpy.hsplit" rel="nofollow">numpy.hsplit
numpy.hsplit
是split()
函数的特例,其中轴为 1 表示水平分割,无论输入数组的维度是什么。
import numpy as np
a = np.arange(16).reshape(4,4) print ''第一个数组:'' print a print ''\n'' print ''水平分割:'' b = np.hsplit(a,2) print b print ''\n''
输出:
第一个数组:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
水平分割:
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])]
numpy.vsplit" rel="nofollow">numpy.vsplit
numpy.vsplit
是split()
函数的特例,其中轴为 0 表示竖直分割,无论输入数组的维度是什么。下面的例子使之更清楚。
import numpy as np
a = np.arange(16).reshape(4,4) print ''第一个数组:'' print a print ''\n'' print ''竖直分割:'' b = np.vsplit(a,2) print b
输出如下:
第一个数组:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
竖直分割:
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])]
添加/删除元素
序号 | 元素及描述 |
---|---|
1. | resize 返回指定形状的新数组 |
2. | append 将值添加到数组末尾 |
3. | insert 沿指定轴将值插入到指定下标之前 |
4. | delete 返回删掉某个轴的子数组的新数组 |
5. | unique 寻找数组内的唯一元素 |
numpy.resize" rel="nofollow">numpy.resize
此函数返回指定大小的新数组。 如果新大小大于原始大小,则包含原始数组中的元素的重复副本。 该函数接受以下参数。
numpy.resize(arr, shape)
其中:
arr
:要修改大小的输入数组shape
:返回数组的新形状
例子
import numpy as np
a = np.array([[1,2,3],[4,5,6]]) print ''第一个数组:'' print a print ''\n'' print ''第一个数组的形状:'' print a.shape print ''\n'' b = np.resize(a, (3,2)) print ''第二个数组:'' print b print ''\n'' print ''第二个数组的形状:'' print b.shape print ''\n'' # 要注意 a 的第一行在 b 中重复出现,因为尺寸变大了 print ''修改第二个数组的大小:'' b = np.resize(a,(3,3)) print b
输出如下:
第一个数组:
[[1 2 3]
[4 5 6]]
第一个数组的形状:
(2, 3)
第二个数组:
[[1 2]
[3 4]
[5 6]]
第二个数组的形状:
(3, 2)
修改第二个数组的大小:
[[1 2 3]
[4 5 6]
[1 2 3]]
numpy.append" rel="nofollow">numpy.append
此函数在输入数组的末尾添加值。 附加操作不是原地的,而是分配新的数组。 此外,输入数组的维度必须匹配否则将生成ValueError
。
函数接受下列函数:
numpy.append(arr, values, axis)
其中:
arr
:输入数组values
:要向arr
添加的值,比如和arr
形状相同(除了要添加的轴)axis
:沿着它完成操作的轴。如果没有提供,两个参数都会被展开。
例子
import numpy as np
a = np.array([[1,2,3],[4,5,6]]) print ''第一个数组:'' print a print ''\n'' print ''向数组添加元素:'' print np.append(a, [7,8,9]) print ''\n'' print ''沿轴 0 添加元素:'' print np.append(a, [[7,8,9]],axis = 0) print ''\n'' print ''沿轴 1 添加元素:'' print np.append(a, [[5,5,5],[7,8,9]],axis = 1)
输出如下:
第一个数组:
[[1 2 3]
[4 5 6]]
向数组添加元素:
[1 2 3 4 5 6 7 8 9]
沿轴 0 添加元素:
[[1 2 3]
[4 5 6]
[7 8 9]]
沿轴 1 添加元素:
[[1 2 3 5 5 5]
[4 5 6 7 8 9]]
numpy.insert" rel="nofollow">numpy.insert
此函数在给定索引之前,沿给定轴在输入数组中插入值。 如果值的类型转换为要插入,则它与输入数组不同。 插入没有原地的,函数会返回一个新数组。 此外,如果未提供轴,则输入数组会被展开。
insert()
函数接受以下参数:
numpy.insert(arr, obj, values, axis)
其中:
arr
:输入数组obj
:在其之前插入值的索引values
:要插入的值axis
:沿着它插入的轴,如果未提供,则输入数组会被展开
例子
import numpy as np
a = np.array([[1,2],[3,4],[5,6]]) print ''第一个数组:'' print a print ''\n'' print ''未传递 Axis 参数。 在插入之前输入数组会被展开。'' print np.insert(a,3,[11,12]) print ''\n'' print ''传递了 Axis 参数。 会广播值数组来配输入数组。'' print ''沿轴 0 广播:'' print np.insert(a,1,[11],axis = 0) print ''\n'' print ''沿轴 1 广播:'' print np.insert(a,1,11,axis = 1)
numpy.delete" rel="nofollow">numpy.delete
此函数返回从输入数组中删除指定子数组的新数组。 与insert()
函数的情况一样,如果未提供轴参数,则输入数组将展开。 该函数接受以下参数:
Numpy.delete(arr, obj, axis)
其中:
arr
:输入数组obj
:可以被切片,整数或者整数数组,表明要从输入数组删除的子数组axis
:沿着它删除给定子数组的轴,如果未提供,则输入数组会被展开
例子
import numpy as np
a = np.arange(12).reshape(3,4) print ''第一个数组:'' print a print ''\n'' print ''未传递 Axis 参数。 在插入之前输入数组会被展开。'' print np.delete(a,5) print ''\n'' print ''删除第二列:'' print np.delete(a,1,axis = 1) print ''\n'' print ''包含从数组中删除的替代值的切片:'' a = np.array([1,2,3,4,5,6,7,8,9,10]) print np.delete(a, np.s_[::2])
输出如下:
第一个数组:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
未传递 Axis 参数。 在插入之前输入数组会被展开。
[ 0 1 2 3 4 6 7 8 9 10 11]
删除第二列:
[[ 0 2 3]
[ 4 6 7]
[ 8 10 11]]
包含从数组中删除的替代值的切片:
[ 2 4 6 8 10]
numpy.unique" rel="nofollow">numpy.unique
此函数返回输入数组中的去重元素数组。 该函数能够返回一个元组,包含去重数组和相关索引的数组。 索引的性质取决于函数调用中返回参数的类型。
numpy.unique(arr, return_index, return_inverse, return_counts)
其中:
arr
:输入数组,如果不是一维数组则会展开return_index
:如果为true
,返回输入数组中的元素下标return_inverse
:如果为true
,返回去重数组的下标,它可以用于重构输入数组return_counts
:如果为true
,返回去重数组中的元素在原数组中的出现次数
例子
import numpy as np
a = np.array([5,2,6,2,7,5,6,8,2,9]) print ''第一个数组:'' print a print ''\n'' print ''第一个数组的去重值:'' u = np.unique(a) print u print ''\n'' print ''去重数组的索引数组:'' u,indices = np.unique(a, return_index = True) print indices print ''\n'' print ''我们可以看到每个和原数组下标对应的数值:'' print a print ''\n'' print ''去重数组的下标:'' u,indices = np.unique(a,return_inverse = True) print u print ''\n'' print ''下标为:'' print indices print ''\n'' print ''使用下标重构原数组:'' print u[indices] print ''\n'' print ''返回去重元素的重复数量:'' u,indices = np.unique(a,return_counts = True) print u print indices
输出如下:
第一个数组:
[5 2 6 2 7 5 6 8 2 9]
第一个数组的去重值:
[2 5 6 7 8 9]
去重数组的索引数组:
[1 0 2 4 7 9]
我们可以看到每个和原数组下标对应的数值:
[5 2 6 2 7 5 6 8 2 9]
去重数组的下标:
[2 5 6 7 8 9]
下标为:
[1 0 2 0 3 1 2 4 0 5]
使用下标重构原数组:
[5 2 6 2 7 5 6 8 2 9]
返回唯一元素的重复数量:
[2 5 6 7 8 9]
[3 2 2 1 1 1]
关于NumPy 教程和第 11 章:数组操作的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于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 - 数组操作等相关知识的信息别忘了在本站进行查找喔。
本文标签: