Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Array Manipulation – Reshape, Split, Join, Sort, and Transform Arrays in Python

🐍 NumPy – Array Manipulation

Array manipulation is a core part of working with NumPy.

It allows you to modify, reshape, combine, and transform arrays efficiently without writing complex loops.

These operations are widely used in data science, machine learning, and scientific computing in Python.


What is Array Manipulation?

Array manipulation refers to changing the structure or content of NumPy arrays without losing data.

You can:

  • Reshape arrays
  • Join multiple arrays
  • Split arrays
  • Sort values
  • Insert or delete elements
  • Transpose matrices

🟢 1. Reshaping Arrays

Change the shape of an array without changing data.

import numpy as np

arr = np.arange(6)

new_arr = arr.reshape(2, 3)

print(new_arr)

🟢 2. Flattening Arrays

Convert multidimensional array into 1D.

arr = np.array([[1, 2], [3, 4]])

print(arr.flatten())

🟢 3. Ravel Function

Similar to flatten but returns a view.

print(arr.ravel())

🟡 4. Transpose

Swap rows and columns.

arr = np.array([[1, 2], [3, 4]])

print(arr.T)

🟡 5. Concatenation

Join arrays together.

a = np.array([1, 2])
b = np.array([3, 4])

print(np.concatenate((a, b)))

🟡 6. Stack Arrays

Vertical Stack

print(np.vstack((a, b)))

Horizontal Stack

print(np.hstack((a, b)))

🔵 7. Splitting Arrays

Divide arrays into multiple parts.

arr = np.array([1, 2, 3, 4, 5, 6])

print(np.array_split(arr, 3))

🔵 8. Inserting Elements

arr = np.array([1, 2, 3])

print(np.insert(arr, 1, 99))

Output:

[ 1 99  2  3]

🔵 9. Deleting Elements

arr = np.array([1, 2, 3])

print(np.delete(arr, 1))

Output:

[1 3]

🟣 10. Sorting Arrays

arr = np.array([3, 1, 2])

print(np.sort(arr))

Output:

[1 2 3]

🟣 11. Unique Values

arr = np.array([1, 1, 2, 3, 3])

print(np.unique(arr))

🧠 Why Array Manipulation is Important

1. Data Preprocessing

Used before feeding data into ML models.

2. Efficient Computation

Avoids loops and manual data handling.

3. Data Cleaning

Remove duplicates, reshape datasets, and fix structure.

4. Machine Learning

Models require properly shaped data.


📊 Common Manipulation Functions

FunctionPurpose
reshape()               Change shape
flatten()               Convert to 1D
ravel()               Flatten view
transpose()               Swap axes
concatenate()               Join arrays
split()               Divide arrays
insert()               Add elements
delete()               Remove elements
sort()               Sort values

🚀 Real-World Example

Preparing Dataset

import numpy as np

data = np.array([
[1, 2, 3],
[4, 5, 6]
])

data = data.T

print(data)

Used in:

  • Data preprocessing
  • Image processing
  • Machine learning pipelines

⚡ Best Practices

  • Use reshape instead of manual restructuring
  • Prefer vectorized operations
  • Avoid unnecessary copies
  • Use ravel() for memory efficiency
  • Always check array dimensions

🧾 Summary

NumPy array manipulation allows you to efficiently transform data structures.

Key operations include:

  • Reshaping
  • Splitting
  • Joining
  • Sorting
  • Transposing
  • Inserting and deleting

These tools are essential for working with numerical data in Python.


🏁 Conclusion

Array manipulation is a powerful feature of NumPy that simplifies data processing and improves performance.

Mastering these techniques is essential for data science, machine learning, and advanced Python programming.




Post a Comment

0 Comments