纪录27个NumPy操作

作者: 开心人开发世界 | 来源:发表于2019-09-29 06:28 被阅读0次

介绍

在上一篇有关21 Pandas操作的文章中,我讨论了一些重要的操作,可以帮助新手开始进行数据分析。本文应该为NumPy提供类似的目的。简要介绍一下,NumPy是一个非常强大的库,可用于执行各种操作,从查找数组的均值到快速傅立叶变换和信号分析。从这个意义上讲,它与MATLAB非常相似。

您将需要将NumPy导入为'np',然后使用它来执行操作。

操作:

  1. 将列表转换为n维NumPy数组
numpy_array = np.array(list_to_convert)

2.使用np.newaxis和np.reshape

np.newaxis用于创建大小为1的新尺寸。

a = [1,2,3,4,5] is a list
a_numpy = np.array(a)

如果打印a_numpy.shape,您会得到(5,)。为了使它成为行向量或列向量,可以

row_vector = a_numpy[:,np.newaxis] ####shape is (5,1) now
col_vector = a_numpy[np.newaxis,:] ####shape is (1,5) now

类似地,np.reshape可以用来对任何数组进行整形。例如:

a = range(0,15) ####list of numbers from 0 to 14
b = a.reshape(3,5) 
b would become:
[[0,1,2,3,4],
 [5,6,7,8,9],
 [10,11,12,13,14],
 [15,16,17,18,19]]

3.将任何数据类型转换为NumPy数组

使用np.asarray。例如

a = [(1,2), [3,4,(5)], (6,7,8)]
b = np.asarray(a)
b::
array([(1, 2), list([3, 4, (5, 6)]), (6, 7, 8)], dtype=object)

4.获得零的n维数组。

a = np.zeros(shape,dtype=type_of_zeros)
type of zeros can be int or float as it is required
eg.
a = np.zeros((3,4), dtype = np.float16)

5.获得一个n维数组。

类似于np.zeros:

a = np.ones((3,4), dtype=np.int32)

6. np.full和np.empty

np.full用于获取由一个特定值填充的数组,而np.empty通过使用随机值初始化数组来帮助创建数组。例如。

1. np.full(shape_as_tuple,value_to_fill,dtype=type_you_want)
a = np.full((2,3),1,dtype=np.float16)
a would be:
array([[1., 1., 1.],
       [1., 1., 1.]], dtype=float16)
2. np.empty(shape_as_tuple,dtype=int)
a = np.empty((2,2),dtype=np.int16)
a would be:
array([[25824, 25701],
       [ 2606,  8224]], dtype=int16) 
The integers here are random.

7.使用np.arrange和np.linspace获取均匀间隔的值的数组

两者都可以用来创建具有均匀间隔的元素的数组。

linspace:

np.linspace(start,stop,num=50,endpoint=bool_value,retstep=bool_value)
endpoint specifies if you want the stop value to be included and retstep tells if you would like to know the step-value.'num' is the number of integer to be returned where 50 is default Eg,
np.linspace(1,2,num=5,endpoint=False,retstep=True)
This means return 5 values starting at 1 and ending befor 2 and returning the step-size.
output would be:
(array([1. , 1.2, 1.4, 1.6, 1.8]), 0.2) 
##### Tuple of numpy array and step-size

人气指数:

np.arange(start=where_to_start,stop=where_to_stop,step=step_size)

如果仅提供一个数字作为参数,则将其视为停止,如果提供2,则将其视为开始和停止。注意这里的拼写。

8.找到NumPy数组的形状

array.shape

9.了解NumPy数组的尺寸

x = np.array([1,2,3])
x.ndim will produce 1

10.查找NumPy数组中的元素数

x = np.ones((3,2,4),dtype=np.int16)
x.size will produce 24

11.获取n维数组占用的内存空间

x.nbytes
output will be 24*memory occupied by 16 bit integer = 24*2 = 48

12.在NumPy数组中查找元素的数据类型

x = np.ones((2,3), dtype=np.int16)
x.dtype will produce
dtype('int16')
It works better when elements in the array are of one type otherwise typecasting happens and result may be difficult to interpret.

13.如何创建NumPy数组的副本

使用np.copy

y = np.array([[1,3],[5,6]])
x = np.copy(y)
If,
x[0][0] = 1000
Then,
x is
100 3
5 6
y is
1 3
5 6

14.获取n-d数组的转置

使用array_name.T

x = [[1,2],[3,4]]
x
1 2
3 4
x.T is
1 3
2 4

15.展平n-d数组以获得一维数组

使用np.reshape和np.ravel:

np.reshape:这确实是一个不错的选择。在重塑时,如果您提供-1作为尺寸之一,则从no推断出来。的元素。例如。对于尺寸数组,(1,3,4)如果将其调整为,(-1,2,2),则第一维的长度计算为3。所以,

If x is:
1 2 3
4 5 9
Then x.reshape(-1) produces:
array([1, 2, 3, 4, 5, 9])

np.ravel

x = np.array([[1, 2, 3], [4, 5, 6]])
x.ravel() produces
array([1, 2, 3, 4, 5, 6])

16.更改n-d数组的轴或交换尺寸

使用np.moveaxis和np.swapaxes。

x = np.ones((3,4,5))
np.moveaxis(x,axes_to_move_as_list, destination_axes_as_list)
For eg.
x.moveaxis([1,2],[0,-2])
This means you want to move 1st axis to 0th axes and 2nd axes to 2nd last axis. So,the new shape would be.
(4,5,3)

转换没有到位,所以不要忘记将其存储在另一个变量中。

np.swapaxes。

x = np.array([[1,2],[3,4]])
x.shape is (2,2) and x is
1 2
3 4
np.swapaxes(x,0,1) will produce
1 3
2 4
If x = np.ones((3,4,5)), and
y = np.swapaxes(0,2)
y.shape will be
(5,4,3)

17.将NumPy数组转换为列表

x = np.array([[3,4,5,9],[2,6,8,0]])
y = x.tolist()
y will be
[[3, 4, 5, 9], [2, 6, 8, 0]]

NumPy文档提到,list(x)如果x是一维数组,使用也将起作用。

18.更改NumPy数组中元素的数据类型。

使用ndarray.astype

x = np.array([0,1,2.0,3.0,4.2],dtype=np.float32)
x.astype(np.int16) will produce
array([0, 1, 2, 3, 4], dtype=int16)
x.astype(np.bool) will produce 
array([False,  True,  True,  True,  True])

19.获取非零元素的索引

使用n-dim_array.nonzero()

x = np.array([0,1,2.0,3.0,4.2],dtype=np.float32)
x.nonzero() will produce
(array([1, 2, 3, 4]),) 
It's important to note that x has shape (5,) so only 1st indices are returned. If x were say,
x = np.array([[0,1],[3,5]])
x.nonzero() would produce 
(array([0, 1, 1]), array([1, 0, 1]))
So, the indices are actually (0,1), (1,0), (1,1). 

20.对NumPy数组进行排序

使用np.ndarray.sort(axis = axis_you_want_to_sort_by)

x = np.array([[4,3],[3,2])
x is
4 3
3 2
x.sort(axis=1) #sort each row
3 4
2 3
x.sort(axis=0) #sort each col
3 2
4 3

21.比较NumPy数组和值

比较将产生布尔类型的NumPy n维数组。例如

x = np.array([[0,1],[2,3]])
x==1 will produce
array([[False,  True],
       [False, False]])

如果您想计算x中的数字,您可以这样做

(x==1).astype(np.int16).sum()

它应该输出 1

22.乘以两个NumPy矩阵

使用numpy.matmul来获取二维矩阵的矩阵乘积:

a = np.eye(2) #identity matrix of size 2
a
1 0
0 1
b = np.array([[1,2],[3,4]])
b
1 2
3 4
np.matmul(a,b) will give
1 2
3 4

如果我们提供一维阵列,则输出将非常不同,因为将使用广播。我们在下面讨论。此外,还有另一个函数称为np.multiply执行元素到元素的乘法。对于前两个矩阵,输出为np.multiply(a,b)

1 0
0 4

23.两个阵列的点积

np.dot(矩阵1,矩阵2)

a = np.array([[1,2,3],[4,8,16]])
a:
1 2 3
4 8 16
b = np.array([5,6,11]).reshape(-1,1)
b:
5
6
11
np.dot(a,b) produces
38
160
Just like any dot product of a matrix with a column vector would produce.

行向量与列向量的点积将产生:

if a is array([[1, 2, 3, 4]])
and b is:
 
array([[4],
       [5],
       [6],
       [7]])
np.dot(a,b) gives:
array([[60]])
a's shape was (1,4) and b's shape was (4,1) so the result will have shape (1,1)

24.获得两个numpy向量的叉积

回想一下物理学中的矢量叉积。这是大约一个点的扭矩方向。

x = [1,2,3]
y = [4,5,6]
z = np.cross(x, y)
z is:
array([-3,  6, -3]) 

25.获取数组的梯度

使用np.gradient。NumPy使用泰勒级数和中心差法计算梯度。您可以在这篇文章中阅读有关它的更多信息

x = np.array([5, 10, 14, 17, 19, 26], dtype=np.float16)
np.gradient(x) will be:
array([5. , 4.5, 3.5, 2.5, 4.5, 7. ], dtype=float16)

26.如何切片NumPy数组?

For single element:
x[r][c] where r,c are row and col number of the element.
For slicing more than one element.
x:
2 4 9
3 1 5
7 8 0
and you want 2,4 and 7,8 then do
x[list_of_rows,list_of_cols] which would be
x[[0,0,2,2],[0,1,0,1]] produces
array([2, 4, 7, 8])
If one of the rows or cols are continuous, it's easier to do it:
x[[0,2],0:2] produces
array([[2, 4],
       [7, 8]])

27. broadcasting

如果不包括broadcasting,则有关NumPy的任何文章都将是不完整的。这是一个重要的概念,可帮助NumPy对操作进行向量化,从而使计算速度更快。理解一些规则将有助于更好地剖析广播。

来自NumPy文档:

在两个数组上进行操作时,NumPy逐元素比较其形状。它从尾随尺寸开始,一直向前发展。两种尺寸兼容

1.他们是平等的,或

2.其中之一是1

要记住的另一件事是,

如果尺寸匹配,则输出将在每个尺寸中具有最大长度。如果其中一个维度的长度为1,则将重复该维度中的值

假设有两个数组A和维度说明B **(3,4,5)****(4,1) **分别,你想添加的两个数组。由于它们的形状不同,因此NumPy将尝试广播这些值。它开始比较两个尺寸的最后一个维度的长度:5和1,这些值不相等,但是由于其中一个值为1,因此将重复该值,最终输出在最后一个维度中的长度为5 。

两者的倒数第二个长度相同4

A中的最后3维或1维具有长度,3而B没有任何长度。当其中一个向量缺少维度时,NumPy在向量前加1。因此,B变为**(1,4,1)**现在,长度匹配为3和1,并且在B中将值重复3次。最终输出将具有shape **(3,4,5)**

a :
3 5 8
4 5 6
9 7 2
b :
b = [2,3,4]
a's shape: (3,3)
b's shape: (3,)
Last dimension has same length 3 for the two and then 1 is prepended to the the dimension of b as it doesn't have anything in that dimension. So, b becomes [[2,3,4]] having shape (1,3). Now, 1st dimension match and values are repeated for b. 
Finally, b can be seen as
2 3 4
2 3 4
2 3 4
So, a+b produces
5 8 12
6 8 10
11 10 6

查看broadcasting中的这些帖子以了解更多信息:第一第二

总结

感谢您的阅读。我希望本文能为需要NumPy入门的任何人提供帮助。我发现这些操作非常有帮助,将它们放在我们的提示上总是一件好事。这些操作非常基础,NumPy中可能有不同的方法来实现相同的目标。除了提供了链接的其他几篇文章外,我主要使用NumPy文档作为参考。

翻译自:https://towardsdatascience.com/27-things-that-a-beginner-needs-to-know-about-numpy-edda217fb662

相关文章

  • 纪录27个NumPy操作

    介绍 在上一篇有关21 Pandas操作的文章中,我讨论了一些重要的操作,可以帮助新手开始进行数据分析。本文应该为...

  • Numpy和Pandas基本操作速查

    """ numpy 基本操作 """'''安装 Numpy 的方法:pip install numpy''''''...

  • pandas numpy

    pandas 时间序列操作 python numpy教程 Numpy中矩阵对象(matrix) numpy中的数据...

  • Python中的数组库——Numpy的应用

    Numpy 的主要用途是以数组的形式进行数据操作。 机器学习中大多数操作都是数学操作,而 Numpy 使这些操作变...

  • 一份精简的Numpy使用指引(附python演练)

    Numpy 的主要用途是以数组的形式进行数据操作。 机器学习中大多数操作都是数学操作,而 Numpy 使这些操作变...

  • Numpy | 基础操作(矩阵)

    NumPy 基础操作 什么是 NumPy NumPy是Python中科学计算的基础包。它是一个Python库,提供...

  • numpy回顾

    翻了下之前写的关于Numpy的文章,回顾下: numpy手册(2)-常用操作杂记 numpy手册(3)-Datet...

  • 一文搞定Numpy&Pandas

    简单来说,Numpy就是用来进行矩阵计算的,而Pandas则基于Numpy,丰富并简化了Numpy的操作 Nump...

  • NumPy索引操作

    NumPy数组中对索引的操作

  • numpy[2]

    numpy1numpy2numpy3 數組的操作 基本索引array[ ] 一. [一維(由0開始數)][二維(...

网友评论

    本文标题:纪录27个NumPy操作

    本文链接:https://www.haomeiwen.com/subject/szlouctx.html