1

I have three dictionaries for example:

first_dictionary = {'Feature1': array([0, 0, 1, 0]),
 'Feature2': array([0, 1, 0, 0]),
 'Feature3': array([1, 0, 0])}

second_dictionary = { 'Feature4': array([0., 1.]),
 'Feature5': array([0.]),
 'Feature6': array([0., 1.])}

third_dictionary = {{'Feature7': array([  0.,   0.,   0., 912.,   0.,
          0.,   0.,   0.,   0.,   0.]),
 'Feature8': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}

Real dictionaries have a lot more keys (about 50 in each). I combine numpy array to produce a single 1D numpy array:


output = []

for dictionary in [first_dictionary,second_dictionary,third_dictionary]:
    for key, value in dictionary.items():
        output += list(value)
    
output_array = np.array(output)

However, when I do this the datatype are all messed where as I want to produce a final numpy array which maintains the datatype of original numpy arrays.

So what I get is:

array([  0.,   0.,   1.,   0.,   0.,   1.,   0.,   0.,   1.,   0.,   0.,
         0.,   1.,   0.,   0.,   1.,   0.,   0.,   0., 912.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.])


Whereas, as you can you first dictionary only have integers so I want it to look like:


array([  0,    0,    1,    0,    0,    1,    0,    0,   1,     0,    0,
         0.,   1.,   0.,   0.,   1.,   0.,   0.,   0., 912.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.])

How can I achieve this? Insights will be appreciated. Side note: All of the numpy arrays in all of dictionaries were created using np.ndarray with type set to None.

1 Answer 1

2

You can do this by setting numpy array's dtype to object

output_array = np.array(output, dtype= object)

output:

[0 0 1 0 0 1 0 0 1 0 0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 912.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.