Numpy is one of the most important and popular libraries in Python for numerical computation. It is widely used in data science and machine learning, a lot of libraries are built on top of it. I wish to share 7 Numpy tricks that I wish I had known earlier as a beginner in this post.

Numpy是Python中用于数值计算的最重要和最受欢迎的库之一。 它被广泛用于数据科学和机器学习中,并且在此基础上建立了许多库。 我希望分享7个Numpy技巧,我希望我早些时候在本文中早已知道这些技巧。

(1. numpy.linspace())

np.linespace(start, stop, num) return an array with the evenly spaced numbers from strat to stop

np.linespace(start, stop, num)返回一个数组,该数组具有从stratstop均匀间隔的数字

For example:

例如:


python做lstm模型代码 lstm python库_人工智能

It is convenient to draw math function:

绘制数学函数很方便:


python做lstm模型代码 lstm python库_java_02

(numpy.arange())

np.arange(start, stop, step) provides similar function, it creates an array from start to stop in step.

np.arange(start, stop, step)提供了类似的功能,它在step中np.arange(start, stop, step)创建一个数组。

For example:

例如:


python做lstm模型代码 lstm python库_python做lstm模型代码_03

(2. numpy.random)

It is quite often we need to generate random numbers for statical calculation. Numpy offers some functions to generate random numbers.

我们经常需要生成随机数以进行静态计算。 Numpy提供了一些生成随机数的功能。

(np.random.randint())

randint(low, high, size) generates an array (size=size) of random integers in the range (low — high).

randint(low, high, size)生成一个范围为(low — high)的随机整数数组(size = size)。


python做lstm模型代码 lstm python库_人工智能_04

(np.random.rand())

rand() generates random numbers uniformly distributed between 0 to 1 in a given shape.

rand()生成给定形状的均匀分布在0到1之间的随机数。


python做lstm模型代码 lstm python库_python_05

(np.random.randn())

randn() generates random numbers in a normal distribution.

randn()以正态分布生成随机数。


python做lstm模型代码 lstm python库_python做lstm模型代码_06

(np.random.choice())

random.choice() allows us to randomly choose samples from a given array. It is also possible to pass a probability.

random.choice()允许我们从给定数组中随机选择样本。 也可以传递概率。


python做lstm模型代码 lstm python库_python做lstm模型代码_07

(3. numpy.argmax())

np.argmax() returns the indices of the maximum values along an axis.

np.argmax()返回沿轴的最大值的索引。


python做lstm模型代码 lstm python库_java_08

It is useful in object classification and detection to find the object with the highest probability.

在对象分类和检测中找到概率最高的对象很有用。

There are also similar functions like argmin() , argwhere() , argpartition()

也有像类似的功能argmin() argwhere() argpartition()

(4. numpy.setdiff1d())

np.setdiff1d() returns the values in an array that are not in another array.

np.setdiff1d()返回数组中不在另一个数组中的值。

For example, we have two arrays:

例如,我们有两个数组:


python做lstm模型代码 lstm python库_leetcode_09

If we want to find the values in athat are not presented in b(the answer should be [1,2,3]), we can use setdiff1d() :

如果我们要寻找的值a未在呈现b (答案应该是[1,2,3]),我们可以使用setdiff1d()


python做lstm模型代码 lstm python库_python_10

We can also do it the other way around, finding values in b that are not presented in a:

我们也可以反过来做,找到b中没有出现在a中的值:


python做lstm模型代码 lstm python库_人工智能_11

(numpy.intersect1d())

One similar function is intersect1d() , it returns the intersection of 2 arrays, which is [4,5,6] in this case.

一个类似的函数是intersect1d() ,它返回2个数组的交集,在这种情况下为[4,5,6]。


python做lstm模型代码 lstm python库_java_12

(5. numpy.where)

np.where(condition,x,y) returns elements chosen from x or y depending on condition.

np.where(condition,x,y)返回根据条件从xy中选择的元素。

For example, we have an array containing exam scores:

例如,我们有一个包含考试成绩的数组:


python做lstm模型代码 lstm python库_java_13

We want to replace the scores by ‘pass’ or ‘not_pass’. The condition can be set as scores>60 :

我们想用“ pass”或“ not_pass”代替分数。 可以将条件设置为scores>60


python做lstm模型代码 lstm python库_人工智能_14

If the x and y are not passed to the np.where , the index position of the elements that meet the condition will be returned.

如果x和y没有传递到np.where ,则将返回满足条件的元素的索引位置。


python做lstm模型代码 lstm python库_python做lstm模型代码_15

(6. reshape())

Sometimes we need to reshape the array, we can use the resphape() method.

有时我们需要调整数组的resphape() ,可以使用resphape()方法。

For example, we have a one dimension array:

例如,我们有一个一维数组:


python做lstm模型代码 lstm python库_python做lstm模型代码_16

We can reshape it to a 2x5 array:

我们可以将其重塑为2x5数组:


python做lstm模型代码 lstm python库_java_17

We can use -1 , numpy calculates the dimension for you.

我们可以使用-1 ,numpy为您计算尺寸。


python做lstm模型代码 lstm python库_leetcode_18


(flatten())

If you want to reshape a multidimensional array to 1D array, you can use flatten()

如果要将多维数组重塑为一维数组,可以使用flatten()


python做lstm模型代码 lstm python库_java_19

(stack())

You can also stack multiple arrays in one array using np.stack() .

您还可以使用np.stack()多个阵列堆叠在一个阵列中。

For example:

例如:


python做lstm模型代码 lstm python库_java_20

You can do it among other axis,

您可以在其他轴之间进行操作,


python做lstm模型代码 lstm python库_java_21

You can also use hstack() to stack arrays horizontally:

您还可以使用hstack()水平堆叠数组:


python做lstm模型代码 lstm python库_人工智能_22

(7. numpy.clip())

If you have an array containing some numbers and a range, you can use clip() to limit the numbers to that range. For numbers outside the range, it returns the edge value.

如果您的数组包含一些数字和一个范围,则可以使用clip()将数字限制在该范围内。 对于超出范围的数字,它将返回边缘值。

For example:

例如:


python做lstm模型代码 lstm python库_人工智能_23

It clips the array between 3 to 5.

它将数组剪切在3到5之间。

That’s it. These Numpy tricks make my code a lot simpler and efficient. I hope these help you too

而已。 这些Numpy技巧使我的代码更加简单和高效。 希望这些对您有帮助

In my previous post, I also shared 7 Python tricks I wish I had known earlier that make my code better and smarter. You can take a look if you are interested.

在我以前的文章中,我还分享了我希望早些时候知道的7个Python技巧,这些技巧可以使我的代码更好,更聪明。 如果您有兴趣,可以看看。


Thanks for reading, happy coding.

感谢您阅读,编码愉快。


翻译自: https://towardsdatascience.com/7-numpy-tricks-to-make-my-code-better-and-smarter-9e8a4ccf43d1

lstm numpy代码