Last active
May 12, 2018 15:48
-
-
Save SemanticBeeng/b3102567b1a566fe0b2eb99edae9409c to your computer and use it in GitHub Desktop.
structured numpy arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# #resource | |
# https://docs.scipy.org/doc/numpy-1.14.0/user/basics.rec.html | |
conda install -c conda-forge traits=4.6.0 | |
traits: 4.6.0-py36_1 conda-forge | |
import numpy as np | |
from traits.api import Array, Tuple, List, String | |
from traitschema import Schema | |
class TrainingResults(Schema): | |
training = List(Tuple(Array(dtype=np.float32), np.int64)) | |
l = List((np.random.random(1), ) | |
result = TrainingResults(training=l) | |
import numpy as np | |
x = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)], | |
dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]) | |
x | |
x[1] | |
x['age'] | |
np.dtype({'names': ['col1', 'col2'], | |
'formats': ['i4','f4'], | |
'offsets': [0, 4], | |
'itemsize': 12}) | |
dt_y = np.dtype([('x', 'f4'), ('y', np.float32), ('z', 'f4', (2,2))]) | |
dt_y.names | |
dt_y.fields | |
y = np.zeros(1, dtype=dt_y) | |
>> array([(0., 0., [[0., 0.], [0., 0.]])], | |
dtype=[('x', '<f4'), ('y', '<f4'), ('z', '<f4', (2, 2))]) | |
y = np.array((12, 3.4, 3), dtype=dt_y) | |
>> array((12., 3.4, [[3., 3.], [3., 3.]]), | |
dtype=[('x', '<f4'), ('y', '<f4'), ('z', '<f4', (2, 2))]) | |
y['x'] | |
>> array(12., dtype=float32) | |
y[0] | |
>> too many indices for array | |
--- | |
dt_y = np.dtype([('x', 'f4'), ('y', np.float32), ('z', np.int64)]) | |
y = np.array((12, 3.4, 3), dtype=dt_y) | |
y[0] | |
>> too many indices for array | |
y = np.array([(12, 3.4, 3)], dtype=dt_y) | |
>> array([(12., 3.4, 3)], dtype=[('x', '<f4'), ('y', '<f4'), ('z', '<i8')]) | |
scalar = y[0] | |
scalar | |
>> (12., 3.4, 3) | |
scalar.item() | |
(12.0, 3.4000000953674316, 3) | |
x = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) | |
x.view(np.recarray) | |
>> rec.array([(1., 2), (3., 4)], | |
dtype=[('x', '<f8'), ('y', '<i8')]) | |
# --- | |
# http://docs.enthought.com/traits/traits_user_manual/defining.html | |
# http://docs.enthought.com/traits/traits_user_manual/index.html | |
t.trait( 'i' ).default | |
t.trait( 'i' ).default_kind | |
t.trait( 'i' ).inner_traits | |
t.trait( 'i' ).is_trait_type(Int) | |
t.trait( 'i' ).is_trait_type(Float) | |
t.trait( 'lf' ).inner_traits[0].is_trait_type(Float) | |
# --- | |
# https://traitschema.readthedocs.io/en/latest/ | |
import numpy as np | |
from traits.api import Array | |
from traitschema import Schema | |
class Matrix(Schema): | |
data = Array(dtype=dt_y) | |
matrix = Matrix(data=y) | |
matrix.data | |
>> array((12., 3.4, [[3., 3.], [3., 3.]]), | |
dtype=[('x', '<f4'), ('y', '<f4'), ('z', '<f4', (2, 2))]) | |
matrix.to_json() | |
>> '{"data": [12.0, 3.4000000953674316, [[3.0, 3.0], [3.0, 3.0]]]}' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment