NumPy(Numpy basic functions III) is a core library for scientific computing in Python. It offers powerful multi-dimensional array objects and a wide range of fast operations.
If you’re working with scientific data in Python, NumPy is essential.
Thanks to its efficient array handling and high-speed computations, it’s widely used in data analysis, machine learning, simulations, and more.

In this post, we’ll explore some basic ways to manipulate arrays using NumPy.

Numpy basic functions III Code

import numpy as np

a = np.arange(6)
print(a) # [0 1 2 3 4 5]
b = a.reshape((2, 3))
print(b) # [[0 1 2] [3 4 5]]

a = np.array([[1, 2, 3], [4, 5, 6]])
t = np.transpose(a)
print(t)
# [[1 4]
# [2 5]
# [3 6]]

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.concatenate((a, b))
print(c) # [1 2 3 4 5 6]

v = np.vstack((a, b))
print(v)
# [[1 2 3]
# [4 5 6]]

h = np.hstack((a, b))
print(h) # [1 2 3 4 5 6]

a = np.array([1, 2, 2, 3, 3, 3])
unique_elements = np.unique(a)
print(unique_elements) # [1 2 3]

a = np.array([1, 2, 3, 4, 5])
indices = np.where(a > 3)
print(indices) # (array([3, 4]),)

rand = np.random.rand(3, 3)
print(rand)

Output

[0 1 2 3 4 5]
[[0 1 2]
 [3 4 5]]
[[1 4]
 [2 5]
 [3 6]]
[1 2 3 4 5 6]
[[1 2 3]
 [4 5 6]]
[1 2 3 4 5 6]
[1 2 3]
(array([3, 4]),)
[[0.23758348 0.49269768 0.86850382]
 [0.70591613 0.89769441 0.38014438]
 [0.77178768 0.25535445 0.54001282]]

1. Creating and Reshaping Arrays

import numpy as np

a = np.arange(6)
print(a) # [0 1 2 3 4 5]
b = a.reshape((2, 3))
print(b) # [[0 1 2] [3 4 5]]
  • np.arange(): Create an array with a range of numbers
  • reshape(): Change the shape of an array (e.g., to a 2×3 matrix)

2. Transpose a Matrix

a = np.array([[1, 2, 3], [4, 5, 6]])
t = np.transpose(a)
print(t)
# [[1 4]
# [2 5]
# [3 6]]
  • np.transpose(): Create a transposed matrix

3. Combine Arrays

Horizontal Stacking
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
h = np.hstack((a, b))
print(h) # [1 2 3 4 5 6]
Vertical Stacking
v = np.vstack((a, b))
print(v)
# [[1 2 3]
# [4 5 6]]
General Stacking (concatenate)
c = np.concatenate((a, b))
print(c) # [1 2 3 4 5 6]

4. Extract Unique Values from an Array

a = np.array([1, 2, 2, 3, 3, 3])
unique_elements = np.unique(a)
print(unique_elements) # [1 2 3]
  • np.unique(): Get Unique Values Without Duplicates

5. Find Indexes Based on Conditions

a = np.array([1, 2, 3, 4, 5])
indices = np.where(a > 3)
print(indices) # (array([3, 4]),)
  • np.where(): Return Indexes of Elements That Meet a Condition

6. Generate Random Numbers

rand = np.random.rand(3, 3)
print(rand)
  • np.random.rand(): Create an Array with Uniform Random Numbers (0 to 1)
  • Generate a Matrix with a Given Shape (e.g., 3×3)

NumPy is an essential library for data analysis, machine learning, and scientific computing.
The array manipulation methods we covered in this post are some of the most commonly used functions in NumPy.

These are core features you’ll often use in real-world tasks.
If you get comfortable with NumPy, it’ll be much easier to work with libraries like Pandas or TensorFlow.
In the next post, we’ll dive into advanced array features like slicing and broadcasting.

By Mark

-_-

Leave a Reply

Your email address will not be published. Required fields are marked *