NumPy arrays are the central data structure for applying the numerous useful operations defined in the NumPy library. Before you can start manipulating or transforming them, you’ll have to create them. In this tutorial, we’ll look at how some of the different ways to create numpy arrays.

There are a number of different ways to create a numpy array. You can use the numpy `array()`

function to create one from an array-like object. You can also other built-in numpy functions to create more specific arrays depending on your use-case.

## 1. The numpy `array()`

function

The numpy `array()`

function is generally used to create a numpy array from other array-like objects. For instance, you can use this function to create a numpy array from a python list. See the example below:

import numpy as np # create a 1D numpy array arr1 = np.array([1,2,3]) print("1D numpy array:\n", arr1) print("Type:", type(arr1)) # create a 2D numpy array arr2 = np.array([[1,2,3],[4,5,6]]) print("2D numpy array:\n", arr2) print("Type:", type(arr2))

Output:

1D numpy array: [1 2 3] Type: <class 'numpy.ndarray'> 2D numpy array: [[1 2 3] [4 5 6]] Type: <class 'numpy.ndarray'>

In the above example, numpy arrays `arr1`

and `arr2`

are created from lists using the numpy `array()`

function. Note that, to create a 2D array we had to pass a nested list to the `array()`

function.

You can also use other array-like objects, such as tuples, etc. to create a numpy array using the `array()`

function.

tup = ((1,2,3),(4,5,6)) print(np.array(tup))

Output:

[[1 2 3] [4 5 6]]

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

The numpy `array()`

function is the standard way of creating numpy arrays. Numpy, however, also provides a bunch of other built-in functions to create certain numpy arrays from scratch. These arrays can come in handy depending upon the use-case. For instance, you may require an array of zeroes, or ones, or random numbers. All of these can be created using numpy built-in functions. Follow along to see how to make such arrays in numpy.

## 2. Create a numpy array of zeros

The numpy `zeros()`

function is used to create an array of the specified shape filled with zeroes. See the example below.

import numpy as np arr = np.zeros((2,3)) print("numpy array:\n", arr) print("Type:", type(arr))

Output:

numpy array: [[0. 0. 0.] [0. 0. 0.]] Type: <class 'numpy.ndarray'>

Here, the default dtype of the array items is *numpy.float64*, you can specify the desired data-type for the array via the `dtype`

parameter.

## 3. Create a numpy array of ones

You can create a numpy array of your desired shape filled with ones using the numpy `ones()`

function. See the example below.

import numpy as np arr = np.ones((2,3)) print("numpy array:\n", arr) print("Type:", type(arr))

Output:

numpy array: [[1. 1. 1.] [1. 1. 1.]] Type: <class 'numpy.ndarray'>

Here as well the default dtype of the array items is *numpy.float64*, you can specify the desired data-type for the array via the `dtype`

parameter.

## 4. Create a numpy array with random integers

The numpy `random.randint()`

function is used to create a numpy array filled with random integers in a given range. The following is its syntax:

arr = numpy.random.randint(low, high, size)

It returns a numpy array of the shape passed to the `size`

parameter filled with integers from `low`

(*inclusive*) to `high`

(*exclusive*). See the example below.

import numpy as np arr = np.random.randint(1, 5, (2,3)) print("numpy array:\n", arr) print("Type:", type(arr))

Output:

numpy array: [[2 1 3] [4 2 4]] Type: <class 'numpy.ndarray'>

## 5. Other miscellaneous methods

There are other numpy methods like `arange()`

and `linspace()`

that can be used to create a numpy array with evenly spaced values between start and stop endpoints. The `arange()`

function gives values in the interval `[start, stop)`

and uses a step-size whereas the `linspace()`

function provides more control on whether or not to include `stop`

and uses number of samples instead of a step-size. See the example below.

import numpy as np # array of evenly spaced numbers from 1 to 10 with a step-size of 2 arr1 = np.arange(1, 10, 2) print("arr1: ", arr1) # array of evenly spaced numbers from 1 to 10 and return 2 numbers arr2 = np.linspace(1, 10, 2) print("arr2: ", arr2)

Output:

arr1: [1 3 5 7 9] arr2: [ 1. 10.]

You can see that `np.arange(1, 10, 2)`

returned evenly spaced values between 1 to 10 with a step size of 2 whereas `np.linspace(1, 10, 2)`

returned 2 evenly spaced values between 1 to 10.

These were some of the different ways to create a numpy array. For more, refer to this guide on array creation by numpy.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**

Tutorials on numpy arrays –

- How to sort a Numpy Array?
- Create Pandas DataFrame from a Numpy Array
- Different ways to Create NumPy Arrays
- Convert Numpy array to a List – With Examples
- Append Values to a Numpy Array
- Find Index of Element in Numpy Array
- Read CSV file as NumPy Array
- Filter a Numpy Array – With Examples
- Python – Randomly select value from a list
- Numpy – Sum of Values in Array
- Numpy – Elementwise sum of two arrays
- Numpy – Elementwise multiplication of two arrays
- Using the numpy linspace() method
- Using numpy vstack() to vertically stack arrays
- Numpy logspace() – Usage and Examples
- Using the numpy arange() method
- Using numpy hstack() to horizontally stack arrays
- Trim zeros from a numpy array in Python
- Get unique values and counts in a numpy array
- Horizontally split numpy array with hsplit()