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 > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "detailed explanation of Numpy library in Python". In the operation of actual 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!
First of all, I need to understand what Numpy is. From my impression, it is generally mentioned that this tool is associated with machine learning. Of course, there is a lot of gap between the actual understanding and the actual understanding.
To put it simply, Python itself is not designed as a scientific computing language, but with the popularity of Python and a large number of expansion, the demand for this area will be increasing, so there is Numpy as a supplement.
You can use pip to download Numpy, and pip install numpy can be done with one command, about more than 10 M
The environment of Numpy is much easier to build than TensorFlow, and the way of verification is very simple, import is fine.
> import numpy
If you want to view the version, you can use the following ways.
> numpy.version.full_version
'1.13.3'
> > >
Of course, we can specify aliases, which is easier to use, as we have done in subsequent tests.
> import numpy as np
> np.version.full_version
'1.13.3'
Let's warm up first.
Get a string of integers in the range of 10, an array to be exact.
> a = np.arange (10)
> print a
[0 1 2 3 4 5 6 7 8 9]
Print it out and see it's an array.
> > type (a)
Then we change the pattern and transform the array into two parts, each of which is five.
> a = a.reshape (2BI 5)
> print a
[[0 1 2 3 4]
[5 6 7 8 9]]
You can continue to ReFactor, recompose 20 elements, and then divide them into four parts, each with five elements.
> a = np.arange (20)
> a = a.reshape (4pm 5)
> print a
[[0 1 2 3 4]
[5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
You can also construct high-dimensional arrays, such as the following.
> a = a.reshape (2pm 2pm 5)
> print a
[0 1 2 3 4]
[5 6 7 8 9]]
[[10 11 12 13 14]
[15 16 17 18 19]
> > >
Not if the input (2, 2, 2, 6) is not allowed, the obvious 2, 2, 2, 6, 24, has overflowed.
> a = a.reshape (2pm 2pm 6)
Traceback (most recent call last):
File "", line 1, in
ValueError: cannot reshape array of size 20 into shape (2mai 2pm 6)
> > >
We get the dimensions of the array
> a.ndim
three
Get the size of each dimension of the array
> a.shape
(2L, 2L, 5L)
View the number of elements in the array
> a.size
twenty
Then we create an array that can convert the list to get
> < raw >
> a = np.array (raw)
> > a
Array ([0,1,2,3,4])
> print a
[0 1 2 3 4]
It's the same operation for a slightly more complicated one.
> raw = [0pr 1, 2, 3, 4], [5, 6, 7, 7, 8, 9]]
B = np.array (raw)
> > b
Array ([0,1,2,3,4]
[5, 6, 7, 8, 9])
> > >
If you want to get an array displayed as 0, you can use this way to zeros
> d = (4pc5)
> > np.zeros (d)
Array ([[0, 0, 0, 0, 0.]
[0., 0., 0., 0.]
[0., 0., 0., 0.]
[0.,0.,0.,0.,0.])
Of course, the effect is not ideal, which shows the decimal point, which can be formatted and changed to an integer.
> np.ones (dforce dtypewriter int)
Array ([1,1,1,1,1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1,1,1,1,1])
Get random numbers and generate five in one breath.
> > np.random.rand (5)
Array ([0.61643689, 0.15915655, 0.20558268, 0.75157157, 0.50395262])
According to this idea, randomly generating 100 is also seconds.
Let's take a look at array operations.
This part gradually shows its advantage and can support computing.
> a = np.array ([[1jue 2], [2je 5]])
> print a
[[1 2]
[2 5]]
> b = np.array ([[2jue 3], [5je 8]])
> print aquib
[[3 5]
[7 13]]
Get some maximum and minimum values.
> a = np.arange (20) .reshape (4jue 5)
> print a
[[0 1 2 3 4]
[5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
> > print str (a.sum ())
one hundred and ninety
> > print str (a.max ())
nineteen
> > print str (a.min ())
0
> > print str (a.max (axis=1))
[4 9 14 19]
> > >
It can be easily changed.
> b = np.arange (2pr 45pr 3) .reshape (5pr 3)
> b = np.mat (b)
> > print b
[[2 5 8]
[11 14 17]
[20 23 26]
[29 32 35]
[38 41 44]]
If the maximum number is 2, starting with 0, taking five numbers is written like this
> np.linspace (0pje 2pm 5)
Array ([0. , 0.5, 1., 1.5, 2.])
If it is nine numbers, it is as follows.
> np.linspace (0pje 2je 9)
Array ([0. , 0.25, 0.5, 0.75, 1., 1.25, 1.5, 1.75, 2.])
> > >
> a = np.array ([[3jue 2], [5je 9]])
> print a [0] [1]
two
> print a [1] [0]
five
> print a [1J 0]
five
> b = a
> > a [0] [1] = 100
> print a
[[3 100]
[5 9]]
> > print b
[[3 100]
[5 9]]
The above result may seem strange, but the reason for the problem is that instead of actually copying a to b, Python points b to the memory address of a's corresponding data.
You can use copy to circumvent.
> a = np.array ([[3jue 2], [5je 9]])
> b = a.copy ()
> > a [0] [1] = 100
> print a
[[3 100]
[5 9]]
> > print b
[[3 2]
[5 9]]
Specify column
> a = np.arange (20) .reshape (4jue 5)
> print a
[[0 1 2 3 4]
[5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
> print a [:, [1jol 3]]
[[1 3]
[6 8]
[11 13]
[16 18]]
Splice two vectors into a matrix by column:
> a = np.array ((1 ~ 2 ~ 3))
> b = np.array ((2d3))
> print np.column_stack ((aPerm b))
[[1 2]
[2 3]
[3 4]]
This is the end of the content of "detailed explanation of Numpy Library in Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.