04 Oct 2022

Posted on:

20 Jun 2022

0

# reshape function and its properties

I'd be infinitely grateful if I could get some clarification on this. New to python and just recently got introduced to the powerful python package called NumPy.

Question 1

In :

``````#let me illustrate my confusion
#Let's create a 1D array(vector) of type ndarray.
import numpy as np
array_1D = np.array([1,3,45])
array_1D
``````

Out:

``````array([ 1,  3, 45])
``````

In :

``````#let's find the shape of array_1D
array_1D.shape
#My expectation is a 1x3 vector but the output shows something else I don't know how to read or understand.
# I understand that the output is a tuple but what is the order of elements in that tuple?
#Is it (row,columns) or (columns,rows)?

``````

Out:

``````(3,)
``````

Question 2

In :

``````#Let's reshape the array to a 3x1 vector
array_1D.reshape(3,1)
#My expectation is that the dimension will not change and only the shape will
#The reasoning is that the number of "[" in the array equates to the number of dimensions right?
#If so why is output14 displaying a result with 2D

``````

Out:

``````array([[ 1],
[ 3],
])
``````

In :

``````#Let's check the shape of the array after reshaping
array_1D.shape
#No change in shape, but the dimension changed.why?? I mean it is quite the reverse to my thinking.What am I missing or misconstruing?

``````

Out:

``````(3,)
``````
Posted on:

04 Oct 2022

0

- Regarding Q1, generally, shape attribute generates (row, column) structure, however, for a vector it is not defined that way and you need to use 'reshape' method to specifically assign rows and columns shape or simply add another square brackets to convert in to a 2D array like that in below

``````array_1D = np.array([[1, 3, 45]])
print(array_1D.shape)  #this will produce a shape of (1, 3)
``````

``````array_1D = np.array([, , ])
print(array_1D.shape) #this will produce a shape of (3, 1)
``````

- Regarding Q2, yes, reshape converts 1D array into a 2D one by default so it changes both the shape and the dimensions
you can check by using 'ndim' attribute before and after the reshaping
{"ft_lang_label":"__label__en","ft_lang_prob":0.84643406,"math_prob":0.9151551,"size":2094,"snap":"2023-40-2023-50","text_gpt3_token_len":579,"char_repetition_ratio":0.12679426,"word_repetition_ratio":0.016997168,"special_character_ratio":0.28462273,"punctuation_ratio":0.12412178,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9893159,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-22T07:50:24Z\",\"WARC-Record-ID\":\"<urn:uuid:17e8abd2-405a-4399-b75f-09898a6c1b36>\",\"Content-Length\":\"110466\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:52bcf01f-27c1-4e69-bd96-ad95e71abe91>\",\"WARC-Concurrent-To\":\"<urn:uuid:bf6b61f5-e014-4951-83b5-b911ea9eaa74>\",\"WARC-IP-Address\":\"35.244.186.184\",\"WARC-Target-URI\":\"https://365datascience.com/question/reshape-function-and-its-properties/\",\"WARC-Payload-Digest\":\"sha1:ENU3XEUIM4UWYLFQYE4IPCF26VZTQOJ2\",\"WARC-Block-Digest\":\"sha1:UZM3R4RSQ7L2TDI7RH2IN4I2AJ3L25FN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506339.10_warc_CC-MAIN-20230922070214-20230922100214-00592.warc.gz\"}"}