# Creating NumPy Arrays

November 24, 2020 2021-02-26 21:51## Creating NumPy Arrays

There are several ways to create NumPy arrays and in this lesson, you will be going through how to create NumPy arrays using Python data structures as well as NumPy’s built-in functions.

## Create NumPy Arrays from Python Data Structures

A common way of creating NumPy arrays is by passing a Python data structure such as Python List or Python Tuples as a parameter of NumPy’s `array()`

method.

The general syntax for NumPy’s `array()`

method is as follows:

`numpy.array(object, dtype, *args)`

The basic parameters that need to be passed into this function are:

**object**: An object that returns a sequence or a nested sequence such as a Python List or a Python Tuple.**dtype:**Data type of the desired array. If the datatype is not given, the type will be determined as the minimum type required to hold the objects in the sequence.***args**: Default arguments/parameters set by NumPy. You can learn about them in NumPy Array Function Documentation.

## Create a NumPy Array from a Python List

You can pass a Python List as the `object`

parameter of the NumPy’s `array()`

method to create a NumPy array.

Consider the example given below to understand how to create a NumPy Array from a Python List:

# Importing the NumPy library as np import numpy as np # Creating a NumPy array from a Python List num_array = np.array([1, 2, 3, 4]) # Printing the content of the variable print(num_array)

[1 2 3 4]

You can also specify the data type of the array to be created as follows:

# Importing the NumPy library as np import numpy as np # Creating a NumPy array from a Python List with dtype as float num_array = np.array([1, 2, 3, 4], dtype='float') # Printing the content of the variable print(num_array)

[1. 2. 3. 4.]

## Create a NumPy Array from a Python Tuple

You can pass a Python Tuple as the `object`

parameter of the NumPy’s `array()`

method to create a NumPy array.

Consider the example given below to understand how to create a NumPy Array from a Python Tuple:

# Importing the NumPy library as np import numpy as np # Creating a NumPy array from a Python Tuple num_array = np.array((1, 2, 3, 4)) # Printing the content of the variable print(num_array)

[1 2 3 4]

You can also specify the data type of the array to be created as follows:

# Importing the NumPy library as np import numpy as np # Creating a NumPy array from a Python Tuple with dtype as float num_array = np.array((1, 2, 3, 4), dtype='float') # Printing the content of the variable print(num_array)

[1. 2. 3. 4.]

## Create NumPy Arrays from Built-in NumPy Functions

NumPy provides a wide variety of built-in functions/methods for creating NumPy arrays from scratch. You will learn how to create NumPy arrays using these functions in the section below.

## 1. NumPy zeros function – numpy.zeros()

The NumPy zeros function, `zeros()`

, creates a NumPy array with all elements as zero based on a given shape and data type. The syntax for the function is as follows:

`numpy.zeros(shape, dtype, *args)`

The basic parameters that need to be passed into this function are:

**shape**: Shape of the desired array (integer or tuple of integers).**dtype:**Datatype of the desired array (default is`float64`

).***args**: Default arguments/parameters set by NumPy. You can learn about them in NumPy zeros function documentation.

Consider the example given below to understand how the NumPy zeros function works:

# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the zeros() function arr1 = np.zeros(shape=(1, 5), dtype=np.int16) arr2 = np.zeros(shape=(5, 5)) # Default dtype is np.float64 # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)

arr1: [[0 0 0 0 0]] arr2: [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]]

## 2. NumPy ones function – numpy.ones()

The NumPy ones function, `ones()`

, creates a NumPy array with all elements as one based on a given shape and data type. The syntax for the function is as follows:

`numpy.ones(shape, dtype, *args)`

The basic parameters that need to be passed into this function are:

**shape**: Shape of the desired array (integer or tuple of integers).**dtype:**Datatype of the desired array (default is`float64`

).***args**: Default arguments/parameters set by NumPy. You can learn about them in NumPy ones function documentation.

Consider the example given below to understand how the NumPy ones function works:

# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the ones() function arr1 = np.ones(shape=(1, 5), dtype=np.int16) arr2 = np.ones(shape=(5, 5)) # Default dtype is np.float64 # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)

arr1: [[1 1 1 1 1]] arr2: [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]]

## 3. NumPy arange function – numpy.arange()

The NumPy arange function, `arange()`

, creates a one-dimensional NumPy array with evenly spaced values within a given interval. The syntax for the function is as follows:

```
numpy.arange([start, ]stop, [step, ]dtype=None, *args)
```

The basic parameters that need to be passed into this function are:

**start**: The starting value of the interval. It should be an integer and the default value is 0. This value is included in the array created.**stop:**The ending value of the interval. This value is not included in the created array.**step:**The distance between two adjacent values in the array to be created. The default step size is 1.**dtype:**Data type of the desired array.***args:**Default arguments/parameters set by NumPy. You can learn about them in NumPy arange function documentation.

The NumPy arange function is quite similar to the Python range function. Consider the example given below to understand how the NumPy arange function works:

# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the arange() function arr1 = np.arange(start=1, stop=5, step=1, dtype=np.int16) arr2 = np.arange(start=10, stop=20, step=2, dtype=np.float32) # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)

arr1: [1 2 3 4] arr2: [10. 12. 14. 16. 18.]

## 4. NumPy linspace function – numpy.linspace()

The NumPy linspace function, `linspace()`

, creates a one-dimensional NumPy array with evenly spaced values within a given interval. The syntax for the function is as follows:

```
numpy.arange(start, stop, num, *args)
```

The basic parameters that need to be passed into this function are:

The starting value of the interval. This value is included in the array created.**start**:**stop:**The ending value of the interval. This value is included by default in the created array.**num:**The number of evenly spaced samples to be generated within the given interval. The default value is 50.***args**: Default arguments/parameters set by NumPy. You can learn about them in NumPy linspace function documentation.

Consider the example given below to understand how the NumPy linspace function works:

# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the linspace() function arr1 = np.linspace(start=1, stop=3, num=5) arr2 = np.linspace(start=10, stop=20, num=2) # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)

arr1: [1. 1.5 2. 2.5 3. ] arr2: [10. 20.]

Note: The data type of a NumPy array created using the linspace function is always a float unless it is specified explicitly.

## 5. NumPy rand function – numpy.random.rand()

The NumPy rand function, `rand()`

, creates a NumPy array of the given shape and populates it with random samples from a uniform distribution over [0, 1) i.e. including 0 and excluding 1. The syntax for the function is as follows:

`numpy.random.rand(d0, d1, d2, ..., dn)`

The basic parameters that need to be passed into this function are:

The dimensions of the array to be created (must be non-negative). Only the shape of the desired array needs to be passed on to this function.**d0, d1, d2, …, dn**:

Consider the example given below to understand how the NumPy rand function works:

# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the rand() function arr1 = np.random.rand(3,) arr2 = np.random.rand(2, 2) # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)

arr1: [0.52768115 0.73880502 0.12197086] arr2: [[0.7268723 0.77066793] [0.39853139 0.28570399]]

Note that the values generated by the rand function can change each time the array is created since the values are randomly generated.

With this lesson, you have learned to create NumPy arrays from Python Lists/Python Tuples as well as from NumPy’s built-in functions.