lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

lstm numpy代码

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() (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:

例如:

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

It is convenient to draw math function:

绘制数学函数很方便:

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

numpy.arange() (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:

例如:

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

2. numpy.random (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() (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)。

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

np.random.rand() (np.random.rand())

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

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

np.random.randn() (np.random.randn())

randn() generates random numbers in a normal distribution.

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

np.random.choice() (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()允许我们从给定数组中随机选择样本。 也可以传递概率。

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

3. numpy.argmax() (3. numpy.argmax())

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

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

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() (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:

例如,我们有两个数组:

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

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()

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

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

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

numpy.intersect1d() (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]。

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

5. numpy.where (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:

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

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

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

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 ,则将返回满足条件的元素的索引位置。

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

6. reshape() (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:

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

We can reshape it to a 2x5 array:

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

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

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明
lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

展平() (flatten())

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

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

stack() (stack())

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

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

For example:

例如:

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

You can do it among other axis,

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

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

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

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

7. numpy.clip() (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:

例如:

lstm numpy代码_7个Numpy技巧使我的代码更好,更聪明

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代码