After OP edits and comments
- TypeError saying floats are unsubscriptable when using
[item for sublist in generated for item in sublist]
means the list elements are single values and single values in a list (e.g. [[14.22388561], [22.04375228], 34.6576]
)
- Check if each element is a list.
- If
item
type is a list, take the value at index 0, assuming each list contains only 1 value.
- If
item
type is not a list, take item
generated = [item[0] if type(item) == list else item for item in generated]
Solution from before OP edited the question
- I'm guessing
generated
is a np.array
, so you can use .reshape
- If a
np.array
is printed, the output looks like that shown by the op, a list with no ,
- The array shown, has the shape
(7, 1)
, and you want it reshaped to (1, 7)
g = generated.reshape(1, 7)
# display g
array([[108.88114502, 19.29647502, 4.08611068, 52.33578872,
134.54672018, 14.22388561, 22.04375228]])
- If
generated
is a list, converting it to a np.array
and using reshape will work.
generated = np.array(generated)
generated = generated.reshape(1, 7) # where 7 is the length of the list)
%%timeit
comparison
Given g
as a list of lists
import numpy as np
np.random.seed(365)
g = [[np.random.randint(100000)/100] for _ in range(1000000)]
Test
%%timeit
[item for sublist in g for item in sublist]
74.6 ms ± 415 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%%timeit
np.array(g).reshape(1, 1000000)
243 ms ± 2.51 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Given g
as a (1000000, 1) np.array
np.random.seed(365)
g = [[np.random.randint(100000)/100] for _ in range(1000000)]
g = np.array(g)
Test
%%timeit
[item for sublist in g for item in sublist]
867 ms ± 3.11 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
g.reshape(1, 1000000)
289 ns ± 1.16 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)