In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "detailed introduction of axes and dimensions in 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 "the detailed introduction of axes and dimensions in numpy".
NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes. The number of axes is rank.
For example, the coordinates of a point in 3D space [1, 2, 1] is an array of rank 1, because it has one axis. That axis has a length of 3. In the example pictured below, the array has rank 2 (it is 2-dimensional). The first dimension (axis) has a length of 2, the second dimension has a length of 3.
[[1., 0., 0.], [0., 1., 2.]]
Ndarray.ndim
The number of axes in an array. In python's world, the number of axes is called rank.
> X = np.reshape (np.arange (24), (2,3,4)) # that is, four planes in two rows and three columns (plane) > > Xarray ([0,1,2,3], [4,5,6,7], [8,9,10,11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]])
Shape function is a function in numpy.core.fromnumeric, its function is to read the length of the matrix, for example, shape [0] is to read the length of the first dimension of the matrix.
Shape (x)
(2pr 3pr 4)
Shape (x) [0]
two
Or
X.shape [0]
two
Let's look at the composition of each plane separately:
> X [:,:, 0] array ([[0,4,8], [12,16,20]]) > X [:, 1] array ([[1,5,9], [13,17,21]]) > X [:,:, 2] array ([2,6,10], [14,18,22]) > X [:,:, 3] array ([[3,7,11], [15,19,23]])
That is, when rearranging np.arange (24) (0,1,2,3,..., 23), the last axis is assigned first in the direction of multiple axes of the multi-dimensional array (for two-dimensional arrays, that is, the direction of rows is allocated first, and for three-dimensional arrays, the direction of the plane is allocated first).
Reshpae, a method in an array object that is used to change the shape of an array.
Two-dimensional array
#! / usr/bin/env python # coding=utf-8 import numpy as np a=np.array ([1, 2, 3, 4, 5, 6, 7, 8]) print a d=a.reshape ((2Jing 4)) print d
Three-dimensional array
#! / usr/bin/env python # coding=utf-8 import numpy as np a=np.array ([1,2,3,4,5,6,7,8]) print a f=a.reshape ((2,2,2)) print f
The principle of shape change is that array elements cannot be changed, for example, it is wrong to write it this way, because the array elements have changed.
#! / usr/bin/env python # coding=utf-8 import numpy as np a=np.array ([1, 2, 3, 4, 5, 6, 7, 8]) print a print a.dtype e=a.reshape ((2jue 2)) print e
Note: the new array generated by reshape shares a memory with the original array, that is, if you change the elements of one array, the other array will change as well.
#! / usr/bin/env python # coding=utf-8 import numpy as np a=np.array ([1,2,3,4,5,6,7,8]) print an e=a.reshape ((2,4)) print e a [1] = 100print a print e
The meaning of the parameter-1 of reshape function in Python
A=np.arange (0,60,10) > > aarray > > a.reshape (- 1) array ([[0], [10], [20], [30], [50]])
If it is written as a.reshape, it will report an error.
ValueError:cannot reshape array of size 6 into shape (1Pol 1)
> a = np.array ([[1dje 2jue 3], [4jre 5je 6]]) > np.reshape (a, (3mai Lok 1)) # the unspecified value is inferred to be 2array ([[1dre 2], [3,4], [5je 6]])
-1 means I'm too lazy to calculate what number to fill in, which is inferred by python through an and other values of 3.
# below are two 2'3 photos (I don't know how many photos are replaced by-1) How to flatten all two-dimensional photos into one-dimensional > image = np.array ([1pence2pence3], [4mae5pence6], [[1recorder 1d1], [1mie1pyr1]]) > image.shape (2,2pyr3) > image.reshape ((- 1,6)) array ([1mem2pyr3, 4pyr5pence6], [1mem1Person1,1parenti1]) Thank you for your reading. The above is the content of "detailed introduction of axes and dimensions in numpy". After the study of this article, I believe you have a deeper understanding of the detailed introduction of axes and dimensions in 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.