← Back to NumPy Course | Chapter 5: Array Manipulation | Lesson 1 of 7

reshape() से Shape बदलना

reshape() same numbers को एक अलग grid layout में rearrange करता है।

In this page:

  1. reshape()
Syntax
python
arr.reshape(new_shape)
arr.reshape(rows, -1)

reshape()

जब तक total element count match करे reshape same data को एक नया shape देता है। एक dimension के लिए -1 इस्तेमाल करें और NumPy इसे work out कर लेगा। जब possible हो result original data का एक view है।

Note: एक single reshape call में सिर्फ एक dimension -1 हो सकती है।

उदाहरण: reshape()

python
import numpy as np

a = np.arange(12)
print(a.reshape(3, 4))
print(a.reshape(2, -1).shape)
print(a.reshape(2, 2, 3).shape)

# Output:
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]
# (2, 6)
# (2, 2, 3)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक shape चुनना जिसका product size से अलग हो
  2. -1 एक से ज़्यादा बार इस्तेमाल करना
  3. यह भूल जाना कि reshape एक नया array object return करता है
चैप्टर सारांश
  • reshape data रखता है लेकिन shape बदलता है
  • Total size match होनी चाहिए
  • -1 एक dimension infer करता है
  • अक्सर एक view return करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.