Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the data type of NumPy ndarray

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to use the data type of NumPy ndarray, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use the data type of NumPy ndarray. Let's take a look at it.

Ndarray is a special numpy data type, which is a multi-dimensional array.

Data type of ndarray

The data type, dtype, is also a special object that contains memory block information that ndarray needs to declare for a certain type of data (also known as metadata, that is, data that represents data).

Dtype is the reason why NumPy can interact flexibly with other system data. Usually, other systems provide a correspondence between hard disk or memory and data, which makes it very convenient to read and write data in low-level languages such as C or Fortran.

The name describes the bool_ Boolean data type (True or False) int_ default integer type (similar to long,int32 or int64 in C) intc, like the int type of C, is generally the integer type used by int32 or int 64intp for indexing (similar to C's ssize_t In general, it is still int32 or int64) int8 bytes (- 32768 to 32767) int32 integers (- 2147483648 to 2147483647) int64 integers (- 9223372036854775808 to 9223372036854775807) uint8 unsigned integers (0 to) uint16 unsigned integers (0 to 65535) uint32 unsigned integers (0 to 4294967295) uint64 unsigned integers (0 to 18446744073709551615) float_float64 type abbreviated float16 semi-precision floating point numbers, including: 1 symbol bit, 5 index digits 10 Mantissa float32 single-precision floating point numbers, including: 1 symbol bit, 8 exponential bits, 23 Mantissa float64 double-precision floating point numbers, including: 1 symbol bit, 11 exponential bits, 52 Mantissa complex_complex128 type abbreviation, that is, 128bit complex complex64 complex number, represents double 32-bit floating point number (real part and imaginary part) complex128 complex number, represents double 64-bit floating point number (real part and imaginary part)

Use the astype method to explicitly convert the data type of an array

Arr = np.array print (arr.dtype) print (arr) float_arr = arr.astype ('float32') # can also write arr.astype (np.float32) print (float_arr.dtype) print (float_arr)

Int32 [1 2 3 4 5] float32 [1. 2. 3. 4. 5.]

Note: it is possible to convert a string array that is a number into a number. When the content is a floating-point number, it can only be converted to float, not int. It can only be converted to int if it is an integer.

Use the dtype of other arrays to convert data types:

Int_arr = np.arange (10) calibers = np.array ([.22, .270, .357], dtype=np.float64) print (calibers) arr_last = int_arr.astype (calibers.dtype) print (arr_last.dtype) print (arr_last)

[0.22 0.27 0.357] float64 [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]

Add: simple use of Python3:numpy (basic properties of ndarray and basic methods for generating arrays)

Statement

Due to my learning needs, I began to learn numpy, a scientific computing tool. This article is used to review what I have learned (the current version of numpy is 1.17.4).

Basic properties of 1.ndarray

two。 Methods for generating arrays (mainly test generation 0 and generation 1 methods: ones and zeros methods)

1. Output the basic properties of the current ndarray # Test the properties in the narray in the current Numpy # the version of the numpy used is: 1.17.4import numpy as npdefault_array = [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5 6]] np_array = np.array (default_array) print ("dtype type of the currently stored data is: {}" .format (np_array.dtype)) # int32print ("View the actual type of this object: {}" .format (type (np_array) # print ("View the shape of this object: {}" .format (np_array.shape)) # (2 6) print ("current length of this object: {}" .format (np_array.itemsize)) # 4print ("current length of this object (using python's len method): {}" .format (len (np_array)) # 2 only iterates over a set of 2D data print ("current length of this object (using your own size method): {}" .format (np_array.size) )) # got the number of all the data print (np.array ([1) 2, 3], [1,2,3]) .dtype) print (np.array ([1.2,2.2,3.2]) .dtype) # you can see that the current default type is int32#, and the default float type is: float64# modifies and sets the current initialization type # print (np.array ([[1.1, 1.2, 1.3]]) Dtype= "int32") .dtype) print (np.array ([1.1rect 1.2je 1.3]], dtype=np.int32) .dtype) summary:

1. When creating two-dimensional data, the length obtained by using the native python's len method is the outer length, not the actual content of the two-dimensional array!

two。 Convert the array in the original python to data of type ndarray through np.array (array)

3. Each ndarray has a data type represented by dtype. The default integer type is int32 and the floating point type is float64.

4. Get the number of elements in the current ndarray through ndarray.size

5. Get the shape of the current ndarray through ndarray.shap

6. When you create a ndarray with np.array (), you can specify the dtype of the current ndarray, which can be in the form of a character or np. Types

two。 Use numpy to generate simple arrays (np.zeros (), np.ones (), np.empty ()) Np.array () # generate the operation np_array = np.zeros of 1 using the generated array data method import numpy as np# in numpy ([2] 2]) print ("currently generated data is: {}" .format (np_array)) print ("the type of output currently generated data is: {}" .format (np_array.dtype)) # indicates that the type of data generated by default is float64# and now changes the current dtype Directly set the data type of the current dtype to int32np_array.dtype = np.int32print ("currently generated data is: {}" .format (np_array)) print ("output the currently generated data type is: {}" .format (np_array.dtype)) # generate 1 data np_array_ones = np.ones ([2,2], dtype=np.int32) print (np_array_ones) # create an uninitialized data Default is not initialized x = np.empty ([3,2], dtype=int) print (x) summary:

1. Use the current np.zeros (shape) and np.ones (shape) methods to generate an array of specified shapes that are all zeros or all ones

two。 Generate an empty array through np.empty (shape)

3. You can change the current ndarray type through ndarray.dtype=dtype

3. Use the second method of generating arrays (np.asarray ()) Np.copy () # create data import numpy as npdefault_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] default_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9) from an existing array 10) print (type (default_tuple)) copy_array = np.asarray (default_array) # for shallow copy copy_tuple = np.asarray (default_tuple) print ("data after the asarray array: {}" .format (copy_array)) print ("data after the array: {}" .format (copy_tuple)) deep_copy_array = np.copy (default_array) print ("data after the copy array: {}" .format (deep_copy_array) summary:

1. Here the array generated by the np.asarray () method is associated with the original array and is a shallow copy

two。 The other backup data generated by the np.copy () method here is a deep copy

4. Generate an array with a specified range (np.range (), np.random.random (), np.random.randint (), np.linspace ()) # generate an array with a fixed range, and use arange to generate data between 0 and 9. The data generated by default is the current range. The step size here is 1 by default, and the result does not contain 10. Here range_array = np.arange (0,10, dtype=np.int32) print ("range_array: {}" .format (range_array)) # is iterated according to the specified step size to generate the array random_array = np.random.random ((2)) randomly. 2) print ("generate arrays randomly: {}" .format (random_array)) # generates data between 0 and 1 by default # 2 generates random integers random_array_int = np.random.randint (1,10, (2,2)) print ("generate random integers: {}" .format (random_array_int)) # generates 15 numbers between 1 and 10 in the specified range, which is a random data It is equidistant. When the required data exceeds the current range of data, some data listspace_array = np.linspace (1,10,15, dtype=np.int32) will be randomly generated by default. It is divided into specified required data according to a certain equal division. The result here contains 10, which is equivalent to the print ("listspace_array: {}" .format (listspace_array)) summary of the current arithmetic series:

1. The current random method is to randomly generate data in a specified range, and the type can be specified.

2.range is the equivalent of the range method in the current python. You can specify the step size, and it is a [aformab) of this data.

3.linspace is used to generate an array in a specified range in a specified manner. This is a congruent sequence. If the current required data is greater than this range, it will be randomly generated.

5. Generate a proportional sequence (np.logspace ()) # generate a proportional sequence, where 2 indicates that the number of samples generated is 2, the starting data is 1, and the end data is 4, indicating that the minimum is 3 to the fourth power of 3 equal_ratio_array = np.logspace (1, 4, 2, dtype=np.int32) # where the default base number is 10, which represents the current minimum power of 10 The maximum is the current 10 to the fourth power print ("the data of the current proportional series is: {}" .format (equal_ratio_array))

The data of the current proportional series is: [10 10000]

This is the end of the article on "how to use the data types of NumPy ndarray". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use the data types of NumPy ndarray". 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: 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report