In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Ndarray Multidimensional Array (N Dimension Array)
NumPy array is a multi-dimensional array object (matrix), called ndarray, which has the ability of vector arithmetic and complex broadcasting, and has the characteristics of fast execution and space saving.
Note: the subscript of ndarray starts at 0, and all elements in the array must be attributes of the same type ndarray.
1.ndim attribute: number of dimensions
2.shape attribute: latitude size
3.dtype attribute: data typ
Random creation of ndarray
Generating random data by numpy.random
Sample code:
# Import numpy, alias npimort numpy as np# to generate random multidimensional floating point data of specified dimension size (3 rows and 4 columns) (2D), rand fixed interval 0.09 ~ 1.0arr = np.random.rand (3,4) print (arr) print (type (arr)) # generate random multidimensional integer data of specified dimension size (3 rows and 4 columns) (2D) Randint () can specify the interval (- 1 ~ 5) arr = np.random.randint (- 1,5, size= (3,4)) # 'size=' can omit print (arr) print (type (arr)) # to generate random multi-dimensional floating point data of specified dimension size (3 rows and 4 columns) (2D) Uniform () can specify the interval (- 1-5) arr = np.random.uniform (- 1,5, size= (3,4)) # 'size=' can omit print (arr) print (type (arr)) print (' number of dimensions:', arr.ndim) print ('dimension size:', arr.shape) print ('data type:', arr.dtype)
Running result:
[[0.09371338 0.06273976 0.22748452 0.49557778] [0.30840042 0.35659161 0.54995724 0.018144] [0.94551493 0.70916088 0.58877255 0.90435672]] [[13 01] [1443] [20-1]] [[2.25275308 1.67484038-0.03161878-0.44635706] [1.35459097 1.66294159 2.47419548-0.51144655] [1.43987571 4.71505054 4.33634358 2.48202309] Number of dimensions: 2 dimensions size: (3 4) data type: the sequence of float64ndarray creates np.array (collection)
Collection is a sequential object (list) and a nested sequence object (list of list).
Sample code:
# list sequence converted to ndarraylis = range (10) arr = np.array (lis) print (arr) # ndarray data print (arr.ndim) # number of dimensions print (arr.shape) # Dimension size # list of list nested sequence converted to ndarraylis_lis = [range (10) Rarnge (10)] arr = np.array (lis_lis) print (arr) # ndarray data print (arr.ndim) # number of dimensions print (arr.shape) # dimension size
Running result:
# list sequences are converted to ndarray [0 1 2 3 4 5 6 7 8 9] 1 (10,) # list of list nested sequences into all 0 arrays of ndarray [[0 1 2 3 4 5 6 7 8 9]] 2 (2, 10) np.zeros () of specified size. Note: the first parameter is a tuple, which is used to specify the size, such as (3pr 4). Np.ones () specifies the size of the all-1 array. Note: the first parameter is a tuple, which is used to specify the size, such as (3pr 4). Np.empty ()
Initializing an array does not always return all zeros, but sometimes returns uninitial random values (random values in memory).
Sample code (2, 3, 4):
# np.zeroszeros_arr = np.zeros ((3,4)) # np.onesones_arr = np.ones ((2,3)) # np.emptyempty_arr = np.empty ((3,3)) # np.empty specifies the data type empty_int_arr = np.empty ((3,3) Int) print ('- zeros_arr- `) print (zeros_arr) print ('\ nMichapes intagliarr`) print (ones_arr) print ('\ nlylyemptyprinciparr`) print (empty_arr) print ('\ nlyemptypeintfolarr`) print (empty_int_arr)
Running result:
-zeros_arr- [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]-ones_arr- [[1. 1. 1.]]-empty_arr- [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]-empty_int_arr- [[00] [0 000] [000]] np.arrange () and reshape ()
Arange () is similar to python's range (), creating an one-dimensional ndarray array.
Reshape () will resize the array
Sample code (5):
# np.arange () arr = np.arange (15) # one-dimensional array of 15 elements print (arr) print (arr.reshape (3,5)) # two-dimensional array of five elements print (arr.reshape (1,3,5)) # three-dimensional array of five elements
Running result:
[0 12 3 4 5 6 6 7 8 9 10 11 12 13 14] [[0 12 3 4] [5 6 7 8 9] [10 11 12 13 14]] [[0 12 3 4] [5 6 7 8 9] [10 11 12 13 14] np.arange () and random.shuffle () random.shuffle () will scramble the array sequence (similar to shuffling)
Sample code (6):
Arr = np.arange (15) print (arr) np.random.shuffle (arr) print (arr) print (arr.reshape (3,5))
Running result:
[0 12 3 4 5 6 6 7 8 9 10 11 12 13 14 14] data type of ndarray [5 8 1 7 4 0 12 9 11 11 2 13 14 10 3 6] [5 8 1 7 4] [0 12 9 11 2] [13 14 10 3 6]
Dtype parameter
Specify the data type of the array, type name + digits, such as float64, int32
Astype method
Convert the data type of an array
Sample code (1, 2):
# initialize a 3-row and 4-column array with the data type float64zeros_float_arr = np.zeros ((3p4), dtype = np.float64) print (zeros_float_arr) print (zeros_float_arr.dtype) # astype convert the data type of the existing array to int32zeros_int_arr = zeros_float_arr.astype = (np. Int32) print (zeros_int_arr) print (zeros_int_arr.dtype)
Running result:
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] float64 [[0 0 0] [0 0 0] [0 0 0]] int32
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.