55034

Stacking arrays in new dimension (numpy)

Question:

let's say I have three arrays

k = np.array([[1,1],[2,2]]) m = np.array([[3,3],[4,4]]) n = np.array([[5,5],[6,6]])

I ideally I would like to achieve a final arrays of a shape (3,2,2), i.e.

array([[[1, 1], [2, 2]], [[3, 3], [4, 4]] [[5, 5], [6, 6]]])

So I did

l = np.stack((k,m), axis=0)

and got an array

array([[[1, 1], [2, 2]], [[3, 3], [4, 4]]])

of size (2,2,2). However, when I tried to append/stack the n array, I always got an error of wrong dimension. I could do np.dstack but that's not giving me what I want. Any help with this would be very much appreciated. Thank you.

Answer1:

Just for the sake of completeness and to close this question, the answers suggested by Akavall and f5r5e5d are all working solutions.

# Akavall's solution np.stack((k, m, n), axis=0) # f5r5e5d's solution np.array([k,m,n]) # my approach In [38]: np.concatenate((k[None, :, :], m[None, :, :], n[None, :, :])) Out[38]: array([[[1, 1], [2, 2]], [[3, 3], [4, 4]], [[5, 5], [6, 6]]])

Recommend

  • Google Analytics - Using Client ID as a custom dimension
  • How to increase the python speed over loops?
  • Numpy: How to get rid of the minima along axis=1, given the indices - in an efficient way?
  • Keep same scale in different graphs ggplot2
  • JFreechart X and Y axis scaling
  • What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and rep
  • Cythonized function unexpectedly slow
  • How to reshape a 3D numpy array?
  • How to change placeholder text in an autocomplete activity of android google place?
  • How do I add a File Type Association in a Windows Phone 8.1 app manifest?
  • Find 5 consecutive numbers in numpy array by row, ignore duplicates
  • Where these are stored?
  • Put value at centre of bins for histogram
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Breaking out column by groups in Pandas
  • wxPython: displaying multiple widgets in same frame
  • Display images in Django
  • Problem deserializing objects from cache on MyBatis 3/Java
  • Meteor: Do Something On Email Verification Confirmation
  • Change multiple background-images with jQuery
  • Android screen density dpi vs ppi
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Where to put my custom functions in Wordpress?
  • Change an a tag attribute in JavaScript based on screen width
  • Rearranging Cells in UITableView Bug & Saving Changes
  • RestKit - RKRequestDelegate does not exist
  • How to delete a row from a dynamic generate table using jquery?
  • WPF Applying a trigger on binding failure
  • Benchmarking RAM performance - UWP and C#
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Is there any way to bind data to data.frame by some index?