The main data structure in the NumPy library is the NumPy array, which is an extremely fast and memory-efficient data structure. The NumPy array is much faster than the common Python list and provides vectorized matrix operations. Let us see the different data types that you can store in a NumPy array, the different ways to create the NumPy arrays, how you can access items in a NumPy array, and how to add or remove items from a NumPy array.
The NumPy library supports all the default Python data types in addition to some of its intrinsic data types. Thus the default Python data types, e.g., strings, integers, floats, Booleans, and complex data types, can be stored in NumPy arrays.
You can check the data type in a NumPy array using the dtype property. Later in this post you will see the different ways of creating NumPy arrays in detail.
Here, we will show you the array() function and then print the type of the NumPy array using the dtype property. Here is an example:
import numpy as np
my_array = np.array([10,12,14,16,20,25])
print(my_array)
print(my_array.dtype)
print(my_array.dtype.itemsize)
The script above defines a NumPy array with six integers. Next, the array type is displayed via the dtype attribute. Finally, the size of each item in the array (in bytes) is displayed via the itemsize attribute.
The output below prints the array and the type of the items in the array, i.e., int32 (integer type), followed by the size of each item in the array, which is 4 bytes (32 bits).
Output:
[10 12 14 16 20 25]
int32
4
The Python NumPy library supports the following data types including the default Python types.
• i – integer
• b – boolean
• u – unsigned integer
• f – float
• c – complex float
• m – timedelta
• M – datetime
• o – object
• S – string
• U – Unicode string
• V – fixed chunk of memory for other type ( void )
Next, let’s see another example of how Python stores text. The following script creates a NumPy array with three text items and displays the data type and size of each item.
import numpy as np
my_array = np.array(["Red", "Green", "Orange"])
print(my_array)
print(my_array.dtype)
print(my_array.dtype.itemsize)
The output below shows that NumPy stores text in the form of Unicode string data type denoted by U. Here, the digit 6 represents the item with the most number of characters.
Output:
['Red' 'Green' 'Orange']
<U6
24
Though the NumPy array is intelligent enough to guess the data type of items stored in it, this is not always the case. For instance, in the following script, you store some dates in a NumPy array. Since the dates are stored in the form of texts (enclosed in double quotations), by default, the NumPy array treats the dates as text. Hence, if you print the data type of the items stored, you will see that it will be a Unicode string (U10). See the code below:
my_array = np.array(["1990-10-04", "1989-05-06", "1990-11-04"])
print(my_array)
print(my_array.dtype)
print(my_array.dtype.itemsize)
Output:
['1990-10-04' '1989-05-06' '1990-11-04']
<U10
40
We will continue to explore NumPy array is the next post. Till then just play around with the code and perform some experiments on your own
0 comments:
Post a Comment