• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Numpy Tips / Numpy reshape function with examples

Numpy reshape function with examples

December 28, 2022 by cmdlinetips

NumPy’s reshape() function allows you to change the shape of an array. It takes two required arguments: the array you want to reshape and the new shape you want to give it.

Here is the basic syntax of Numpy’s reshape() function.

np.reshape(a, newshape, order='C')

It takes three arguments, the first is the array we want to reshape and the second is dimension of the new shape we want, and the third is how we want to reshape. By default, the third argument order is set to C-like index order.

Reshaping 1d array to 2d array with Numpy’s reshape()

Often you may need to change the shape of an array to fit a specific requirement. For example, you may have a one-dimensional array of 10 elements that you need to reshape into a two-dimensional array of 5 rows and 2 columns. You can use the reshape function for this purpose:

Here’s an example of reshaping 1d Numpy array into to 2d array using reshape() function.

Let us create a one-dimensional array.

my_array = np.arange(10)

Our array contains sequence of numbers from 0 to 9.

my_array

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

To reshape this one-dimensional array into 2d-array with 5 rows and 2 columns, we specify the new shape as tuple to the argument newshape

new_array = np.reshape(a = my_array,
                       newshape=(5, 2))

Our new reshaped array will be of the dimension 5×2. Note that the original array is not modified.

new_array
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

Numpy reshape() function: Example 2

Here is another example where we reshape a one-dimensional array into 2d arrray containing 3 rows and 3 columns.

my_array = np.arange(9)
new_array = my_array.reshape(3, 3)

new_array
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]]

Numpy reshape() function from 2d array to 1d-array: Example 2

Numpy’s reshape function can also be used in the reverse direction compared to the previous example. We can reshape a 2d-array into 1d-array by using -1 as argument to reshape() function.

In the example below, we use the 3×3 matrix from the previous example to convert into 1d-array.

new_array.reshape(-1)

array([0, 1, 2, 3, 4, 5, 6, 7, 8])

Here is the addition notes on the third argument of Numpy’s reshape() function.

‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. ‘A’ means to read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.

Here is an example to understand what happens when we choose the different options for the third argument “order”.

First, we reshape using the default order= C argument.

a = np.arange(12).reshape((3, 4))
a

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

Note the differences in index order between C-like and F-like options we have

np.reshape(np.arange(12), (3, 4), order='F')

array([[ 0,  3,  6,  9],
       [ 1,  4,  7, 10],
       [ 2,  5,  8, 11]])

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailNumpy arange() function with examples Default ThumbnailHow to Randomly permute Numpy Array Default ThumbnailNumpy isclose() function with examples Default ThumbnailHow To Concatenate Arrays in NumPy?

Filed Under: Numpy Tips Tagged With: NumPy reshape

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version