<DataInsights />
  • 🏠 Home
  • 📊 SQL
  • 🐍 Python
  • 📈 Power BI
  • 📗 Excel
  • 💼 Career
  • 🎯 Interview Q&A
  • 📁 Case Study
  • 📥 Downloads
  • 🚀 My Portfolio
<DataInsights />

Practical Data Analytics tutorials covering SQL, Python, Power BI, Excel and career guidance for aspiring analysts — 100% free.

Topics

  • SQL Tutorials
  • Python Guide
  • Power BI
  • Excel Tips
  • Career Guide

Quick Links

  • 🛠️ All Tools
  • 🗓️ Archive
  • 📬 Contact
  • 🔍 Search
  • Portfolio
  • Kaggle
  • GitHub

Legal & Info

  • About
  • Contact
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • DMCA
  • Sitemap
Copyright © 2026 Data Insights by Jatin Kumar. All Rights Reserved.Built with ❤️ for Data Analysts
Home/Python/"NumPy Fundamentals — Arrays, Creation And Indexin...

"NumPy Fundamentals — Arrays, Creation And Indexing"

A
August 3, 2026 Jatin Kumar 23 min read Python
Data Insights NumPy Masterclass — Part 1

NumPy Fundamentals — Arrays, Creation & Indexing

Data Science ka backbone — NumPy. Arrays banane se lekar slicing tak, reshape se lekar broadcasting tak — sab kuch real-world examples aur interview questions ke saath Data Insights par.

📑 Is Part 1 Mein Aap Kya Sikhenge:

  • Topic 1: NumPy Kya Hai — Introduction & Why Use It
  • Topic 2: np.array() — Array Creation from Lists
  • Topic 3: np.zeros(), np.ones(), np.full() — Special Arrays
  • Topic 4: np.arange() & np.linspace() — Range Arrays
  • Topic 5: Array Indexing & Slicing — 1D & 2D
  • Topic 6: Array Shape — reshape(), flatten(), ravel()

1. NumPy Kya Hai — Introduction & Why Use It

text

🔍 Definition: NumPy (Numerical Python) is a fundamental Python library for numerical computing. It provides a powerful N-dimensional array object (ndarray), mathematical functions, linear algebra operations, and random number generation — all implemented in C for maximum performance.

🎯 Samjho Simple Bhasha Mein: Socho tumhare paas 1 lakh numbers hain aur tumhe sabka average nikalna hai. Python list se yeh karo toh loop lagana padega — slow hoga. NumPy array se ek line mein hoga aur 50x fast hoga! NumPy data science ka foundation hai — Pandas, Scikit-learn, TensorFlow sab NumPy ke upar bane hain.

💡 NumPy vs Python List — Key Differences:

Speed: NumPy C mein likha hai — Python list se 50-100x fast
Memory: NumPy less memory use karta hai — contiguous memory blocks
Operations: NumPy mein vectorized operations — loop ki zaroorat nahi
Data Type: NumPy array mein ek hi type — list mein mixed types

💻 Real-World Code Examples:

Example 1: NumPy install aur import karo.

# Install (terminal mein)
# pip install numpy

# Import karo
import numpy as np

# Version check
print(np.version)
print("NumPy ready!")

Example 2: NumPy vs Python List — Speed comparison.

import numpy as np
import time

# Python List approach
py_list = list(range(1000000))
start = time.time()
result = [x * 2 for x in py_list]
print(f"List time: {time.time() - start:.4f} sec")

# NumPy approach
np_arr = np.arange(1000000)
start = time.time()
result = np_arr * 2
print(f"NumPy time: {time.time() - start:.4f} sec")

📊 Expected Output:

1.26.0
NumPy ready!
List time: 0.1245 sec
NumPy time: 0.0021 sec

NumPy ~60x faster!

⚠️ Common Mistakes:

  • Mistake: import numpy likhna bina alias ke → Har jagah numpy.array() likhna padega.
    Fix: Hamesha import numpy as np use karo — industry standard hai.
  • Mistake: NumPy array mein mixed types dalna → np.array([1, 'hello', 3.5]) — sab string mein convert ho jayega!
    Fix: Ek hi type ka data rakho NumPy array mein.

💬 Interview Questions:

Q1: Why is NumPy faster than Python lists?
Ans: NumPy is implemented in C and stores data in contiguous memory blocks with a fixed data type. Python lists store pointers to Python objects scattered in memory. NumPy performs vectorized operations (SIMD instructions) on entire arrays at once without Python loops. This combination makes NumPy 50-100x faster for numerical operations.

Q2: What is the difference between NumPy array and Python list?
Ans: NumPy array: fixed size, homogeneous data type, contiguous memory, supports vectorized math operations, less memory usage. Python list: dynamic size, heterogeneous types allowed, non-contiguous memory, requires loops for math operations, more memory overhead per element.

Q3: What does ndarray stand for?
Ans: ndarray stands for N-dimensional array. It can be 1D (vector), 2D (matrix), 3D (tensor) or any higher dimension. The 'N' means it can have any number of dimensions, making it suitable for representing scalars, vectors, matrices, and higher-order tensors used in machine learning.

2. np.array() — Array Creation from Lists

text

🔍 Definition: np.array() creates an ndarray from a Python list, tuple, or nested list. It automatically detects the data type or you can specify it using the dtype parameter. The result is a NumPy array with powerful mathematical capabilities.

🎯 Samjho Simple Bhasha Mein: np.array() Python ki list ko NumPy array mein convert karta hai. Ek list diya — 1D array bana. List of lists diya — 2D array (matrix) bana. Isi tarah 3D, 4D bhi bana sakte hain. Array ka har element same type ka hota hai — yahi iska power hai.

💡 Important Array Properties:
arr.shape → Dimensions ka size (rows, cols)
arr.ndim → Number of dimensions
arr.dtype → Data type (int64, float64, etc.)
arr.size → Total number of elements
arr.itemsize → Each element ka size in bytes

💻 Real-World Code Examples:

Example 1: 1D, 2D aur 3D arrays banana.

import numpy as np
# 1D Array (Vector)
arr_1d = np.array([10, 20, 30, 40, 50])
print("1D Array:", arr_1d)
print("Shape:", arr_1d.shape)
print("Dimensions:", arr_1d.ndim)

# 2D Array (Matrix) — Student marks
marks = np.array([
[85, 90, 78], # Rahul: Math, Science, English
[92, 88, 95], # Priya
[70, 75, 80] # Amit
])
print("\n2D Array (marks):\n", marks)
print("Shape:", marks.shape) # (3 rows, 3 cols)
print("Total elements:", marks.size)

Example 2: dtype specify karna — data type control.

# Default dtype (auto-detect)
arr_int = np.array([1, 2, 3, 4])
print("Default dtype:", arr_int.dtype) # int64

# Specify float dtype
arr_float = np.array([1, 2, 3, 4], dtype=np.float64)
print("Float dtype:", arr_float.dtype)

# Boolean array
arr_bool = np.array([True, False, True], dtype=bool)
print("Bool array:", arr_bool)

# Type conversion
arr_str = np.array(['1', '2', '3'])
arr_converted = arr_str.astype(np.int32)
print("Converted:", arr_converted, "dtype:", arr_converted.dtype)

Example 3: Real-world — Employee salary data array.

# Employee salary data
salaries = np.array([75000, 85000, 92000, 68000,
55000, 110000, 48000, 95000],
dtype=np.float64)

print("Salaries:", salaries)
print("Shape:", salaries.shape)
print("Dtype:", salaries.dtype)
print("Memory (bytes):", salaries.nbytes)

📊 Expected Output:

1D Array: [10 20 30 40 50]
Shape: (5,)
Dimensions: 1

2D Array (marks):
[[85 90 78]
[92 88 95]
[70 75 80]]
Shape: (3, 3)
Total elements: 9

Default dtype: int64
Float dtype: float64
Bool array: [ True False True]
Converted: [1 2 3] dtype: int32

Salaries: [ 75000. 85000. 92000. 68000. 55000. 110000. 48000. 95000.]
Shape: (8,)
Dtype: float64
Memory (bytes): 64

text

⚠️ Common Mistakes:

  • Mistake: Unequal rows dena 2D array mein → np.array([[1,2,3],[4,5]]) — ragged array ban jaati hai, zyaadatar operations fail honge.
    Fix: Saari rows mein same number of elements hone chahiye.
  • Mistake: arr.shape ko arr.shape() call karna → shape property hai, method nahi — parentheses mat lagao.
    Fix: arr.shape ✅ — arr.shape() ❌
  • Mistake: Integer array mein float result expect karna → np.array([1,2,3]) / 2 → float result aayega Python 3 mein but dtype track karo.
    Fix: Explicit dtype=float64 specify karo financial calculations mein.

💬 Interview Questions:

Q1: What is the difference between shape, size and ndim?
Ans: shape returns a tuple of dimensions (e.g., (3,3) for a 3x3 matrix). ndim returns the number of dimensions (2 for a matrix, 1 for a vector). size returns total number of elements (9 for a 3x3 matrix). Example: arr = np.array([[1,2,3],[4,5,6]]) → shape=(2,3), ndim=2, size=6.

Q2: What happens when you create a NumPy array with mixed data types?
Ans: NumPy performs "upcasting" — it converts all elements to the most general type that can hold all values. int + float → float64. int + string → Unicode string. bool + int → int. This is called type promotion. To control this, explicitly specify dtype parameter.

Q3: What is the difference between astype() and dtype parameter?
Ans: dtype parameter sets the type during array creation: np.array([1,2,3], dtype=float64). astype() converts an existing array to a new type and returns a NEW array: arr.astype(np.float32). astype() creates a copy by default (original unchanged). Both are used for type conversion but at different stages.

3. np.zeros(), np.ones(), np.full() — Special Arrays

text

🔍 Definition: NumPy provides functions to create pre-filled arrays without manually listing values. np.zeros() creates arrays filled with 0s, np.ones() with 1s, np.full() with any specified value, np.eye() creates identity matrix, and np.empty() creates uninitialized array (random garbage values).

🎯 Samjho Simple Bhasha Mein: Kabhi kabhi tumhe ek placeholder array chahiye hoti hai — jaise Machine Learning mein weights initialize karna ho, ya ek empty grid banana ho. Manually sab zero likhna tedious hai. np.zeros() ek command mein koi bhi size ka zero-filled array de deta hai. Yeh initialization ke liye bahut use hota hai.

💡 Quick Reference:
np.zeros(shape) → All zeros
np.ones(shape) → All ones
np.full(shape, value) → All same value
np.eye(n) → Identity matrix (diagonal=1)
np.empty(shape) → Uninitialized (fastest)
np.zeros_like(arr) → Same shape as arr, all zeros

💻 Real-World Code Examples:

Example 1: Basic special arrays banana.

import numpy as np
# Zeros array
zeros_1d = np.zeros(5)
zeros_2d = np.zeros((3, 4)) # 3 rows, 4 cols
print("Zeros 1D:", zeros_1d)
print("Zeros 2D:\n", zeros_2d)

# Ones array
ones_2d = np.ones((2, 3), dtype=np.int32)
print("\nOnes 2D:\n", ones_2d)

# Full array — fill with specific value
full_arr = np.full((3, 3), 7)
print("\nFull array (7s):\n", full_arr)

# Identity matrix
identity = np.eye(4)
print("\nIdentity Matrix:\n", identity)

Example 2: Real-world — Sales tracking grid initialize karna.

# 4 products, 12 months ka sales grid initialize karo
# Pehle zero se shuru, baad mein data fill karenge
sales_grid = np.zeros((4, 12), dtype=np.float64)
print("Sales Grid (4 products x 12 months):")
print(sales_grid)
print("Shape:", sales_grid.shape)

# zeros_like — same shape as existing array
existing_data = np.array([[100, 200], [300, 400]])
reset_data = np.zeros_like(existing_data)
print("\nReset to zeros:", reset_data)

# ones_like for multiplication mask
multiplier = np.ones_like(existing_data, dtype=np.float64)
multiplier = multiplier * 1.1 # 10% increase
result = existing_data * multiplier
print("10% increased:", result)

📊 Expected Output:

Zeros 1D: [0. 0. 0. 0. 0.]
Zeros 2D:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

Ones 2D:
[[1 1 1]
[1 1 1]]

Full array (7s):
[[7 7 7]
[7 7 7]
[7 7 7]]

Identity Matrix:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]

Reset to zeros: [[0 0] [0 0]]
10% increased: [[110. 220.] [330. 440.]]

text

⚠️ Common Mistakes:

  • Mistake: np.zeros(3, 4) likhna → Error! Shape tuple mein deni hai.
    Fix: np.zeros((3, 4)) — double parentheses zaroori hain 2D ke liye.
  • Mistake: np.empty() mein values 0 assume karna → Empty array mein garbage/random values hoti hain — use karne se pehle fill karo.
    Fix: Agar zeros chahiye toh np.zeros() use karo — empty performance ke liye hai jab tum immediately overwrite karoge.

💬 Interview Questions:

Q1: What is the difference between np.zeros() and np.empty()?
Ans: np.zeros() allocates memory AND fills all values with 0 — safe to use immediately. np.empty() only allocates memory without initializing values — contains random garbage from previous memory usage. np.empty() is slightly faster because it skips the initialization step. Use np.empty() only when you plan to fill all values before reading.

Q2: What is an Identity Matrix and when is it used?
Ans: An identity matrix (np.eye(n)) is a square matrix with 1s on the main diagonal and 0s everywhere else. It is the matrix equivalent of the number 1 — multiplying any matrix by the identity matrix returns the original matrix unchanged. Used in linear algebra, solving systems of equations, and as initialization in deep learning weight matrices.

4. np.arange() & np.linspace() — Range Arrays

text

🔍 Definition: np.arange(start, stop, step) creates evenly spaced values within a given interval using a step size — similar to Python's range() but returns an array. np.linspace(start, stop, num) creates exactly num evenly spaced values between start and stop (both inclusive by default).

🎯 Samjho Simple Bhasha Mein: np.arange() step size bata ke values banata hai — "0 se 10 tak, har 2 step pe." np.linspace() count bata ke values banata hai — "0 se 1 ke beech exactly 5 values chahiye." Plotting ke liye linspace zyada use hota hai — exact count chahiye hota hai. Data engineering mein arange zyada use hota hai.

💡 arange vs linspace — Key Difference:
arange: Step size specify karo → count auto-determine
linspace: Count specify karo → step size auto-calculate

np.arange(0, 1, 0.1) → [0, 0.1, 0.2...] (10 values, stop excluded)
np.linspace(0, 1, 11) → [0, 0.1, 0.2...1.0] (11 values, stop included)

💻 Real-World Code Examples:

Example 1: np.arange() — step-based sequences.

import numpy as np
# Basic arange
arr1 = np.arange(10) # 0 to 9
arr2 = np.arange(1, 11) # 1 to 10
arr3 = np.arange(0, 20, 2) # 0 to 18, step 2 (even numbers)
arr4 = np.arange(10, 0, -1) # 10 to 1 (reverse)

print("0 to 9:", arr1)
print("1 to 10:", arr2)
print("Even numbers:", arr3)
print("Reverse:", arr4)

# Float step
float_arr = np.arange(0.0, 1.0, 0.25)
print("Float step:", float_arr)

Example 2: np.linspace() — count-based sequences.

# Basic linspace
arr5 = np.linspace(0, 10, 5) # 5 values from 0 to 10
arr6 = np.linspace(0, 1, 11) # 11 values from 0 to 1
print("5 values 0-10:", arr5)
print("11 values 0-1:", arr6)

# endpoint=False (exclude end)
arr7 = np.linspace(0, 10, 5, endpoint=False)
print("Exclude end:", arr7)

# Real-world: Temperature analysis points
temps = np.linspace(0, 100, 9) # 9 evenly spaced temperature points
print("Temperature points:", temps)

# Real-world: Time series (0 to 2π for sine wave)
import numpy as np
time_points = np.linspace(0, 2 * np.pi, 100)
sine_wave = np.sin(time_points)
print("Sine wave first 5:", np.round(sine_wave[:5], 3))

📊 Expected Output:

0 to 9: [0 1 2 3 4 5 6 7 8 9]
1 to 10: [ 1 2 3 4 5 6 7 8 9 10]
Even numbers: [ 0 2 4 6 8 10 12 14 16 18]
Reverse: [10 9 8 7 6 5 4 3 2 1]
Float step: [0. 0.25 0.5 0.75]

5
values 0-10: [ 0. 2.5 5. 7.5 10. ]
11
values 0-1: [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
Exclude
end: [0. 2. 4. 6. 8.]
Temperature points: [ 0. 12.5 25. 37.5 50. 62.5 75. 87.5 100. ]
Sine wave first 5: [0. 0.063 0.125 0.187 0.248]

⚠️ Common Mistakes:

  • Mistake: arange mein float step dena → Floating point precision errors aa sakte hain — count unpredictable ho sakti hai.
    Fix: Float ranges ke liye np.linspace() prefer karo — exact count guaranteed.
  • Mistake: arange ki stop value included samajhna → arange stop value EXCLUDE karta hai (like Python range).
    Fix: np.arange(1, 11) gives 1 to 10 — 11 included nahi. linspace mein stop included hai by default.

💬 Interview Questions:

Q1: What is the difference between np.arange() and np.linspace()?
Ans: np.arange(start, stop, step) generates values by specifying step size — stop is excluded, and the total count depends on the step. np.linspace(start, stop, num) generates exactly num evenly spaced values — stop is included by default. For float ranges, linspace is preferred because arange with float steps can have floating-point precision issues leading to unpredictable element counts.

Q2: How do you create an array of 100 evenly spaced values between 0 and 2π for plotting a sine wave?
Ans: x = np.linspace(0, 2*np.pi, 100) then y = np.sin(x). linspace is ideal here because we need an exact count (100 points) over a specified range. The evenly spaced points ensure smooth curve representation. This is one of the most common NumPy patterns in data visualization.

5. Array Indexing & Slicing — 1D & 2D

text

🔍 Definition: Array indexing accesses individual elements using their position (index starts at 0). Slicing extracts a subset using start:stop:step notation. NumPy supports standard Python slicing plus advanced indexing methods: boolean indexing and fancy indexing (using arrays as indices).

🎯 Samjho Simple Bhasha Mein: Indexing matlab "mujhe exactly yeh element chahiye." Slicing matlab "mujhe yahan se wahan tak ke elements chahiye." 2D array mein dono dimensions specify karne padte hain — row aur column. NumPy mein slicing original array ka VIEW deta hai — copy nahi! Yeh bahut important hai — slice modify karo toh original bhi change hoga.

💡 Indexing Rules:
Positive index: 0 se shuru (left to right)
Negative index: -1 se shuru (right to left)
Slice: arr[start:stop:step] — stop excluded
2D: arr[row, col] or arr[row][col]
View vs Copy: Slice → View. arr.copy() → New copy

💻 Real-World Code Examples:

Example 1: 1D Array indexing aur slicing.

import numpy as np
salaries = np.array([75000, 85000, 92000, 68000,
55000, 110000, 48000, 95000])

# Basic indexing
print("First salary:", salaries[0]) # 75000
print("Last salary:", salaries[-1]) # 95000
print("Third salary:", salaries[2]) # 92000

# Slicing
print("First 3:", salaries[:3])
print("Last 3:", salaries[-3:])
print("Middle (2-5):", salaries[2:5])
print("Every 2nd:", salaries[::2])
print("Reversed:", salaries[::-1])

Example 2: 2D Array indexing — marks matrix.

# 2D: Students x Subjects marks
# Subjects: Math, Science, English, History
marks = np.array([
[85, 90, 78, 88], # Rahul
[92, 88, 95, 70], # Priya
[70, 75, 80, 65], # Amit
[88, 92, 85, 90] # Sneha
])

# Single element
print("Priya's Science marks:", marks[1, 1]) # Row 1, Col 1

# Entire row (Rahul's all marks)
print("Rahul's marks:", marks[0, :])

# Entire column (all Math marks)
print("Math marks:", marks[:, 0])

# Sub-matrix (first 2 students, first 2 subjects)
print("Sub-matrix:\n", marks[:2, :2])

# Last row, last column
print("Sneha's History:", marks[-1, -1])

Example 3: Boolean Indexing — condition-based filtering.

# Boolean Indexing — condition se filter
salaries = np.array([75000, 85000, 92000, 68000,
55000, 110000, 48000, 95000])

# Create boolean mask
high_salary_mask = salaries > 80000
print("Mask:", high_salary_mask)

# Apply mask
high_earners = salaries[high_salary_mask]
print("High earners (>80K):", high_earners)

# One-liner
mid_range = salaries[(salaries >= 60000) & (salaries 90000)]
print("Mid range (60K-90K):", mid_range)

# Fancy Indexing — array of indices
selected = salaries[[0, 2, 5]] # 1st, 3rd, 6th element
print("Selected (index 0,2,5):", selected)

📊 Expected Output:

First salary: 75000
Last salary: 95000
Third salary: 92000
First 3: [75000 85000 92000]
Last 3: [110000 48000 95000]
Middle (2-5): [92000 68000 55000]
Every 2nd: [75000 92000 55000 48000]
Reversed: [ 95000 48000 110000 55000 68000 92000 85000 75000]

Priya's Science marks: 88
Rahul's marks: [85 90 78 88]
Math marks: [85 92 70 88]
Sub-matrix:
[[85 90]
[92 88]]
Sneha's History: 90

Mask: [False True True False False True False True]
High earners (>80K): [ 85000 92000 110000 95000]
Mid range (60K-90K): [75000 85000 68000]
Selected (index 0,2,5): [ 75000 92000 110000]

text

⚠️ Common Mistakes:

  • Mistake: Slice ko copy samajhna → b = a[2:5] — b change karo toh a bhi change hoga! Yeh VIEW hai.
    Fix: Independent copy chahiye toh b = a[2:5].copy() use karo.
  • Mistake: Boolean indexing mein and/or use karna → salaries > 80000 and salaries < 90000 — Error!
    Fix: & (and), | (or), ~ (not) use karo NumPy mein. Conditions ko parentheses mein wrap karo.
  • Mistake: 2D mein arr[1][2] vs arr[1, 2] → Dono kaam karte hain but arr[1, 2] efficient hai (ek lookup).
    Fix: arr[row, col] prefer karo — faster aur cleaner.

💬 Interview Questions:

Q1: What is the difference between a View and a Copy in NumPy?
Ans: A View shares the same memory as the original array — modifying the view changes the original. Slicing creates a view. A Copy is an independent array with its own memory — changes don't affect the original. arr.copy() creates a copy. Fancy indexing (using array of indices) and boolean indexing always return copies, while basic slicing returns views.

Q2: How do you select all rows but only specific columns in a 2D array?
Ans: Use arr[:, [col1, col2]] for fancy indexing or arr[:, start:stop] for slice. Example: marks[:, 0] selects all rows, first column only. marks[:, :2] selects all rows, first two columns. marks[:, [0, 2]] selects all rows, columns 0 and 2.

Q3: Why use & instead of 'and' for boolean conditions in NumPy?
Ans: Python's 'and' operator works on single boolean values and cannot be applied element-wise to arrays. NumPy's & (bitwise AND) operator works element-wise on boolean arrays. Using 'and' raises "ValueError: The truth value of an array is ambiguous." Always use & for AND, | for OR, ~ for NOT, and wrap each condition in parentheses.

6. Array Shape — reshape(), flatten(), ravel()

text

🔍 Definition: Shape manipulation functions change the structure of an array without changing its data. reshape() gives a new shape to the array. flatten() converts to 1D and returns a copy. ravel() converts to 1D and returns a view. transpose() flips rows and columns.

🎯 Samjho Simple Bhasha Mein: Socho 12 numbers hain — tumhe kabhi 3x4 matrix chahiye, kabhi 4x3, kabhi 2x6, kabhi 1D. Reshape se yeh sab possible hai — data same rehta hai, sirf shape change hoti hai. Machine Learning mein yeh bahut zaroori hai — images 3D hoti hain (pixels x pixels x channels) lekin neural network ko 1D input chahiye — flatten se convert karte hain.

💡 reshape() Rules:
Total elements same rehne chahiye: 3x4=12 → 2x6=12 ✅ → 2x5=10 ❌
-1 use karo ek dimension auto-calculate ke liye
arr.reshape(3, -1) → 3 rows, columns auto
arr.reshape(-1) → Flatten to 1D automatically

💻 Real-World Code Examples:

Example 1: reshape() — array structure change karo.

import numpy as np
# 12 elements ka 1D array
arr = np.arange(1, 13)
print("Original 1D:", arr)

# Reshape to 2D
arr_3x4 = arr.reshape(3, 4) # 3 rows, 4 cols
arr_4x3 = arr.reshape(4, 3) # 4 rows, 3 cols
arr_2x6 = arr.reshape(2, 6) # 2 rows, 6 cols

print("\n3x4 Matrix:\n", arr_3x4)
print("\n4x3 Matrix:\n", arr_4x3)

# Auto-calculate one dimension using -1
arr_auto = arr.reshape(3, -1) # 3 rows, NumPy figures out cols (4)
print("\nAuto reshape (3, -1):\n", arr_auto)

# Reshape to 3D
arr_3d = arr.reshape(2, 2, 3) # 2 blocks, 2 rows, 3 cols
print("\n3D Array (2x2x3):\n", arr_3d)

Example 2: flatten(), ravel() aur transpose().

# 2D array
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

# flatten() — returns COPY, always 1D
flat_copy = matrix.flatten()
flat_copy[0] = 999 # Change copy
print("Flattened copy:", flat_copy)
print("Original unchanged:\n", matrix)

# ravel() — returns VIEW (if possible)
raveled = matrix.ravel()
raveled[0] = 100 # Change view
print("Raveled:", raveled)
print("Original CHANGED:\n", matrix) # matrix[0,0] = 100!

# Transpose — rows become columns
matrix2 = np.array([[1, 2, 3],
[4, 5, 6]]) # Shape: (2,3)
transposed = matrix2.T # Shape: (3,2)
print("\nOriginal (2x3):\n", matrix2)
print("Transposed (3x2):\n", transposed)

📊 Expected Output:

Original 1D: [ 1 2 3 4 5 6 7 8 9 10 11 12]
3x4 Matrix:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

4x3 Matrix:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

Auto reshape (3, -1):
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

Flattened copy: [999 2 3 4 5 6 7 8 9]
Original unchanged:
[[1 2 3] [4 5 6] [7 8 9]]

Original CHANGED:
[[100 2 3]
[ 4 5 6]
[ 7 8 9]]

Original (2x3):
[[1 2 3] [4 5 6]]
Transposed (3x2):
[[1 4] [2 5] [3 6]]

text

⚠️ Common Mistakes:

  • Mistake: reshape mein incompatible shape dena → arr.reshape(3, 5) for 12 elements → ValueError: cannot reshape array of size 12 into shape (3,5).
    Fix: Total elements same hona chahiye: 3×5=15 ≠ 12. Use -1 to auto-calculate.
  • Mistake: flatten() aur ravel() ko same samajhna → flatten() hamesha copy. ravel() hamesha view (agar possible).
    Fix: Original protect karna ho → flatten(). Performance chahiye → ravel().
  • Mistake: reshape ke baad original changed samajhna → reshape() ek VIEW return karta hai (mostly). Original data same memory mein.
    Fix: Agar independent reshape chahiye → arr.reshape(...).copy()

💬 Interview Questions:

Q1: What is the difference between flatten() and ravel()?
Ans: Both convert N-dimensional array to 1D. flatten() always returns a copy — modifying it does not affect the original. ravel() returns a view when possible (same memory) — modifying it changes the original. flatten() uses more memory but is safer. ravel() is faster and memory-efficient. Use ravel() for temporary operations, flatten() when you need an independent 1D array.

Q2: How does reshape(-1) work?
Ans: -1 in reshape means "calculate this dimension automatically based on total elements and other specified dimensions." arr.reshape(-1) flattens to 1D regardless of original shape. arr.reshape(3, -1) creates 3 rows and automatically calculates columns (total/3). arr.reshape(-1, 1) creates a column vector (n rows, 1 column). It's a convenient shorthand for unknown dimension calculation.

Q3: When do you use transpose in data science?
Ans: Transpose (arr.T) flips rows and columns. Common uses: converting row vectors to column vectors for matrix multiplication, aligning dimensions for dot products in neural networks, converting feature matrices where you need features as rows instead of columns, and in linear algebra operations. In deep learning, data shapes frequently need transposition before layer operations.

Summary — NumPy Part 1 Quick Reference

Function Purpose Returns Key Note
np.array() List to array ndarray dtype auto-detect
np.zeros() All zeros array ndarray Shape as tuple
np.ones() All ones array ndarray Good for masks
np.arange() Step-based range ndarray Stop excluded
np.linspace() Count-based range ndarray Stop included
reshape() Change shape View/ndarray Total elements same
flatten() To 1D Copy Safe, independent
ravel() To 1D View Fast, shares memory
arr.T Transpose View Rows ↔ Columns

Next: Data Insights NumPy Masterclass — Part 2

Part 2 mein hum cover karenge: Array Operations — Arithmetic, Broadcasting, Comparison, np.where(), aur Math Functions with real-world data analysis examples Data Insights par.

Happy Coding & Keep Learning! 🚀

👤
Jatin Kumar
Data Analyst & Educator

Python, SQL, Power BI aur Excel mein practical tutorials likhta hoon — taaki data analytics seekhna aasan ho. Portfolio: jatinanalytics.co.in

Portfolio LinkedIn GitHub Kaggle All Articles
Share:

💬 Comments (0)

Spam/links allowed nahi hain — respectful comments welcome!

Loading comments...

Was this article helpful?
Previous ArticleAdvanced Charts, Graph Objects And Export in PlotlyNext Article "Array Operations — Arithmetic, Broadcasting, Comparison And

📚 More Articles Like This

Interactive Charts with Plotly Express

Read Article

Relationships, Matrix Charts And Styling in Seaborn

Read Article

Categorical Charts in Seaborn

Read Article