0% found this document useful (0 votes)
3 views5 pages

NumPy Array Reshaping

The document explains NumPy array reshaping in a Q&A format, detailing its definition, necessity, and usage in Python. It covers the reshape() function, conditions for reshaping, and differences between reshape() and resize(). Additionally, it provides real-world applications and emphasizes that reshaping does not alter the data itself.

Uploaded by

shaanprasad277
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

NumPy Array Reshaping

The document explains NumPy array reshaping in a Q&A format, detailing its definition, necessity, and usage in Python. It covers the reshape() function, conditions for reshaping, and differences between reshape() and resize(). Additionally, it provides real-world applications and emphasizes that reshaping does not alter the data itself.

Uploaded by

shaanprasad277
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NumPy Array Reshaping

ChatGPT said:

Below is a NumPy Array Reshaping explanation in Hinglish, structured in Interview Q&A format,
suitable for Data Analyst / Python interviews.

Q1. What is NumPy Array Reshaping?

Answer:
NumPy array reshaping ka matlab hai array ka shape (dimensions) change karna bina data change kiye.
Reshape se hum 1D array ko 2D, 3D, ya multi-dimensional array me convert kar sakte hain.

Q2. Why do we need array reshaping?

Answer:
Array reshaping ki zarurat hoti hai:

 Machine Learning models ke input ke liye

 Matrix operations ke liye

 Data ko rows & columns me properly organize karne ke liye

 Broadcasting aur vectorized operations ke liye

Q3. Which function is used for reshaping in NumPy?

Answer:
NumPy me reshaping ke liye mainly use hota hai:

reshape()

Example:

import numpy as np arr = [Link]([1, 2, 3, 4]) [Link](2, 2)

Q4. Explain reshape() with an example.


Answer:

import numpy as np arr = [Link]([1, 2, 3, 4, 5, 6]) new_arr = [Link](2, 3) print(new_arr)

Output:

[[1 2 3]

[4 5 6]]

Yahan:

 2 = rows

 3 = columns

 Total elements = 6 (same as original)

Q5. What is the condition for reshaping an array?

Answer:
Total number of elements must remain same.

Formula:

rows × columns = total elements

Example:
✔ Possible: 6 → (2,3)
Not possible: 6 → (2,4)

Q6. What happens if reshape condition is violated?

Answer:
NumPy error throw karta hai:

ValueError: cannot reshape array

Example:

[Link](2, 4) # Error

Q7. What is -1 in reshape?


Answer:
-1 ka matlab hai NumPy khud dimension calculate karega.

Example:

arr = [Link]([1,2,3,4,5,6]) [Link](2, -1)

Output:

[[1 2 3]

[4 5 6]]

NumPy automatically column count calculate kar leta hai.

Q8. Can we reshape into 3D array?

Answer:
Yes.

Example:

arr = [Link]([1,2,3,4,5,6,7,8]) [Link](2, 2, 2)

Shape: (2,2,2)
2 blocks, each with 2 rows and 2 columns.

Q9. Difference between reshape() and resize()?

Answer:

reshape() resize()

New view return karta hai Original array change karta hai

Data same rehta hai Data repeat ya trim ho sakta hai

Safe operation Risky


Q10. What does shape tuple represent after reshaping?

Answer:
Shape tuple har dimension me kitne elements hain wo batata hai.

Example:

arr = [Link]([1,2,3,4,5,6]) [Link](1, 2, 3).shape

Output:

(1, 2, 3)

 1st dimension → 1 block

 2nd dimension → 2 rows

 3rd dimension → 3 columns

Q11. Is reshaping a copy or view?

Answer:
By default, reshape() view return karta hai (no extra memory).
Lekin agar possible na ho, to copy bana deta hai.

Q12. Real-world use case of reshaping?

Answer:

 Image processing (height × width × channels)

 ML model input formatting

 Converting flat data into tables

 Batch processing in deep learning

Interview Tip

Reshaping does NOT change data, only its structure.


Always check .shape before and after reshaping.

You might also like