numpy.column_stack#
Take a sequence of 1-D arrays and stack them as columns to make a single 2-D array. 2-D arrays are stacked as-is, just like with hstack . 1-D arrays are turned into 2-D columns first.
Parameters : tup sequence of 1-D or 2-D arrays.
Arrays to stack. All of them must have the same first dimension.
Returns : stacked 2-D array
The array formed by stacking the given arrays.
>>> a = np.array((1,2,3)) >>> b = np.array((2,3,4)) >>> np.column_stack((a,b)) array([[1, 2], [2, 3], [3, 4]])
pandas.DataFrame.unstack#
Pivot a level of the (necessarily hierarchical) index labels.
Returns a DataFrame having a new level of column labels whose inner-most level consists of the pivoted index labels.
If the index is not a MultiIndex, the output will be a Series (the analogue of stack when the columns are not a MultiIndex).
Parameters : level int, str, or list of these, default -1 (last level)
Level(s) of index to unstack, can pass level name.
fill_value int, str or dict
Replace NaN with this value if the unstack produces missing values.
sort bool, default True
Sort the level(s) in the resulting MultiIndex columns.
Returns : Series or DataFrame
Pivot a table based on column values.
Pivot a level of the column labels (inverse operation from unstack ).
Reference the user guide for more examples.
>>> index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'), . ('two', 'a'), ('two', 'b')]) >>> s = pd.Series(np.arange(1.0, 5.0), index=index) >>> s one a 1.0 b 2.0 two a 3.0 b 4.0 dtype: float64
>>> s.unstack(level=-1) a b one 1.0 2.0 two 3.0 4.0
>>> s.unstack(level=0) one two a 1.0 3.0 b 2.0 4.0
>>> df = s.unstack(level=0) >>> df.unstack() one a 1.0 b 2.0 two a 3.0 b 4.0 dtype: float64
What does -1 mean in numpy reshape?
It is quite simple, -1 means ‘whatever it takes’ to flatten. So, in the above example, a.reshape(2,-1) would mean 2*4, a.reshape(4,-1) would mean 4*2, a.reshape(2,2,-1) would mean 2,2,2 and just a.reshape(-1) would mean 8
Sep 21, 2022 at 23:46
12 Answers 12
The criterion to satisfy for providing the new shape is that ‘The new shape should be compatible with the original shape’
numpy allow us to give one of new shape parameter as -1 (eg: (2,-1) or (-1,3) but not (-1, -1)). It simply means that it is an unknown dimension and we want numpy to figure it out. And numpy will figure this by looking at the ‘length of the array and remaining dimensions’ and making sure it satisfies the above mentioned criteria
Now see the example.
z = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) z.shape (3, 4)
Now trying to reshape with (-1) . Result new shape is (12,) and is compatible with original shape (3,4)
z.reshape(-1) array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
Now trying to reshape with (-1, 1) . We have provided column as 1 but rows as unknown . So we get result new shape as (12, 1).again compatible with original shape(3,4)
z.reshape(-1,1) array([[ 1], [ 2], [ 3], [ 4], [ 5], [ 6], [ 7], [ 8], [ 9], [10], [11], [12]])
The above is consistent with numpy advice/error message, to use reshape(-1,1) for a single feature; i.e. single column
Reshape your data using array.reshape(-1, 1) if your data has a single feature
New shape as (-1, 2). row unknown, column 2. we get result new shape as (6, 2)
z.reshape(-1, 2) array([[ 1, 2], [ 3, 4], [ 5, 6], [ 7, 8], [ 9, 10], [11, 12]])
Now trying to keep column as unknown. New shape as (1,-1). i.e, row is 1, column unknown. we get result new shape as (1, 12)
z.reshape(1,-1) array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]])
The above is consistent with numpy advice/error message, to use reshape(1,-1) for a single sample; i.e. single row
Reshape your data using array.reshape(1, -1) if it contains a single sample
New shape (2, -1). Row 2, column unknown. we get result new shape as (2,6)
z.reshape(2, -1) array([[ 1, 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11, 12]])
New shape as (3, -1). Row 3, column unknown. we get result new shape as (3,4)
z.reshape(3, -1) array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]])
And finally, if we try to provide both dimension as unknown i.e new shape as (-1,-1). It will throw an error
z.reshape(-1, -1) ValueError: can only specify one unknown dimension
18.7k 20 20 gold badges 100 100 silver badges 136 136 bronze badges
answered Feb 28, 2017 at 13:48
Julu Ahamed Julu Ahamed
8,913 1 1 gold badge 13 13 silver badges 10 10 bronze badges
This answer contains a lot of examples but doesn’t lay out what -1 does in plain English. When reshaping an array, the new shape must contain the same number of elements as the old shape, meaning the products of the two shapes’ dimensions must be equal. When using a -1, the dimension corresponding to the -1 will be the product of the dimensions of the original array divided by the product of the dimensions given to reshape so as to maintain the same number of elements.
Apr 30, 2018 at 21:50
In my opinion the accepted answer and this answer are both helpful, whereas the accepted answer is more simple, I prefer the simpler answer
Aug 23, 2018 at 2:34
How is the shape (12, 1) «compatible» with shape (3,4)?
Feb 10, 2019 at 23:12
@Vijender I guess it means the same number of elements but different axis — i.e. 12×1 == 3×4?
Apr 21, 2020 at 0:19
a (12,1) array is a container with 12 elements. (3,4) and (2,6) arrays etc. also have 12 elements. They are compatible for reshaping the elements, which is the function in the OP’s question. Note that a (12×1) array would not be compatible with a (3×4) array for a whole bunch of functions eg np.matmul()
Jan 7, 2022 at 17:28
Say we have a 3 dimensional array of dimensions 2 x 10 x 10:
r = numpy.random.rand(2, 10, 10)
Now we want to reshape to 5 X 5 x 8:
numpy.reshape(r, shape=(5, 5, 8))
will do the job.
Note that, once you fix first dim = 5 and second dim = 5 , you don’t need to determine third dimension. To assist your laziness, Numpy gives the option of using -1 :
numpy.reshape(r, shape=(5, 5, -1))
will give you an array of shape = (5, 5, 8) .
numpy.reshape(r, shape=(50, -1))
will give you an array of shape = (50, 4)
19.5k 5 5 gold badges 54 54 silver badges 65 65 bronze badges
answered Mar 22, 2017 at 11:35
Anuj Gupta Anuj Gupta
6,348 7 7 gold badges 36 36 silver badges 55 55 bronze badges
TypeError: _reshape_dispatcher() got an unexpected keyword argument ‘shape’ use r.reshape(5,5,8) instead.
Feb 25, 2022 at 22:06
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. In this case, the value is inferred from the length of the array and remaining dimensions.
answered Sep 9, 2013 at 3:27
359k 63 63 gold badges 736 736 silver badges 638 638 bronze badges
In this case, the value is inferred to be [1, 8]. And 8 is the total number of matrix a. right?
Sep 9, 2013 at 3:35
@user2262504, I’m not sure. I think the value inferred is [8] because the documentation say so ( 1-D array ). Try numpy.reshape(a, [8]) . It yields same result with numpy.reshape(a, [1,8]) for the matrix.
Sep 9, 2013 at 3:55
-1 lets numpy determine for you the unknown number of columns or rows in the resulting matrix. Note: the unknown should be either columns or rows, not both.
May 10, 2017 at 10:07
numpy.reshape(a,newshape,order<>)
for the below example you mentioned the output explains the resultant vector to be a single row.(-1) indicates the number of rows to be 1. if the
a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]]) b = numpy.reshape(a, -1)
matrix([[1, 2, 3, 4, 5, 6, 7, 8]])
this can be explained more precisely with another example:
b = np.arange(10).reshape((-1,1))
output:(is a 1 dimensional columnar array)
array([[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]])
b = np.arange(10).reshape((1,-1))
output:(is a 1 dimensional row array)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
13.9k 5 5 gold badges 43 43 silver badges 60 60 bronze badges
answered Jan 2, 2017 at 16:41
Dinesh Kumar Dinesh Kumar
414 3 3 silver badges 7 7 bronze badges
It simply means that you are not sure about what number of rows or columns you can give and you are asking numpy to suggest number of column or rows to get reshaped in.
check below code and its output to better understand about (-1):
import numpy a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]]) print("Without reshaping -> ") print(a) b = numpy.reshape(a, -1) print("HERE We don't know about what number we should give to row/col") print("Reshaping as (a,-1)") print(b) c = numpy.reshape(a, (-1,2)) print("HERE We just know about number of columns") print("Reshaping as (a,(-1,2))") print(c) d = numpy.reshape(a, (2,-1)) print("HERE We just know about number of rows") print("Reshaping as (a,(2,-1))") print(d)
Without reshaping -> [[1 2 3 4] [5 6 7 8]] HERE We don't know about what number we should give to row/col Reshaping as (a,-1) [[1 2 3 4 5 6 7 8]] HERE We just know about number of columns Reshaping as (a,(-1,2)) [[1 2] [3 4] [5 6] [7 8]] HERE We just know about number of rows Reshaping as (a,(2,-1)) [[1 2 3 4] [5 6 7 8]]
NumPy: numpy.column_stack() function
Stack 1-D arrays as columns into a 2-D array.
Take a sequence of 1-D arrays and stack them as columns to make a single 2-D array. 2-D arrays are stacked as-is, just like with hstack. 1-D arrays are turned into 2-D columns first.
This function is useful when we want to combine two arrays in a column-wise fashion, which means we combine the arrays by their columns, i.e., we stack one array’s columns next to the other array’s columns.
The numpy.column_stack() function takes a sequence of 1-D or 2-D arrays and stacks them as columns into a 2-D array. If the input arrays are 1-D, then they will be converted to 2-D arrays first.
Syntax:
numpy.column_stack(tup)
Parameter:
Name | Description | Required / Optional |
---|---|---|
tup | Arrays to stack. All of them must have the same first dimension. | Required |
Return value:
stacked : 2-D array The array formed by stacking the given arrays.
Example: Column stacking two numpy arrays using numpy.column_stack()
>>> import numpy as np >>> x = np.array((3,4,5)) >>> y = np.array((4,5,6)) >>> np.column_stack((x,y)) array([[3, 4], [4, 5], [5, 6]])
The above code creates two numpy arrays «x» and «y» with values (3,4,5) and (4,5,6) respectively. Then, it calls the «np.column_stack()» function and passes both the arrays as arguments.
The «np.column_stack()» function stacks two 1-D arrays as columns into a 2-D array. The output of the function is a 2-D array with shape (3, 2) in this case, where each row contains elements from both input arrays.
Here, the function returns a 2-D array where the first column contains elements from array «x» and the second column contains elements from array «y». The resulting array is [[3, 4], [4, 5], [5, 6]]
Pictorial Presentation:
Python — NumPy Code Editor:
Previous: stack()
Next: dstack()
Follow us on Facebook and Twitter for latest update.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution — JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook