How do you convert 1d array to 2d in 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: Show
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.)
asked Sep 25, 2012 at 2:23
You want to
where
nbro 14.3k27 gold badges103 silver badges188 bronze badges answered Sep 25, 2012 at 2:27
Matt BallMatt Ball 348k98 gold badges634 silver badges699 bronze badges 0 You have two options:
If you can't respect the requirement
answered Sep 25, 2012 at 8:03
Pierre GMPierre GM 19k3 gold badges54 silver badges65 bronze badges 0 Try something like:
You'll need to make sure that you can divide the number of elements in your array by 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:
answered Jan 14, 2020 at 18:09
ArunArun 6358 silver badges13 bronze badges convert a 1-dimensional array into a 2-dimensional array by adding new axis.
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:
Tamás Sengel 52.9k29 gold badges156 silver badges201 bronze badges answered Jan 7, 2021 at 3:07
1 You can use
Output:
answered Mar 20, 2019 at 16:19
RafiRafi 192 bronze badges 2
or get a new one
This will make dimensions +1, equals to adding a bracket on the outermost answered Apr 18, 2020 at 2:27
ZDL-soZDL-so 3781 gold badge6 silver badges7 bronze badges
Milo 3,2679 gold badges28 silver badges43 bronze badges answered Nov 25, 2019 at 14:38
2 Change 1D array into 2D array without using Numpy.
answered Sep 6, 2019 at 10:37
How do you convert a 1D array to a 2D array in Python?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.
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 a 1D list to a 2D list in Python?Using islice. The islice function can be used to slice a given list with certain number of elements as required by the 2D list. So here week looked through each element of the 2D list and use that value 2 slice the original list. We need itertools package to use the islice function.
How do you make an array 2D in Python?Insert.py. # Write a program to insert the element into the 2D (two dimensional) array of Python.. from array import * # import all package related to the array.. arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements.. print("Before inserting the array elements: "). print(arr1) # print the arr1 elements.. |