1. 程式人生 > 實用技巧 >lstm numpy程式碼_7個Numpy技巧使我的程式碼更好,更聰明

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:

例如:

Image for post

It is convenient to draw math function:

繪製數學函式很方便:

Image for post

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:

例如:

Image for post

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

Image for post

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

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

rand()生成給定形狀的均勻分佈在0到1之間的隨機數。

Image for post

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

randn() generates random numbers in a normal distribution.

randn()以正態分佈生成隨機數。

Image for post

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()允許我們從給定陣列中隨機選擇樣本。 也可以傳遞概率。

Image for post

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

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

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

Image for post

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:

例如,我們有兩個陣列:

Image for post

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

Image for post

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

我們也可以反過來做,找到b中沒有出現在a中的值:

Image for post

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]。

Image for post

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:

例如,我們有一個包含考試成績的陣列:

Image for post

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

我們想用“ pass”或“ not_pass”代替分數。 可以將條件設​​置為scores>60

Image for post

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 ,則將返回滿足條件的元素的索引位置。

Image for post

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:

例如,我們有一個一維陣列:

Image for post

We can reshape it to a 2x5 array:

我們可以將其重塑為2x5陣列:

Image for post

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

我們可以使用-1 ,numpy為您計算尺寸。

Image for post
Image for post

展平() (flatten())

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

如果要將多維陣列重塑為一維陣列,可以使用flatten()

Image for post

stack() (stack())

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

您還可以使用np.stack()多個陣列堆疊在一個陣列中。

For example:

例如:

Image for post

You can do it among other axis,

您可以在其他軸之間進行操作,

Image for post

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

您還可以使用hstack()水平堆疊陣列:

Image for post

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:

例如:

Image for post

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程式碼