In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the difference between ravel () and flatten () in Numpy. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The operation that is often used in Numpy is flattened. Numpy provides two functions to do this. They have the same function, but are very different in memory.
Let's take a look at the use of these two functions:
From numpy import * a = arange (12). Reshape (3) print (a) # [[0 123] # [4 567] # [8 9 10 11] print (a.ravel ()) # [0 12 3 4 5 6 7 8 9 10 11] print (a.flatten ()) # [0 12 3 4 5 6 7 8 9 10 11]
You can see that the two functions perform the same function, but flatten () is more appropriate when we use it. Flatten () allocates new memory during use, but ravel () returns a view of an array. A view is a reference to an array (it is not appropriate to say that the reference is not the same as the address of the array returned by ravel ()). In the process of use, care should be taken to avoid affecting the original array when modifying the view. What does this mean? let's explain it in detail through the code:
From numpy import * a = arange (12). Reshape (3) print (a) # [[0 12 3] # [4 567] # [8 9 10 11] # create an array bb = a.copy () c = a.ravel () d = b.flatten () # output c and d array print (c) # [0 12 3 4 5 6 7 8 9 10 11] print (d) # [0 12 12 3 4 5 6 7 8 9 10 11] # you can see that the c and d arrays are flattened arrays With the same content print (an is c) # Falseprint (b is d) # False#, you can see that the above arecalogy bpencil cpene d is four different objects # but because c is a way of displaying a, although they are different objects, when you modify c The corresponding number in an also changed c [1] = 99d [1] = 99print (a) # [[0 99 23] # [4 567] # [8 9 10 11] print (b) # [[0 1 23] # [4 5 6 7] # [8 9 10 11]] print (c) # [0 99 2 3 4 5 6 7 8 9 10 11] print (d) # [0 99 2 3 4 5 6 7 8 9 10 11] This is the difference between ravel () and flatten () in Numpy shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.