## 1. Intro

We sometimes require to generate a NumPy array automatically for testing or other purposes. NumPy provides methods to create 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

Python 3.6.5 and NumPy 1.15 is used. Visual Studio Code 1.30.2 used to run iPython interactive codes.

## 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, entire array copied to a new block in memory. It is inefficient and creates a gap in memory for new elements.

One of the optimisation technique, 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 of an existing array

We can use *numpy.empty_like(an_existing_array)* method to create an empty array of same size of 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 specified diagonal value

We can use *numpy.eye(number_of_rows, number_of_cols, index_of_diagonal)** *method to generate an array of specified size with ones one diagonal and zeros elsewhere.

When *index_of_diagonal* is 0, one is used at primary diagonal. When index_of_diagonal is positive value upper diagonal is used, whereas for 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 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 size 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 values. 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 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 specified number of values between a range. Use endPoint=True|False to include and exclude the last value as 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 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 (geomatric 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 shape.

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