Auto generate NumPy array

 

1. Intro

We are sometimes required to generate a NumPy array automatically for testing or other purposes. NumPy provides methods to create a NumPy array with the same numerical values, values between two numbers, and the identity matrix. In this tutorial, we will see code examples for most of the available methods. These methods are a great toolkit to decrease the time to quick scripting and prototyping.

There are other tutorials for creating NumPy array from an existing Python data structure and creating NumPy array from values kept in files.

index

2. Creating an empty NumPy array

As we know NumPy array is stored as a contagious block in memory. When we add or remove rows or columns in an existing array, the entire array is copied to a new block in memory. It is inefficient and creates a gap in memory for new elements.

One of the optimization techniques, we can use in the case, when we are adding rows, is to define an array of a size which we anticipate it ends up being. We can create an empty NumPy array of specified size quickly using numpy_.empty(size, dtype=int)_ method.

import numpy as np
print("Numpy Version is ", np.version)
%% Create an empty array of size (2,3)
size = (2, 3)
print("Empty array of size (2,3)n", np.empty(size, dtype=int))

OUTPUT:

Numpy Version is  1.15.4
Empty array of size (2,3)
[[0 0 0]
[0 0 0]]
Empty array of same size as a
[[4607182418800017408 4607182418800017408]
[4607182418800017408 0]]

3. Creating a NumPy array of the same size as an existing array

We can use numpy.empty-like(an-existing-array) method to create an empty array of the same size as an existing array. It is a handy tool for quickly creating another array.

# Create an empty array of same size as an existing array
## existing array

an-existing-array = np.array([[1, 2], [3, 4]])
print("Empty array of same size as an-existing-arrayn", np.empty-like(an-existing-array))

OUTPUT

Empty array of size (2,3)
[[0 0 0]
[0 0 0]]
Empty array of same size as an-existing-array
[[-1152921504606846976 -1152921504606846976]
[ 8 0]]

4. Creating a NumPy array with the specified diagonal value

We can use numpy.eye(number-of-rows, number-of-cols, index-of-diagonal) method to generate an array of a specified size with ones one diagonal and zeros elsewhere.

When index-of-diagonal is 0, one is used at the primary diagonal. When index-of-diagonal is positive value upper diagonal is used, whereas for a negative value lower diagonal.

#%%
# Create an array with 4 rows and 3 cols with 1 on diagonal and 0 on other places
number-of-rows = 4
number-of-cols = 3
# 0 for main diagonal, positive value as upper and negative value as lower diagonal
index-of-diagonal = 0
print(
"4 by 3 array with 1 on diagonal n",
np.eye(number-of-rows, number-of-cols, index-of-diagonal),
)
# Lower diagonal example
index-of-diagonal = -1
print(
"4 by 3 array with 1 on lower diagonal n",
np.eye(number-of-rows, number-of-cols, index-of-diagonal),
)

OUTPUT:

4 by 3 array with 1 on diagonal 
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
4 by 3 array with 1 on lower diagonal
[[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
3 by 3 identity array
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

5. Creating an identity matrix

Identity matrix of size n is n x n square matrix with ones on main diagonal. We use numpy__.identity(number-of-rows-and-cols) to create identity matrix.

# Identity Matrix of 3 x 3 size
number-of-rows-and-cols = 3
print("3 by 3 identity array n", np.identity(number-of-rows-and-cols))

#3 by 3 identity array
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

6. Creating an array with specific values

We can use numpy.full(shape, fill-value) method to create an array of specified sizes and values.

#%%
# Create 3 x 4 size array full of value 7
shape = (3, 4)
fill-value = 7
print("3 x 4 array full of value 7n", np.full(shape, fill-value))

OUTPUT:

3 x 4 array full of value 7
[[7 7 7 7]
[7 7 7 7]
[7 7 7 7]]

We can use numpy.ones(shape) to quickly create an array full of one value. We can alternatively use numpy.full() method.

# Shortcut for an array with full of 1
print("3 x 4 array full of value 1n", np.ones(shape))

OUTPUT:

3 x 4 array full of value 1
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]

We can also use numpy.zeroes(shape) to quickly create an array full of zero values.

# Shortcut for an array with full of 0
print("3 x 4 array full of value 0n", np.zeros(shape))

OUTPUT:

3 x 4 array full of value 0
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

7. Creating an array with values between two numbers

Use numpy.arange(start, stop, step) method to generate a one-dimensional array with evenly spaced values between a range.

#%%
# Generate 1d array with values between two numbers with specified step
start = 10
stop = 20
step = 0.75
print("Array with values between two numbern", np.arange(start, stop, step))

OUTPUT:

Array with values between two number
[10. 10.75 11.5 12.25 13. 13.75 14.5 15.25 16. 16.75 17.5 18.25
19. 19.75]

Use numpy.linspace(start, stop, number-of-samples-to-generate, endpoint=False) to generate a specified number of values between a range. Use endPoint=True|False to include and exclude the last value as a stop.

number-of-samples-to-generate = 5
print(
"Array with specified no of values between two nnosn",
np.linspace(start, stop, number-of-samples-to-generate, endpoint=False)
)

OUTPUT:

Array with specified no of values between two nos
[10. 12. 14. 16. 18.]

Use numpy.logspace(start, stop, number-of-samples-to-generate, endpoint=False) to generate a specified number of values spaced evenly on log space between a range.

print(
"Array with specified no of values spaced evenly on log space between two nos.n",
np.logspace(start, stop, number-of-samples-to-generate, endpoint=True)
)

OUTPUT:

Array with specified no of values spaced evenly on log space between two nos.
[1.00000000e+10 3.16227766e+12 1.00000000e+15 3.16227766e+17
1.00000000e+20]

Use numpy.geomspace(start, stop, number-of-samples-to-generate, endpoint=False) to generate the specified number of values spaced evenly on log space (geometric progression) between a range.

print(
"Array with specified no of values spaced evenly on log space (geomatric progression) between two nos.n",
np.geomspace(start, stop, number-of-samples-to-generate, endpoint=True)
)

OUTPUT:

Array with specified no of values spaced evenly on log space (geomatric progression) between two nos.
[10. 11.89207115 14.14213562 16.81792831 20. ]

8. Conclusion

In this tutorial, we learn several techniques to auto-generate NumPy arrays of various different values and shapes.

Please download the source code related to this tutorial here. You can run the Jupyter notebook for this tutorial here.

Leave a Comment

Your email address will not be published.