|
||||||||||||||||||||||||||||||||||||||||
Data Types
Время создания: 29.09.2017 21:31
Раздел: numPy
Запись: xintrea/mytetra_db_mcold/master/base/1506709904ouzqm12ev1/text.html на raw.githubusercontent.com
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
NumPy supports a much greater variety of numerical types than Python does. The following table shows different scalar data types defined in NumPy.
NumPy numerical types are instances of dtype (data-type) objects, each having unique characteristics. The dtypes are available as np.bool_, np.float32, etc. Data Type Objects (dtype) A data type object describes interpretation of fixed block of memory corresponding to an array, depending on the following aspects −
The byte order is decided by prefixing '<' or '>' to data type. '<' means that encoding is little-endian (least significant is stored in smallest address). '>' means that encoding is big-endian (most significant byte is stored in smallest address). A dtype object is constructed using the following syntax − numpy.dtype(object, align, copy)
The parameters are −
Example 1 # using array-scalar type
import numpy as np dt = np.dtype(np.int32) print dt The output is as follows − int32
Example 2 #int8, int16, int32, int64 can be replaced by equivalent string 'i1', 'i2','i4', etc.
import numpy as np dt = np.dtype('i4') print dt The output is as follows − int32
Example 3 # using endian notation
import numpy as np dt = np.dtype('>i4') print dt The output is as follows − >i4
The following examples show the use of structured data type. Here, the field name and the corresponding scalar data type is to be declared. Example 4 # first create structured data type
import numpy as np dt = np.dtype([('age',np.int8)]) print dt The output is as follows − [('age', 'i1')]
Example 5 # now apply it to ndarray object
import numpy as np dt = np.dtype([('age',np.int8)]) a = np.array([(10,),(20,),(30,)], dtype = dt) print a The output is as follows − [(10,) (20,) (30,)]
Example 6 # file name can be used to access content of age column
import numpy as np dt = np.dtype([('age',np.int8)]) a = np.array([(10,),(20,),(30,)], dtype = dt) print a['age'] The output is as follows − [10 20 30]
Example 7 The following examples define a structured data type called student with a string field 'name', an integer field 'age' and a float field 'marks'. This dtype is applied to ndarray object. import numpy as np student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) print student The output is as follows − [('name', 'S20'), ('age', 'i1'), ('marks', '<f4')])
Example 8 import numpy as np student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student) print a The output is as follows − [('abc', 21, 50.0), ('xyz', 18, 75.0)]
Each built-in data type has a character code that uniquely identifies it.
|
||||||||||||||||||||||||||||||||||||||||
Так же в этом разделе:
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|