In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of generating ndarray by Numpy". The content of the explanation in the article 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 method of generating ndarray by Numpy".
What is Numpy?
Numpy is an open source scientific computing tool kit of Python and an advanced numerical programming tool.
Powerful N-dimensional array object: ndarray
You can operate on array structure data (without traversing loops)
It has the functions of random number, linear algebra, Fourier transform, etc.
How to install it? Install anaconda scientific computing environment
Anaconda is recommended, which integrates many commonly used libraries and is easier to use when configuring the environment.
Download address: https://www.anaconda.com/download/
Install Numpy
Method 1: after installing anaconda, numpy can be used directly without the need for secondary installation.
Method 2: do not install anaconda can be installed using pip install numpy.
Install jupyter notebooks (recommended)
Method 1: after installing anaconda, jupyter notebooks can be used directly without the need for secondary installation.
Method 2: do not install anaconda can be installed using pip install jupyter.
Import of Numpy basic data structures
From numpy import np is recommended
From numpy import * is not recommended because numpy contains a large number of functions with the same name as the Python built-in function.
Generate ndarray
You can use array to generate arrays
Take a chestnut:
Import numpy as np
Ar = np.array ([[1mem2d3re4], [1min2pd3re4]))
Print (ar, type (ar))
> > >
[[1 2 3 4]
[1 2 3 4]]
Besides np.array, there are other functions that can create new arrays. Here are a few commonly used ones:
Array version of arange # python range
Asarray # converts input to ndarray
Ones # generates an array of all 1s based on a given shape and type
Ones_like # generates an array of all 1s with the same shape from a given array
Zeros # generates an array of all zeros based on a given shape and type
Zeros_like # generates an array of all 1s with the same shape from a given array
Eye # generates a Numeric feature matrix (diagonal 1, the rest 0)
Linspance # returns num evenly spaced samples calculated on the interval [start, stop]
Here are the examples of zeros,zeros_like and linspance:
Arr = np.zeros ((3pm 5))
Print (arr)
> > >
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
S = np.array ([list (range (10)), list (range (10))]))
Print (s)
Print (np.zeros_like (s)
> > >
[[0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]]
[[0 0 0]
[0 0 0]]
Print (np.linspace (10 ~ (10) 20 ~ (m) num = 21)) # generated between 10 ~ (th) and 21 ~ (th)
Print (np.linspace (10 and 20 endpoint num = 21, endpoint = False) # appointment defaults to True and does not include the value on the left when it is False
Print (np.linspace (10 and 20 retstep num = 21, retstep = True)) # restep display step
> > >
[10. 10.5 11. 11.5 12. 12.5 13. 13.5 14. 14.5 15. 15.5 16. 16.5
17. 17.5 18. 18.5 19. 19.5 20.]
[10. 10.47619048 10.95238095 11.42857143 11.9047619 12.38095238
12.85714286 13.33333333 13.80952381 14.28571429 14.76190476 15.23809524
15.71428571 16.19047619 16.66666667 17.14285714 17.61904762 18.0952381
18.57142857 19.04761905 19.52380952]
Array ([10., 10. 5, 11., 11. 5, 12., 12. 5, 13., 13. 5, 14., 14. 5, 15.
15.5, 16., 16.5, 17., 17.5, 18., 18.5, 19., 19.5, 20.]), 0.5)
In addition to a few commonly used functions for generating arrays, here are some commonly used methods:
Import numpy as np
Ar = np.array ([[1mem2d3re4], [1min2pd3re4]))
Print (ar, type (ar))
Print (ar.ndim) # returns the number of dimensions in the array
The dimension of the print (ar.shape) # array that returns several rows and columns
The number of elements in the print (ar.size) # array
Type of print (ar.dtype) # element
The size of the elements in the print (ar.itemsize) # array
> > >
[[1 2 3 4]
[1 2 3 4]]
two
(2, 4)
eight
Int64
eight
Numpy Universal function Array shape Transformation (.T / .reshape () / .resize ())
T is the transpose function, which has no effect on the one-dimensional array.
# .T
Import numpy as np
Ar1 = np.arange (10)
Ar2 = np.zeros ((2pm 5))
Print (ar1.T)
Print (ar2.T) # transpose function
> > >
[0 1 2 3 4 5 6 7 8 9]
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]
.reshape (), directly change the shape of the array, but the number of array elements must be the same before and after the change
Ar1 = np.arange (10)
Print (ar1.reshape (2, 5))
Print (np.reshape (np.arange (16), (2Jing 8))
> > >
[[0 1 2 3 4]
[5 6 7 8 9]]
[[0 1 2 3 4 5 6 7]
[8 9 10 11 12 13 14 15]]
.resize ()
Print (np.resize (np.arange (16), (3Jing 5)) # resize iterates sequentially when the number of subsequent array elements is less than the number generated previously
Print (np.resize (np.arange (12), (3Jing 5)) # resize is randomly populated when the number of elements behind the array is greater than the number of elements generated before.
> > >
[[0 1 2 3 4]
[5 6 7 8 9]
[10 11 12 13 14]]
[[0 1 2 3 4]
[5 6 7 8 9]
[10 11 0 1 2]]
Replication of array
Similar to the deep and shallow copy in python: Python | the deep and shallow copy of Python learning
Type conversion of array
.astype () can convert the types of elements in an array. In numpy, there are several element types (if there are too many, not all of them):
Int8, uint8 # signed and unsigned 8-bit integers
Int16, uint16 # signed and unsigned 16-bit integers
Int32, uint32 # signed and unsigned 32-bit integer
Int64, uint64 # signed and unsigned 64-bit integer
Float16 # semi-precision
Float32 # single precision
Float64 # double precision
Bool # Boolean
.
Take a type-converted chestnut:
Ar1 = np.arange (10 dtypewritten float)
Ar2 = ar1.astype (np.int64)
Print (ar1,ar2)
> > >
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] [0 1 2 3 4 5 6 7 8 9]
Stacking of arrays
The array is stacked with hstack (), vstack (), and stack (), with the following examples:
A = np.arange (10)
B = np.arange (100.20)
Print (ar1,ar2)
# horizontal links
Print (np.hstack ((aPermib)
# Vertical links
A = np.array ([[1], [2], [3]])
B = np.array ([['a'], ['b'], ['c']])
Print (np.vstack ((aPermib)
# arbitrary stacking
A = np.arange (10)
B = np.arange (100.20)
Print (np.stack ((aQuinine b), axis=1)) # Vertical stacking
Print (np.stack ((aPerm) # stacking horizontally
>
[0 12 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18 19]
[0 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[['1']
['2']
['3']
['a']
['b']
['c']]
[[0 10]
[1 11]
[2 12]
[3 13]
[4 14]
[5 15]
[6 16]
[7 17]
[8 18]
[9 19]]
[[0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]]
Array split
Array split is also divided into horizontal split and vertical split.
# Array split
Ar = np.arange (16) .reshape (4jue 4)
Print (ar)
Print (np.hsplit (ar,2)) # Vertical split
Print (np.vsplit (ar,2)) # horizontal split
> > >
[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]
[12 13 14 15]]
[array ([0,1]
[4, 5]
[8, 9]
[12, 13]), array ([2, 3]
[6, 7]
[10, 11]
[14, 15]])]
[array ([0, 1, 2, 3]
[4, 5, 6, 7]), array ([8,9,10,11]
[12, 13, 14, 15])]
Common calculation function
The computational functions here are used in the same way as those in Python, and will not be discussed too much here.
# Computing function
Np.mean () # find the average
Np.max () # maximum
Np.min () # minimum
Np.gtd () # standard deviation
Np.var () # Variance
Np.sum () # where the parameter axis=0 is summed by column and axis=1 by row
Thank you for your reading, the above is the content of "what is the method of generating ndarray by Numpy". After the study of this article, I believe you have a deeper understanding of what the method of generating ndarray by Numpy is, 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.