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

squeeze/expand_dims का इस्तेमाल

squeeze size-1 dimensions हटाता है और expand_dims एक वापस add करता है।

In this page:

  1. squeeze/expand_dims
Syntax
python
np.expand_dims(arr, axis)
np.squeeze(arr)

squeeze/expand_dims

expand_dims आपके चुने position पर length 1 का एक नया axis insert करता है, जो अक्सर broadcasting के लिए चाहिए। squeeze length 1 वाली axes हटाता है। आप np.newaxis से भी axes add कर सकते हैं।

Note: arr[:, np.newaxis] एक 1-D array को एक column में बदल देता है।

उदाहरण: squeeze/expand_dims

python
import numpy as np

a = np.array([1, 2, 3])
col = np.expand_dims(a, axis=1)
print(col.shape)
print(a[np.newaxis, :].shape)
print(np.squeeze(col).shape)

# Output:
# (3, 1)
# (1, 3)
# (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. एक ऐसी axis squeeze करना जिसकी length 1 न हो
  2. यह track खोना कि कौन-सी axis add हुई
  3. squeeze इस्तेमाल करना और unexpectedly एक ज़रूरी dimension collapse करना
चैप्टर सारांश
  • expand_dims एक length-1 axis add करता है
  • squeeze length-1 axes हटाता है
  • np.newaxis एक shorthand है
  • Broadcasting के लिए useful
🔒

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.