Manipulating NumPy Arrays

Creating and Manipulating NumPy Arrays


Creating and Manipulating NumPy Arrays


Creating NumPy Arrays

NumPy provides several functions to create arrays:

  • np.array(): Creates an array from a list or tuple.
  • np.zeros(): Creates an array filled with zeros.
  • np.ones(): Creates an array filled with ones.
  • np.empty(): Creates an uninitialized array (values are unpredictable).
import numpy as np
# Basic array
arr = np.array([1, 2, 3])
# Zeros array
zeros = np.zeros((2, 3))
# Ones array
ones = np.ones((2, 2))
# Empty array
empty = np.empty((2, 2))
print(arr)
print(zeros)
print(ones)
print(empty)



np.arange() vs np.linspace()

Both create arrays with evenly spaced values, but they differ in approach:

  • np.arange(start, stop, step): Generates values from start to stop (exclusive) with a specified step size.
  • np.linspace(start, stop, num): Generates a specified number of evenly spaced points between start and stop (inclusive).
import numpy as np
# arange example
arange_arr = np.arange(0, 10, 2)
# linspace example
linspace_arr = np.linspace(0, 10, 5)
print(arange_arr)  # [0 2 4 6 8]
print(linspace_arr)  # [ 0.  2.5  5.  7.5 10. ]



Random Numbers

NumPy's random module generates random numbers:

  • np.random.rand(shape): Generates random floats from 0 to 1 (uniform distribution).
  • np.random.randn(shape): Generates random floats from a standard normal distribution (mean 0, std 1).
  • np.random.randint(low, high, shape): Generates random integers from low (inclusive) to high (exclusive).
import numpy as np
# Uniform distribution
rand_arr = np.random.rand(2, 3)
# Normal distribution
randn_arr = np.random.randn(2, 2)
# Random integers
randint_arr = np.random.randint(1, 10, (2, 3))
print(rand_arr)
print(randn_arr)
print(randint_arr)




Reshaping Arrays

Change the shape of arrays without altering data:

  • reshape(new_shape): Reshapes the array to a new shape (must match total elements).
  • flatten(): Returns a 1D copy of the array.
  • ravel(): Returns a 1D view of the array (faster, modifies original if changed).
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Reshape to 3x2
reshaped = arr.reshape(3, 2)
# Flatten array
flattened = arr.flatten()
# Ravel array
raveled = arr.ravel()
print(reshaped)
print(flattened)
print(raveled)

Array Indexing and Slicing


Basic Slicing ([start:stop:step])

Access elements using start (inclusive), stop (exclusive), and step:

import numpy as np
arr = np.array([0, 1, 2, 3, 4, 5])
print(arr[1:4])  # [1 2 3]
print(arr[::2])  # [0 2 4]



Multidimensional Slicing

Slice multi-dimensional arrays with commas for each dimension:

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[0:2, 1:3])  # [[2 3] [5 6]]



Boolean Indexing

Use boolean conditions to filter elements:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[arr > 3])  # [4 5]



Fancy Indexing

Use arrays of indices to access specific elements:

import numpy as np
arr = np.array([10, 20, 30, 40, 50])
indices = [0, 2, 4]
print(arr[indices])  # [10 30 50]