Skip to main content

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-array\n", 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 7\n", 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 1\n", 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 0\n", 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 number\n", 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 n\nos\n",
    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.

Comments

Popular posts from this blog

Working with request header in Jersey (JAX-RS) guide

In the  previous post , we talked about, how to get parameters and their values from the request query string. In this guide learn how to get request header values in Jersey (JAX-RS) based application. We had tested or used the following tools and technologies in this project: Jersey (v 2.21) Gradle Build System (v 2.9) Spring Boot (v 1.3) Java (v 1.8) Eclipse IDE This is a part of  Jersey (JAX-RS) Restful Web Services Development Guides series. Please read Jersey + Spring Boot getting started guide . Gradle Build File We are using Gradle for our build and dependency management (Using Maven rather than Gradle is a very trivial task). File: build.gradle buildscript { ext { springBootVersion = '1.3.0.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' a

Ajax Cross Domain Resource Access Using jQuery

Some time back in our project we faced a problem while making an Ajax call using jQuery. Chrome Browser console had given some weird error message like below when we try to access one of our web pages: When we try to access the same web page in the Firefox browser, it doesn't give any error in the console but some parsing error occurred. In our case, we were accessing XML as an Ajax request resource. I was curious to check if the non-XML cross-domain resource was successfully loading or not. But finally, I realized that it is not going through. jersey-spring-boot-quick-starter-guide In our Ajax call, requesting domain was not the same as the requested URL domain. $.ajax({ url: "https://10.11.2.171:81/xxxxxx/xxxxxxx.xml" , type : "get" , success: function (response) { alert( "Load was performed." ); }, error : function (xhr, status) {

FastAPI first shot

Setup on my Mac (Macbook Pro 15 inch Retina, Mid 2014) Prerequisite Python 3.6+ (I used 3.7.x. I recently reinstalled OS after cleaning up disk, where stock Python 2.7 was available. I installed Pyenv and then used it to install 3.7.x). I already had a git repo initialized at Github for this project. I checked that out. I use this approach to keep all the source code safe or at a specific place 😀. I set the Python version in .python-version file. I also initialize the virtual environment using pyenv in venv folder. I started the virtual environment. FastAPI specific dependencies setup Now I started with basic pip commands to install dependency for the project. I saved dependencies in requirements.txt  the file. Minimal viable code to spin an API Server FastAPI is as cool as NodeJS or Go Lang (?) to demonstrate the ability to spin an API endpoint up and running in no time. I had the same feeling for the Flask too, which was also super cool. app/main.py: from typing i