Convert 1d array to 2d python

I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this:

> import numpy as np
> A = np.array([1,2,3,4,5,6])
> B = vec2matrix(A,ncol=2)
> B
array([[1, 2],
       [3, 4],
       [5, 6]])

Does numpy have a function that works like my made-up function "vec2matrix"? (I understand that you can index a 1D array like a 2D array, but that isn't an option in the code I have - I need to make this conversion.)

Convert 1d array to 2d python

asked Sep 25, 2012 at 2:23

You want to reshape the array.

B = np.reshape(A, (-1, 2))

where -1 infers the size of the new dimension from the size of the input array.

nbro

14.3k27 gold badges103 silver badges188 bronze badges

answered Sep 25, 2012 at 2:27

Matt BallMatt Ball

347k98 gold badges634 silver badges699 bronze badges

0

You have two options:

  • If you no longer want the original shape, the easiest is just to assign a new shape to the array

    a.shape = (a.size//ncols, ncols)
    

    You can switch the a.size//ncols by -1 to compute the proper shape automatically. Make sure that a.shape[0]*a.shape[1]=a.size, else you'll run into some problem.

  • You can get a new array with the np.reshape function, that works mostly like the version presented above

    new = np.reshape(a, (-1, ncols))
    

    When it's possible, new will be just a view of the initial array a, meaning that the data are shared. In some cases, though, new array will be acopy instead. Note that np.reshape also accepts an optional keyword order that lets you switch from row-major C order to column-major Fortran order. np.reshape is the function version of the a.reshape method.

If you can't respect the requirement a.shape[0]*a.shape[1]=a.size, you're stuck with having to create a new array. You can use the np.resize function and mixing it with np.reshape, such as

>>> a =np.arange(9)
>>> np.resize(a, 10).reshape(5,2)

answered Sep 25, 2012 at 8:03

Pierre GMPierre GM

19k3 gold badges54 silver badges65 bronze badges

0

Try something like:

B = np.reshape(A,(-1,ncols))

You'll need to make sure that you can divide the number of elements in your array by ncols though. You can also play with the order in which the numbers are pulled into B using the order keyword.

answered Sep 25, 2012 at 4:19

JoshAdelJoshAdel

63.7k24 gold badges138 silver badges136 bronze badges

If your sole purpose is to convert a 1d array X to a 2d array just do:

X = np.reshape(X,(1, X.size))

answered Jan 14, 2020 at 18:09

Convert 1d array to 2d python

ArunArun

6358 silver badges13 bronze badges

convert a 1-dimensional array into a 2-dimensional array by adding new axis.

a=np.array([10,20,30,40,50,60])

b=a[:,np.newaxis]--it will convert it to two dimension.

derloopkat

6,05115 gold badges38 silver badges43 bronze badges

answered Jan 10, 2021 at 4:38

There is a simple way as well, we can use the reshape function in a different way:

A_reshape = A.reshape(No_of_rows, No_of_columns)

Convert 1d array to 2d python

Tamás Sengel

52.8k29 gold badges156 silver badges201 bronze badges

answered Jan 7, 2021 at 3:07

Convert 1d array to 2d python

1

You can useflatten() from the numpy package.

import numpy as np
a = np.array([[1, 2],
       [3, 4],
       [5, 6]])
a_flat = a.flatten()
print(f"original array: {a} \nflattened array = {a_flat}")

Output:

original array: [[1 2]
 [3 4]
 [5 6]] 
flattened array = [1 2 3 4 5 6]

answered Mar 20, 2019 at 16:19

Convert 1d array to 2d python

RafiRafi

192 bronze badges

2

some_array.shape = (1,)+some_array.shape

or get a new one

another_array = numpy.reshape(some_array, (1,)+some_array.shape)

This will make dimensions +1, equals to adding a bracket on the outermost

answered Apr 18, 2020 at 2:27

Convert 1d array to 2d python

ZDL-soZDL-so

3781 gold badge6 silver badges7 bronze badges

import numpy as np
array = np.arange(8) 
print("Original array : \n", array)
array = np.arange(8).reshape(2, 4)
print("New array : \n", array)

Convert 1d array to 2d python

Milo

3,2679 gold badges28 silver badges43 bronze badges

answered Nov 25, 2019 at 14:38

Convert 1d array to 2d python

2

Change 1D array into 2D array without using Numpy.

l = [i for i in range(1,21)]
part = 3
new = []
start, end = 0, part


while end <= len(l):
    temp = []
    for i in range(start, end):
        temp.append(l[i])
    new.append(temp)
    start += part
    end += part
print("new values:  ", new)


# for uneven cases
temp = []
while start < len(l):
    temp.append(l[start])
    start += 1
    new.append(temp)
print("new values for uneven cases:   ", new)

Convert 1d array to 2d python

answered Sep 6, 2019 at 10:37

Convert 1d array to 2d python

How do I convert a 1D array to a 2D array in Python?

Let's use this to convert our 1D numpy array to 2D numpy array,.
arr = np. array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).
# Convert 1D array to a 2D numpy array of 2 rows and 3 columns..
arr_2d = np. reshape(arr, (2, 5)).
print(arr_2d).

How do you convert a 1D array to a 2D array?

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping. To modify the layout of a NumPy ndarray, we will be using the reshape() method.

How do you convert 1D to 2D list in Python?

We also supply the values for the number of elements inside the 2D list to the program..
Using append and index. In this approach we will create a for loop to loop through each element in the 2D list and use it as an index for the new list to be created. ... .
Example. ... .
Output. ... .
Using islice. ... .
Example. ... .
Output..

How do you convert a 1D array of tuples to a 2D numpy array?

Yes it is possible to convert a 1 dimensional numpy array to a 2 dimensional numpy array, by using "np. reshape()" this function we can achiev this.