0

Currently I have a list of arrays as follows:

array =
[array([100000.        , 100848.06652142,  99648.67144694, 102047.67944271,
       103655.99048427, 104602.87678005, 104597.83419837, 103505.42736768,
       104976.01311214, 104457.34603404, 105855.46549162, 105613.30235519,
       105212.71840922, 107647.5054673 ])
 array([107647.5054673 , 106891.82007643, 106979.91405552, 106030.74186486,
       107856.04281712, 108502.71948581, 106557.2401056 , 105659.59829843,
       105663.01875745, 107300.69453018, 106336.34733019, 107584.99034227,
       108089.2320392 , 106786.91702337])
 array([106786.91702337, 104416.74000465, 101289.12525402, 101932.58219813,
       102625.04352578, 101767.46209616, 103345.4263012 , 102816.73322055,
       102417.59316407, 104439.37518975, 103755.22627215, 103817.9744686 ,
       107872.40234863, 108110.9662065 ])
 array([108110.9662065 , 109544.86827069, 111072.22392645, 112618.46235766,
       113847.1768898 , 116708.86391903, 115790.02599715, 115614.72242411,
       119225.88436354, 121991.38468934, 123304.85972848, 125571.38040251,
       122456.3667249 ])
 array([122456.3667249 , 127497.74699282, 128659.85495604, 125813.77115906,
       129008.46450085, 128111.00914756, 123039.92607546, 124723.87932757,
       124181.57385836, 125134.9276196 , 126027.8631434 , 129304.85119148,
       128912.58600657])]

What would be the best possible way to join all these subarrays back together into one large array? I tried np.concatenate() but that did not work.

SOLUTION

For whatever reason, my output would not put commas between the subarrays, once I was able to find the fix for that np.concatenate() did indeed work.

lee.edward01
  • 453
  • 1
  • 6
  • 16
  • 1
    Possible duplicate of [numpy concatenate two arrays vertically](https://stackoverflow.com/questions/21887754/numpy-concatenate-two-arrays-vertically) – AGN Gazer May 22 '18 at 16:50
  • Could you use smaller arrays in your examples, preferably with more humane values that are easy to track when checking the solution? – AGN Gazer May 22 '18 at 16:52
  • Also, how do you want to join the arrays? Vertically? Horizontally? That is why it helps to show desired output. Finally, even though `np.concatenate()` did not work for you, can you show the actual code that you have attempted. – AGN Gazer May 22 '18 at 16:54
  • Why do you also say that `np.concatenate()` **to no surprise** did not work? _I am_ surprised! – AGN Gazer May 22 '18 at 16:57
  • Another possible duplicate (depending on the question) is here: https://stackoverflow.com/q/9236926/8033585 – AGN Gazer May 22 '18 at 17:00
  • @AGNGazer I apologize, I misspoke, I meant to say to my surprise. And for whatever reason, my output was not putting commas in between the subarrays, and after finding the fix for that concatenate did work. I apologize for writing a rather stupid question – lee.edward01 May 22 '18 at 17:04
  • 1
    When a `print` display of an object omits commas, that means it's a numpy array, not a list. Your `array` looks like an object dtype array of `ndarrays`. I can make a valid list from @AGNGazer's modification. But when I try `concatenate` I get the `shape shape` error message. `np.array(thatlist, dtype=object)` produces an array whose 'print(arr)` looks like your `array`. The `repr` adds an outer `array` and `object` dtype. – hpaulj May 22 '18 at 23:40

2 Answers2

2

concatenate works.. I think you forgot some commas in your example data between each array in the list.

import numpy as np
a = [np.array([1,2,3]), np.array(['4','5','6'])]

np.concatenate(a)

outputs

array(['1', '2', '3', '4', '5', '6']
ak_slick
  • 1,006
  • 6
  • 19
1
np.concatenate(array)

The only problem, in your example, array in the list array (now, do you see how bad it is to use confusing names) should become np.array and you should add commas after each numpy.ndarray in the list array:

array = [np.array([100000., 100848.06652142,  99648.67144694, 102047.67944271,
       103655.99048427, 104602.87678005, 104597.83419837, 103505.42736768,
       104976.01311214, 104457.34603404, 105855.46549162, 105613.30235519,
       105212.71840922, 107647.5054673 ]),
 np.array([107647.5054673 , 106891.82007643, 106979.91405552, 106030.74186486,
       107856.04281712, 108502.71948581, 106557.2401056 , 105659.59829843,
       105663.01875745, 107300.69453018, 106336.34733019, 107584.99034227,
       108089.2320392 , 106786.91702337]),
 np.array([106786.91702337, 104416.74000465, 101289.12525402, 101932.58219813,
       102625.04352578, 101767.46209616, 103345.4263012 , 102816.73322055,
       102417.59316407, 104439.37518975, 103755.22627215, 103817.9744686 ,
       107872.40234863, 108110.9662065 ]),
 np.array([108110.9662065 , 109544.86827069, 111072.22392645, 112618.46235766,
       113847.1768898 , 116708.86391903, 115790.02599715, 115614.72242411,
       119225.88436354, 121991.38468934, 123304.85972848, 125571.38040251,
       122456.3667249 ]),
 np.array([122456.3667249 , 127497.74699282, 128659.85495604, 125813.77115906,
       129008.46450085, 128111.00914756, 123039.92607546, 124723.87932757,
       124181.57385836, 125134.9276196 , 126027.8631434 , 129304.85119148,
       128912.58600657])]
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45