## Reshape arrays in Python

I need to reshape two arrays into a certain shape

import numpy as np x = np.array([(1, 2, 3, 4, 5), (6, 7, 8, 9)]) y = np.array([(10, 11, 12, 13, 14), (15, 16, 17, 18)])

I already used np.column_stack(x,y)

np.column_stack((x,y))

to get:

array([[(1, 2, 3, 4, 5), (10, 11, 12, 13, 14)], [(6, 7, 8, 9), (15, 16, 17, 18)]])

however, now i need the array to get to the following shape:

array([[(1, 2, 3, 4, 5, 10, 11, 12, 13, 14)], [(6, 7, 8, 9, 15, 16, 17, 18)]])

Is this possible?

Thanks!!

Given that you have an array of `tuples`

, what you could do is add them along the first axis:

np.sum([x,y], axis=0)[:,None] [[(1, 2, 3, 4, 5, 10, 11, 12, 13, 14)] [(6, 7, 8, 9, 15, 16, 17, 18)]]

**NumPy Array Reshaping,** of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. Reshaping arrays. Reshaping means changing the shape of an array. Convert the following 1-D array with 12 elements into a 2-D array. numpy.reshape (array, shape, order = ‘C’) : shapes an array without changing data of array.

Stacking introduces a new dimension. You want concatenation along the columns (*existing* axis 1):

np.concatenate((x, y), axis=1)

**NumPy Array manipulation: reshape() function,** NumPy Array manipulation: reshape() function The reshape() function is used to give a new shape to an array without changing its data. Array to be reshaped. The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. numpy.reshape¶ numpy.reshape (a, newshape, order='C') [source] ¶ Gives a new shape to an array without changing its data. Parameters a array_like. Array to be reshaped. newshape int or tuple of ints. The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1.

This should work!

z = np.column_stack((x,y)) out = np.array([tuple(np.concatenate(z[0,:])), tuple(np.concatenate(z[1,:]))]).reshape((2,1))

**numpy.reshape() in Python,** numpy.reshape() in Python. Last Updated: 30-03-2020. About : numpy.reshape( array, shape, order = 'C') : shapes an array without changing data of array. In this article, you will learn, How to reshape numpy arrays in python using numpy.reshape () function. Before going further into article, first learn about numpy.reshape () function syntax and it’s parameters. Syntax: numpy.reshape (a, newshape, order=’C’) This function helps to get a new shape to an array without changing its data.

try

import numpy as np x = np.array([(1, 2, 3, 4, 5), (6, 7, 8, 9)]) y = np.array([(10, 11, 12, 13, 14), (15, 16, 17, 18)]) np.sum([x,y], axis=0)

array([(1, 2, 3, 4, 5, 10, 11, 12, 13, 14), (6, 7, 8, 9, 15, 16, 17, 18)], dtype=object)

Reshaping numpy arrays in python Reshape is an important feature which lets you to change the shape of your array without changing its data whereas ravel is used to get the 1D contiguous flattened array containing the input elements In this post we will see how ravel and reshape works and how it can be applied on a multidimensional array

In python, reshaping numpy array can be very critical while creating a matrix or tensor from vectors. In order to reshape numpy array of one dimension to n dimensions one can use np.reshape() method. Let’s check out some simple examples. It is very important to reshape you numpy array, especially you are training with some deep learning network.

numpy.reshape () function The reshape () function is used to give a new shape to an array without changing its data.

For reshaping a numpy-array containing numpy-arrays I found this contribution helpful - you can first use b=np.hstack (array_of_arrays) to create a flattened 1D numpy-array, and then just reshape b.

##### Comments

- Concatenate along axis 1
- Are you showing parentheses vs brackets accurately?
- Hi @stijn don' forget you can upvote and accept answers, see What should I do when someone answers my question?
- @yatu I did, but since I am new it did not register it or something
- Thanks, this did it for me!
- Note however that to define the inner arrays in a numpy array you should be using brackets not parenthesis. Otherwise you'll have an array of tuples and won't be able to apply most vectorised operations
- @Stijn. You should select this answer by clicking on the check mark next to it
- This gave me the following error: AxisError: axis 1 is out of bounds for array of dimension 1
- @Stijn. Aahh. You're really using tuples? Why?