# NumPy Array Reshape and Resize

NumPy has two functions (and also methods) to change array shapes - `reshape` and `resize`. They have a significant difference that will our focus in this chapter.

## `numpy.reshape()`

Let’s start with the function to change the shape of array - `reshape()`.

``````import numpy as np

arrayA = np.arange(8)
# arrayA = array([0, 1, 2, 3, 4, 5, 6, 7])

np.reshape(arrayA, (2, 4))
#array([[0, 1, 2, 3],
#       [4, 5, 6, 7]])
``````

It converts a vector of 8 elements to the array of the shape of `(4, 2)`. It could be executed successfully because the amount of elements before and after reshape is identical. It raises `ValueError` if the amounts are different.

``````In : np.reshape(arrayA, (3, 4))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: cannot reshape array of size 8 into shape (3,4)
``````

Let’s take a closer look of the reshaped array. The first row is the first 4 data of `arrayA` and the second row takes the last 4. It fills the data in the order of row in this reshape conversion.

You need to change the parameter `order` if you want the order of filling data to be column.

``````In : np.reshape(arrayA, (2, 4), order='F')
Out: array([[0, 2, 4, 6],
[1, 3, 5, 7]])
``````

The default of `order` is `C` that means to read or write data in C-like index order, or in simple words, in the order of row. `F` means to read or write data in Fortan-like index order, or let’s say, in the order of `column`. You could refer to official NumPy documentation to know more details of different indexing methods.

## `ndarray.reshape()`

Besides the `reshape` function, NumPy has also `reshape` method in the `ndarray` object. The method has the same parameters as the function but without given array as a parameter.

``````In : arrayB = arrayA.reshape((2, 4))

In : arrayB
Out:	array([[0, 1, 2, 3],
[4, 5, 6, 7]])
In : arrayA
Out: array([0, 1, 2, 3, 4, 5, 6, 7])
``````

You could see, `reshape` method is similar to `reshape` function. And you should also be aware that `ndarray.reshape()` method doesn’t change data and shape of the original array but returns a new `ndarray` instance.

## `reshape()` Function/Method Shared Memory

The converted array in `reshape` function or method shares the same memory of the original array. You could think it as `shallow copy` in Python, where if you change the data in one array, the corresponding data in the other array is also modified.

``````In : arrayA = np.arange(8)
arrayB = arrayA.reshape((2, 4))
arrayB
Out:	array([[0, 1, 2, 3],
[4, 5, 6, 7]])
In : arrayA = 10
arrayA
Out: array([10, 1, 2, 3, 4, 5, 6, 7])
In : arrayB
Out:	array([[10, 1, 2, 3],
[4, 5, 6, 7]])
``````

## `numpy.resize()`

`numpy.resize()` is a bit similar to `reshape` in the sense of shape conversion. But it has some significant differences.

1. It doesn’t have `order` parameter. The order of `resize` is the same as `order='C'` in `reshape`.
2. If the number of elements of target array is not the same as original array, it will force to resize but not raise errors.

Let’s focus on the second difference.

``````In : arrayA = np.arange(8)
arrayB = np.resize(arrayA, (2, 4))
Out: array([[0, 1, 2, 3],
[4, 5, 6, 7]])
``````

The result is the same as that in `reshpae` if the element numbers are the same.

``````In : arrayC = np.resize(arrayA, (3, 4))
arrayC
Out: array([[0, 1, 2, 3],
[4, 5, 6, 7],
[0, 1, 2, 3]])
In : arrayD = np.resize(arrayA, (4, 4))
arrayD
Out: array([[0, 1, 2, 3],
[4, 5, 6, 7],
[0, 1, 2, 3],
[4, 5, 6, 7]])
``````

If the new array has more rows, it will repeat the data in the original array but not raise the error.

``````In : arrayE = np.resize(arrayA, (2, 2))
arrayE
Out: array([[0, 1],
[2, 3]])
In : np.resize(arrayA, (1,4))
Out: array([[0, 1, 2, 3]])
``````

If the number of elements in the new array is smaller, it fetches the number of elements it needs to fill in the new array in the order of row.

### `resize` Function/Method Memory

The new array doesn’t share the same memory with the original array in `resize` function/method. The data change in one array is not mapped to the other.

``````In : arrayA = np.arange(8)
arrayB = arrayA.reshape((2, 4))
arrayB
Out:	array([[0, 1, 2, 3],
[4, 5, 6, 7]])
In : arrayA = 10
arrayA
Out: array([10, 1, 2, 3, 4, 5, 6, 7])
In : arrayB
Out:	array([[0, 1, 2, 3],
[4, 5, 6, 7]])
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.74931484,"math_prob":0.98703,"size":4274,"snap":"2019-35-2019-39","text_gpt3_token_len":1392,"char_repetition_ratio":0.20163934,"word_repetition_ratio":0.168435,"special_character_ratio":0.36499766,"punctuation_ratio":0.21988527,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9870659,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-08-21T22:54:31Z\",\"WARC-Record-ID\":\"<urn:uuid:952239f2-914e-4885-997c-1f598ed37ebc>\",\"Content-Length\":\"20058\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:07f41886-c155-4db5-ae01-fac1cae9550f>\",\"WARC-Concurrent-To\":\"<urn:uuid:516dbb68-2cb6-4d1f-9cb5-e33cb1ba3618>\",\"WARC-IP-Address\":\"167.99.4.63\",\"WARC-Target-URI\":\"https://www.delftstack.com/tutorial/python-numpy/numpy-array-reshape-and-resize/\",\"WARC-Payload-Digest\":\"sha1:Q6VCYY4AMAINZ5HQMG5P7UUT7EM2567S\",\"WARC-Block-Digest\":\"sha1:YSY42EX2NROP4FG65RAIQDSGI5YQI55P\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-35/CC-MAIN-2019-35_segments_1566027316549.78_warc_CC-MAIN-20190821220456-20190822002456-00206.warc.gz\"}"}