Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Numpy more efficiently

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge points about how to use Numpy more efficiently, with detailed content and clear logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Use the argument-1 in the reshape function

Numpy allows us to reshape the matrix based on a given new shape, which should be compatible with the original shape. Interestingly, we can assign a parameter in the new shape to-1. This simply indicates that it is an unknown dimension, and we want Numpy to figure out what the unknown dimension should be: Numpy will ensure that it meets the above criteria by looking at the length and remaining dimensions of the array. Let's look at the following examples:

Diagram of different reshape operations with dimension-1.

A = np.array ([1, 2, 3, 4], [5, 6, 7, 8]) a.shape (2, 4)

Assuming that we give a row parameter of 1 and a column parameter of-1, then Numpy will calculate the number of columns after reshape as 8.

A.reshape (1) array ([1, 2, 3, 4, 5, 6, 7, 8])

Assuming that we give a row parameter of-1 and a column parameter of 1, then Numpy will calculate the number of rows after reshape as 8.

A.reshape (- 1) array ([[1], [2], [3], [4], [5], [6], [7], [8]])

The same is true of the following code.

A.reshape (- 1pr 4) array ([[1Jer 2,3,4], [5,6,7,8]]) a.reshape (- 1JEI 2) array ([[1JEO 2], [3,4], [5,6], [7,8]) a.reshape (2MET 1) array ([[1Met 2,3,4], [5,6,7]) 8]]) a.reshape (4) array ([[1,2], [3,4], [5,6], [7,8]])

This also applies to the reshape of any higher dimensional tensor, but only one dimension's parameter can be assigned to-1.

A.reshape (2, 2) array ([1,2], [3,4]], [[5,6], [7,8]) a.reshape ([1], [2], [3], [4]], [5], [6], [7]) [8])

If we try an reshape incompatible shape or given more than one unknown dimension parameter, an error will be reported.

A.reshape (- 1) ValueError: can only specify one unknown dimensiona.reshape (3) ValueError: cannot reshape array of size 8 into shape (3)

All in all, when trying to reshape a tensor, the new shape must contain the same number of elements as the old shape, which means that the dimensional product of the two shapes must be equal. When the-1 parameter is used, the dimension corresponding to-1 will be the dimension of the original array divided by the product of the dimension given in the new shape in order to maintain the same number of elements.

Argpartition: find the largest N elements in the array.

Numpy's argpartion function can efficiently find the index of N maximum values and return N values. After the index is given, we can sort the values as needed.

Array = np.array ([10, 7, 4, 3, 2, 2, 5, 9, 0, 4, 6, 0]) index = np.argpartition* (array,-5) [- 5:] index array ([6, 1, 10, 7, 0], dtype=int64) np.sort (array [index]) array ([5, 6, 7, 9, 10]) Clip: how to keep the values in an array within a certain range

In many data processing and algorithms (such as PPO in reinforcement learning), we need to keep all values within an upper and lower bound. Numpy's built-in Clip function can solve this problem. The Numpy clip () function is used to restrict values in an array. Given an interval range, the values outside the interval range will be truncated to the boundary of the interval. For example, if the specified interval is [- 1 minute 1], values less than-1 will become-1, and values greater than 1 will become 1.

Clip example: the minimum value in the restricted array is 2 and the maximum value is 6.

# Example-1 array = np.array ([10, 7, 4, 3, 2, 2, 5, 9, 0, 4, 6, 0]) print (np.clip (array,2,6)) [6 6 4 3 2 5 5 6 2 4 62] # Example-2 array = np.array ([10,-1, 4,-3, 2, 2, 5, 9, 0, 4, 6, 0]) print (array) Extract: extract eligible elements from the array

We can use the Numpy extract () function to extract specific elements that match the criteria from the array.

Arr = np.arange (10) arrarray ([0,1,2,3,4,5,6,7,8,9]) # Define the codition, here we take MOD 3 if zero condition = np.mod (arr, 3) = = 0 conditionarray ([True, False, False,True]) np.extract (condition, arr) array ([0,3,6,9])

Similarly, if necessary, we can use the direct condition of the combination of AND and OR, as follows:

Np.extract ((arr > 2) & (arr < 8), arr) array ([3,4,5,6,7]) setdiff1d: how to find elements that only exist in array A but not in array B.

Returns unique elements in an array that are not in another array. This is equivalent to the difference between two sets of array elements.

A = np.array ([1, 2, 3 Numpy 4, 5, 6) b = np.array ([3, 4, 4, 7, 7)) c = np.setdiff1d (a) carray ([1, 2, 5, 9]) is all the content of this article "how to use Numpy more efficiently". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report