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

What are the efficient techniques for using Numpy and Pandas

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "what are the efficient techniques for using Numpy and Pandas". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Six efficient functions of Numpy

Let's start with Numpy. Numpy is a Python language extension package for scientific computing, which usually contains powerful N-dimensional array objects, complex functions, tools for integrating Cmax Cure + and Fortran code, and useful linear algebra, Fourier transform, and random number generation capabilities.

In addition to these obvious uses, Numpy can also be used as an efficient multidimensional container (container) for general-purpose data, defining any data type. This enables Numpy to integrate itself with various databases seamlessly and quickly.

Next, six Numpy functions are parsed one by one.

Argpartition ()

With the help of argpartition (), Numpy can find the indexes with N maximum values and output the indexes found. Then we sort the values as needed.

X = np.array ([12, 10, 12, 0, 6, 8, 9, 1, 16, 4, 6, 0]) index_val = np.argpartition (x,-4) [- 4:] index_val array ([1,8,2,0], dtype=int64) np.sort (x [index _ val]) array ([10,12,12,16])

Allclose ()

Allclose () is used to match two arrays and get the output represented by a Boolean value. If two arrays are not equal within one tolerance (within a tolerance), allclose () returns False. This function is useful for checking whether two arrays are similar.

Array1 = np.array ([0.12 array1,array2,0.1 0.17 False# with a tolerance of 0.24 array1,array2,0.2 0.29]) array2 = np.array ([0.13 record0.19 0.26 meme 0.31]) # with a tolerance of 0.1, it should return False: np.allclose (array1,array2,0.1) 0.20, it should return True: np.allclose (array1,array2,0.2) True

Clip ()

Clip () keeps the values in an array within an interval. Sometimes we need to make sure that the value is within the upper and lower limits. To do this, we can do this with the help of Numpy's clip () function. Given an interval, the values outside the interval are cut to the upper and lower bound of the interval (interval edge).

X = np.array ([3, 17, 14, 23, 2, 2, 6, 8, 1, 2, 16, 0]) np.clip (XL2) array ([3, 5, 5, 5, 2, 2, 2))

Extract ()

As the name implies, extract () extracts specific elements from an array under certain conditions. With the help of extract (), we can also use conditions such as and and or.

# Random integers array = np.random.randint (20, size=12) array array ([0, 1, 8, 19, 16, 18, 10, 11, 2, 13, 14, 3]) # Divide by 2 and check if remainder is 1 cond = np.mod (array, 2) = 1 cond array ([False, True, False, True]) # Use extract to get the values np.extract (cond, array) array ([1,19,11,13) 3]) # Apply condition on extract directly np.extract ((array)

< 3) | (array >

15), array) array ([0, 1, 19, 16, 18, 2])

Where ()

Where () is used to return elements from an array that meet specific conditions. For example, it returns the index position of a numeric value that meets a specific condition. Where () is similar to the where condition used in SQL, as shown in the following example:

Y = np.array ([1 Where y is greater than 5, returns index position np.where (y > 5) array ([2, 3, 5, 7, 8], dtype=int64),) # First will replace the values that match the condition, # second will replace the values that does not np.where (y > 5, "Hit", "Miss") array ([Miss, Miss, Hit, Hit, Miss, Hit, Miss, Hit, Hit], dtype=

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