Numpy Tutorial

Creating NumPy Array

NumPy Array Manipulation

Matrix in NumPy

Operations on NumPy Array

Reshaping NumPy Array

Indexing NumPy Array

Arithmetic operations on NumPy Array

Linear Algebra in NumPy Array

NumPy and Random Data

Sorting and Searching in NumPy Array

Universal Functions

Working With Images

Projects and Applications with NumPy

How to create an empty and a full NumPy array?

Creating arrays with specific values or no initialized values is a common requirement in data science and machine learning workflows. This tutorial will guide you on how to create both empty and full NumPy arrays.

Create an Empty and a Full NumPy Array

1. Setup:

Ensure you have NumPy installed:

pip install numpy

Next, let's import NumPy:

import numpy as np

2. Creating an Empty Array:

An "empty" array in NumPy does not mean it contains no values. It means the array is filled with whatever values are present in memory at the moment. This method is faster because it avoids the initialization overhead, but the content is unpredictable. You should fill this array later before using it.

# Create an empty array of shape (3, 3)
empty_array = np.empty((3, 3))

print(empty_array)

3. Creating a Full Array:

If you want to initialize an array with a specific value, you can use the full() method.

# Create a 3x3 array filled with the number 7
full_array = np.full((3, 3), 7)

print(full_array)

4. Additional Tips:

  • If you want to create an array filled with zeros, use np.zeros().

    zeros_array = np.zeros((3, 3))
    print(zeros_array)
    
  • For an array filled with ones, use np.ones().

    ones_array = np.ones((3, 3))
    print(ones_array)
    
  • For an array with uninitialized values but of the same shape and type as another existing array, use np.empty_like() and np.full_like().

    existing_array = np.array([[1, 2, 3], [4, 5, 6]])
    
    empty_like_array = np.empty_like(existing_array)
    print(empty_like_array)
    
    full_like_array = np.full_like(existing_array, 7)
    print(full_like_array)
    

5. Conclusion:

Creating and initializing arrays with specific values (or without any initial values) is a foundational operation in NumPy. With methods like empty(), full(), zeros(), and ones(), NumPy provides a versatile toolkit for array creation, ensuring you can efficiently set up your arrays for any computational needs.

1. Copy NumPy array to another array in Python:

Copy a NumPy array to another array using the copy method.

import numpy as np

# Create a NumPy array
original_array = np.array([1, 2, 3, 4, 5])

# Copy the array to another array
copied_array = original_array.copy()

# Display the copied array
print("Original Array:", original_array)
print("Copied Array:", copied_array)

2. Deep copy vs shallow copy in NumPy arrays:

Understand the difference between deep copy and shallow copy.

import numpy as np

# Create a NumPy array
original_array = np.array([1, 2, 3, 4, 5])

# Shallow copy (view)
shallow_copy = original_array.view()

# Deep copy
deep_copy = original_array.copy()

# Modify the original array
original_array[0] = 10

# Display the arrays
print("Original Array:", original_array)
print("Shallow Copy (View):", shallow_copy)
print("Deep Copy:", deep_copy)

3. Copying arrays in NumPy:

Copy arrays using different methods in NumPy.

import numpy as np

# Create a NumPy array
original_array = np.array([1, 2, 3, 4, 5])

# Method 1: Using copy method
copied_array_1 = original_array.copy()

# Method 2: Using array function
copied_array_2 = np.array(original_array)

# Method 3: Using view method for a shallow copy
shallow_copy = original_array.view()

# Display the copied arrays
print("Original Array:", original_array)
print("Copied Array (Method 1):", copied_array_1)
print("Copied Array (Method 2):", copied_array_2)
print("Shallow Copy (View):", shallow_copy)

4. Python NumPy array assignment:

Assign one NumPy array to another using simple assignment.

import numpy as np

# Create a NumPy array
original_array = np.array([1, 2, 3, 4, 5])

# Assign the array to another array
assigned_array = original_array

# Display the assigned array
print("Original Array:", original_array)
print("Assigned Array:", assigned_array)

5. Copy part of a NumPy array to another array:

Copy a specific part of a NumPy array to another array.

import numpy as np

# Create a NumPy array
original_array = np.array([1, 2, 3, 4, 5])

# Copy a part of the array to another array
copied_part = original_array[1:4].copy()

# Display the arrays
print("Original Array:", original_array)
print("Copied Part:", copied_part)

6. NumPy copy vs assignment:

Understand the difference between NumPy copy and assignment.

import numpy as np

# Create a NumPy array
original_array = np.array([1, 2, 3, 4, 5])

# Method 1: Using copy method
copied_array = original_array.copy()

# Method 2: Using assignment
assigned_array = original_array

# Modify the original array
original_array[0] = 10

# Display the arrays
print("Original Array:", original_array)
print("Copied Array:", copied_array)
print("Assigned Array:", assigned_array)

7. Copying elements between NumPy arrays:

Copy elements between two NumPy arrays.

import numpy as np

# Create two NumPy arrays
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])

# Copy elements from array1 to array2
array2[:3] = array1[:3].copy()

# Display the arrays
print("Array1:", array1)
print("Array2:", array2)

8. How to duplicate a NumPy array in Python:

Duplicate a NumPy array using the np.repeat function.

import numpy as np

# Create a NumPy array
original_array = np.array([1, 2, 3, 4, 5])

# Duplicate the array
duplicated_array = np.repeat(original_array, 2)

# Display the arrays
print("Original Array:", original_array)
print("Duplicated Array:", duplicated_array)

9. NumPy array slicing and copying:

Use NumPy array slicing to copy elements.

import numpy as np

# Create a NumPy array
original_array = np.array([1, 2, 3, 4, 5])

# Copy elements using slicing
copied_array = original_array[:].copy()

# Display the arrays
print("Original Array:", original_array)
print("Copied Array:", copied_array)