How to Create a NumPy Array with Default Values?

In this article, we will look into some of the specialized NumPy Array creation functions to create a NumPy Array with default values

Using numpy.zeros()

The `np.zeros()` function is used to create an array where each element is initialized to `0`. This is particularly useful when you need an array of a certain size as a placeholder, which you plan to populate with other values later on.

```import numpy as np

# Creating a 2x3 array filled with zeros
arr = np.zeros((2, 3))

print("2x3 array filled with zeros:")
print(arr)
```

The output will be a 2×3 matrix with all elements being `0`.

```2x3 array filled with zeros:
[[0. 0. 0.]
[0. 0. 0.]]
```

Using numpy.ones()

The `np.ones()` creates an array where each element is initialized to `1`. This can serve as a starting point for operations that require an initial value of `1` for each element of an array.

```import numpy as np

# Creating a 3x4 array filled with ones
arr = np.ones((3, 4))

print("3x4 array filled with ones:")
print(arr)
```

This will output a 3×4 matrix with all elements being `1`.

```3x4 array filled with ones:
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
```

Using numpy.arange()

The `np.arange()` function is similar to the built-in Python `range()` function but returns a NumPy array. This function is used to create an array with a sequence of numbers from start (inclusive) to stop (exclusive) with a given step size.

```import numpy as np

# Creating an array of even numbers between 10
# (inclusive) and 20 (exclusive)
arr = np.arange(10, 20, 2)

print("Array of even numbers from 10 to 18:")
print(arr)
```

The output will be an array with elements `[10, 12, 14, 16, 18]`.

Using numpy.linspace()

The `np.linspace()` function generates an array with a specified number of elements that are spaced equally between the specified start and end values. This is particularly useful when you need to represent a range of values divided into a certain number of intervals.

```import numpy as np

# Creating an array with 5 elements
# between 0 and 1, both included
arr = np.linspace(0, 1, 5)

print("Array with 5 elements linearly spaced between 0 and 1:")
print(arr)
```

Output:

```Array with 5 elements linearly spaced between 0 and 1:
[0.   0.25 0.5  0.75 1.  ]
```

Using numpy.logspace()

For situations where you need to create an array with a logarithmic scale, `logspace` is the right tool. This is especially useful in scenarios dealing with exponential or logarithmic data.

```import numpy as np

# Create an array from 10^1 to 10^2 with 4 elements
arr = np.logspace(1, 2, 4)

print(arr)
```

This will generate numbers spaced evenly on a log scale from 10^1 (10) to 10^2 (100), with a default base of 10 i.e.

```[ 10.          21.5443469   46.41588834 100.        ]
```

Using numpy.full()

Sometimes you may need an array where each element is the same value. `np.full` fills this need:

```import numpy as np

# Create a 2x3 array where every element is 5
arr = np.full((2, 3), 5)

print(arr)
```

Output:

```[[5 5 5]
[5 5 5]]
```

Using numpy.eye()

To create an identity matrix, which is a square matrix with ones on the diagonal and zeros elsewhere, we can use the eye() method of numpy module. Like this,

```import numpy as np

# Create a 5x5 identity matrix
arr = np.eye(5)

print(arr)
```

Output:

```[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]```

Creating Empty NumPy Arrays

Finally, if you need an uninitialized array, `np.empty` is the function to use:

```import numpy as np

# Create an uninitialized array of shape (3, 4)
arr = np.empty((3, 4))

print(arr)
```
```[[4.67970426e-310 0.00000000e+000 6.91178578e-310 6.91177621e-310]
[6.91177660e-310 6.91178578e-310 6.91177620e-310 6.91177620e-310]
[6.91177620e-310 6.91177620e-310 6.91178578e-310 6.91177547e-310]]```

Do note that `np.empty` doesn’t actually populate with zeros. It just allocates the memory, so the contents are whatever happens to already be at those memory locations (often appearing as very small numbers close to zero).

Summary

Each of these functions is essential in different scenarios. For instance, `np.zeros()` and `np.ones()` can be used to create baseline arrays for computations, while `np.arange()` and `np.linspace()` are great for generating numerical sequences for iterations or graph plotting.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top