In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Numpy array and list of how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will gain after reading this Numpy array and list how to use the article, let's take a look.
1. This article introduces
Today, I would like to introduce to you the following:
Mutual conversion between Ⅰ ndarray array and list
Data type conversion of Ⅱ ndarray array
Ⅲ changes the shape of the ndarray array
To put it bluntly, it is about three functions. Of course, this is just the tip of the iceberg in the numpy function. This is just an introduction to the first few functions you encounter in the process of learning numpy.
2. Mutual conversion between ndarray array and list
When you learn numpy, I acquiesce that you must have learned the basics of Python. You are certainly no stranger to the basic data type of list lists.
So how do we achieve the conversion between "list" and "array"?
① list to array: pass a list directly into the array () function
Listlist1 = list (range (10)) print (list1) array1 = np.array (list1) print (array1)
The results are as follows:
② array to list: call the tolist () method of the array
Array2 = np.arange (10) print (array2) list2 = array2.tolist () print (list2)
The results are as follows:
3. Data type conversion of ndarray array
Remember one sentence: data type conversion in numpy, do not use x.dtype to modify the data type of an element, it is better to use x.astype ().
Data types commonly used in ① numpy
Here we only describe the data types commonly used in numpy, int types and float types, but generally do not write int and float directly, but write things like int32 and float64, so let's briefly talk about the meaning of 32 or 64.
In a computer, the lowest level executes binary instructions consisting of 0 and 1. A 0 or 1 represents a binary bit, also known as "bit", where 32 or 64 represent binary bits.
According to the conversion unit of the computer, 1 byte = 8 binary bits, or "1bytes=8bit", so according to this conversion: "32bit-4bytes", "64bit=8bytes".
From the above analysis, we now make a summary of int32. Int32 shows that the data type of each element in the array is int32. At the same time, through 32, we can know that the storage space of each element in the array is 4 bytes, so the range is roughly [- 2147483648].
What happens when ② uses dtype to modify the data type of an array in place?
X = np.array ([1.2, 3.4, 5.6) Dtype=np.float64) print (x) print (x.dtype) print (x.nbytes) #-x.dtype = "float32" print (x) print (x.nbytes) # -x.dtype = "float16" print (x) print (x.nbytes)
The results are as follows:
Why did the above phenomenon occur?
Through the above test, it is found that when we use "dtype" to modify the data type of the array, each time the bit (that is, a "binary bit") is compressed to 1/2, the number of elements in the array will double.
Why is that?
Once the array is created, its data type is determined, which is equivalent to opening up a piece of memory to store the array. Here is a formula to illustrate the reasons for the above phenomenon.
Suppose an array has x elements and the data type is int64. This data type takes up 8 bytes of memory, so the entire array takes up 8 bytes of memory.
When the data type becomes int32, the data type in the array becomes 4 bytes. Through the above calculation, we already know that the memory opened up in this array is 8x, so the number of elements becomes 8x/4-2, which means that the array elements are doubled.
③ uses the astype () function to modify the data type of an array: it is equivalent to creating a new array
Z = np.array ([1.5 zz 3.7) print (z) print (z.dtype) zzz = z.astype (np.float32) print (zz) print (zz.dtype)
The results are as follows:
4. Change the shape of the ndarray array
① uses the reshape () function in numpy to modify array objects
Xx = np.arange (10). Reshape (2) xxx = np.reshape (xx, (5)) print (xxx)
The results are as follows:
② uses the reshape () method of an array object to modify an array object
Yy = np.arange (10). Reshape (2) print (yy)
The results are as follows:
When ③ changes the shape of the array, if the dimension is greater than 1, you can set the Last Dimension to-1
P = np.arange (6). Reshape (2) print (p) Q = np.arange (6). Reshape (2) print (Q)
The results are as follows:
Note: the reason you can do this is that when you specify the previous dimension, the last dimension automatically calculates the number of dimensions of the last dimension based on the number of elements in the array and the number of dimensions in front of it. * Dimension N = number of elements].
This is the end of the article on "how to use Numpy arrays and lists". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use Numpy arrays and lists". If you want to learn more, you are welcome to follow the industry information channel.
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: 230
*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.