介绍
在上一篇有关21 Pandas操作的文章中,我讨论了一些重要的操作,可以帮助新手开始进行数据分析。本文应该为NumPy提供类似的目的。简要介绍一下,NumPy是一个非常强大的库,可用于执行各种操作,从查找数组的均值到快速傅立叶变换和信号分析。从这个意义上讲,它与MATLAB非常相似。
您将需要将NumPy导入为'np',然后使用它来执行操作。
操作:
numpy_array = np.array(list_to_convert)
2.使用np.newaxis和np.reshape
np.newaxis用于创建大小为1的新尺寸。
a = [1,2,3,4,5] is a lista_numpy = np.array(a)
如果打印a_numpy.shape,您会得到(5,)。为了使它成为行向量或列向量,可以
row_vector = a_numpy[:,np.newaxis] ####shape is (5,1) nowcol_vector = a_numpy[np.newaxis,:] ####shape is (1,5) now
类似地,np.reshape可以用来对任何数组进行整形。例如:
a = range(0,15) ####list of numbers from 0 to 14b = 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 requiredeg.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.nbytesoutput 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 producedtype('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] = 1000Then,x is100 35 6y is1 35 6
14.获取n-d数组的转置
使用array_name.T
x = [[1,2],[3,4]]x1 23 4x.T is1 32 4
15.展平n-d数组以获得一维数组
使用np.reshape和np.ravel:
np.reshape:这确实是一个不错的选择。在重塑时,如果您提供-1作为尺寸之一,则从no推断出来。的元素。例如。对于尺寸数组,(1,3,4)如果将其调整为,(-1,2,2),则第一维的长度计算为3。所以,
If x is:1 2 34 5 9Then x.reshape(-1) produces:array([1, 2, 3, 4, 5, 9])
np.ravel
x = np.array([[1, 2, 3], [4, 5, 6]])x.ravel() producesarray([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 is1 23 4np.swapaxes(x,0,1) will produce1 32 4If x = np.ones((3,4,5)), andy = 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 producearray([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 is4 33 2x.sort(axis=1) #sort each row3 42 3x.sort(axis=0) #sort each col3 24 3
21.比较NumPy数组和值
比较将产生布尔类型的NumPy n维数组。例如
x = np.array([[0,1],[2,3]])x==1 will producearray([[False, True], [False, False]])
如果您想计算x中的数字,您可以这样做
(x==1).astype(np.int16).sum()
它应该输出 1
22.乘以两个NumPy矩阵
使用numpy.matmul来获取二维矩阵的矩阵乘积:
a = np.eye(2) #identity matrix of size 2a1 00 1b = np.array([[1,2],[3,4]])b1 23 4np.matmul(a,b) will give1 23 4
如果我们提供一维阵列,则输出将非常不同,因为将使用广播。我们在下面讨论。此外,还有另一个函数称为np.multiply执行元素到元素的乘法。对于前两个矩阵,输出为np.multiply(a,b)。
1 00 4
23.两个阵列的点积
np.dot(矩阵1,矩阵2)
a = np.array([[1,2,3],[4,8,16]])a:1 2 34 8 16b = np.array([5,6,11]).reshape(-1,1)b:5611np.dot(a,b) produces38160Just 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 93 1 57 8 0and you want 2,4 and 7,8 then dox[list_of_rows,list_of_cols] which would bex[[0,0,2,2],[0,1,0,1]] producesarray([2, 4, 7, 8])If one of the rows or cols are continuous, it's easier to do it:x[[0,2],0:2] producesarray([[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 84 5 69 7 2b :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 as2 3 42 3 42 3 4So, a+b produces5 8 126 8 1011 10 6
查看broadcasting中的这些帖子以了解更多信息:第一和第二
总结
感谢您的阅读。我希望本文能为需要NumPy入门的任何人提供帮助。我发现这些操作非常有帮助,将它们放在我们的提示上总是一件好事。这些操作非常基础,NumPy中可能有不同的方法来实现相同的目标。除了提供了链接的其他几篇文章外,我主要使用NumPy文档作为参考。
翻译自:https://towardsdatascience.com/27-things-that-a-beginner-needs-to-know-about-numpy-edda217fb662