1

I have the following output for a

 [  1.   3.   5.   7.   9.  11.  13.  15.  17.  19.  21.  23.  25.  27.
    29.  31.  33.  35.  37.  39.  41.  43.  45.  47.  97.  99. 101. 103.
    105. 107. 109. 111. 113. 115. 117. 119. 121. 123. 125. 127. 129. 131.
    133. 135. 137. 139. 141. 143.]

I want to reshape it to the below

[[1.   3.   5.   7.   9.   11.  13.  15.]
 [17.  19.  21.  23.  25.  27.  29.  31.]
 [33.  35.  37.  39.  41.  43.  45.  47.]
 [97.  99. 101. 103.  105. 107. 109. 111.]
 [113. 115. 117. 119. 121. 123. 125. 127.]
 [129. 131. 133. 135. 137. 139. 141. 143.]]

I tried to use a.resize(6, 8), but it gives me this error: "resize only works on single-segment arrays" Also, when I am trying to use a.reshape(6, 8), it gives me the same array. I don't understand what is the reason for that as I have tested another array and worked well.

1
  • reshape does not work in-place. It returns a new array: b = a.reshape(6,8). The resize method does work in-place, but as a result is picky about the kind of array that it works with. Unless we need to change the total number of elements, we usually use reshape. Commented Nov 22, 2020 at 17:28

2 Answers 2

2

try a.reshape((8, 6)) notice the double parentheses

a = np.array([1., 3., 5., 7., 9., 11., 13., 15., 17., 19., 21., 23., 25., 27.,
              29., 31., 33., 35., 37., 39., 41., 43., 45., 47., 97., 99., 101., 103.,
              105., 107., 109., 111., 113., 115., 117., 119., 121., 123., 125., 127., 129., 131.,
              133., 135., 137., 139., 141., 143.])
print(a.reshape((8, 6)))

out:

[[  1.   3.   5.   7.   9.  11.]
 [ 13.  15.  17.  19.  21.  23.]
 [ 25.  27.  29.  31.  33.  35.]
 [ 37.  39.  41.  43.  45.  47.]
 [ 97.  99. 101. 103. 105. 107.]
 [109. 111. 113. 115. 117. 119.]
 [121. 123. 125. 127. 129. 131.]
 [133. 135. 137. 139. 141. 143.]]

Process finished with exit code 0

do notice that for the output you requested, the dimensions should be

a.reshape((6,8))

out:

[[  1.   3.   5.   7.   9.  11.  13.  15.]
 [ 17.  19.  21.  23.  25.  27.  29.  31.]
 [ 33.  35.  37.  39.  41.  43.  45.  47.]
 [ 97.  99. 101. 103. 105. 107. 109. 111.]
 [113. 115. 117. 119. 121. 123. 125. 127.]
 [129. 131. 133. 135. 137. 139. 141. 143.]]

Process finished with exit code 0

you can read about NumPy's reshape here: reshape documentation

Sign up to request clarification or add additional context in comments.

4 Comments

oh sorry, I made a mistake. the initial array that I have put here is my output. I editted that.
the initial array is numpy.ndarray.
x.reshape((4,5)) and x.reshape(4,5) are both ok. For np.zeros the tuple shape is required, but reshape method allows either.
I still couldnt understand. I have variable a which is a <class 'numpy.ndarray'>. I try reshape on other numpy.ndarray variables, and it workd but here it gives me the same array.
0

Try

b = a.reshape((8,6))

and keep in mind 2 things, for future use of similar methods:

  1. the reshape method takes a tuple as input, in that case (8,6) , calling b = a.reshape(8,6) gives 2 int arguments to the method instead of the tuple it expects. always pay attention to the expected values. you can investigate that by just hovering over a function in pycharm and most editors.

  2. in numpy, many methods do not manipulate the given object but rather return a new value for you to use. it is healthy to always check for that in documentation, in order to avoid catastrophic heartbreaks, trust me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.