In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the use of NumPy". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of NumPy"?
Create an array
We can create a NumPy array (that is, the powerful ndarray) by passing a python list and using 'np.array ()'. In this case, python creates an array that we can see here:
Normally, we want NumPy to initialize the values of the array for us. NumPy provides methods like ones (), zeros (), and random.random () for these situations. We just pass them the number of elements we want to generate:
Once we have created the arrays, we can start to manipulate them in interesting ways.
Array operation
Let's create two NumPy arrays to demonstrate their usefulness. We call them data and ones:
Adding them by position (that is, adding the value of each row) is as simple as typing data + ones:
When I started to learn these tools, I found that such abstraction freed me from having to write such computing programs in loops, which was refreshing to me. This is a good abstract concept that allows you to think at a higher level.
We can also do this:
Typically, we want to perform an operation between an array and a single number (we can also call this an operation between a vector and a scalar). For example, our array represents the distance in miles, and we want to convert it to kilometers. Let's simply say data* 1.6:
See how NumPy understands this operation? This concept is called broadcasting, and it is very useful.
Indexes
We can index and slice all the methods of the NumPy array, and we can slice the python list:
Polymerization
The added benefit of NumPy is the aggregation feature:
In addition to the minimum, maximum, and summation, you can also use other aggregate functions, such as mean to get the average, prod to get the multiplication of all elements, std to get the standard deviation, and many others.
A higher dimension
All the examples we've seen are about one-dimensional vectors. A key part of NumPy is its ability to apply everything we've seen so far to any number of dimensions.
Create a matrix
We can pass the shape of the python list as follows, and let NumPy create a matrix to represent them:
Np.array ([[1, 2], [3, 4]])
We can also use the methods mentioned above (ones (), zero (), and random.random ()), as long as we give them a tuple to describe the dimension of the matrix we are creating:
Matrix operation
If the two matrices are of the same size, we can use the arithmetic operator (+-* /) to add and multiply the matrix. NumPy treats these operations as location operations:
Only when different dimensions are 1 (for example, a matrix has only one column or row) can we perform these arithmetic operations on matrices of different sizes, in which case NumPy uses its broadcast rules to do so:
Point multiplication
A key difference in arithmetic is the use of point multiplication and matrix multiplication. NumPy gives each matrix a dot multiplication dot () method, which we can use to perform dot product operations with other matrices:
I added a matrix dimension to the bottom of the graph to emphasize that the two matrices must have the same dimension on the side of each other. You can think of this operation as this:
Matrix aggregation
We can aggregate the matrix like an aggregation vector:
We can not only aggregate all the values in the matrix, but also use the axis parameter to aggregate across rows or columns:
Thank you for your reading, the above is the content of "what is the use of NumPy?" after the study of this article, I believe you have a deeper understanding of the use of NumPy, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.