[New Book] Click to get The Beginner's Guide to Data Science!
Use the offer code 20offearlybird to get 20% off. Hurry, sale ends soon!

A Gentle Introduction to NumPy Arrays in Python

Arrays are the main data structure used in machine learning.

In Python, arrays from the NumPy library, called N-dimensional arrays or the ndarray, are used as the primary data structure for representing data.

In this tutorial, you will discover the N-dimensional array in NumPy for representing numerical and manipulating data in Python.

After completing this tutorial, you will know:

  • What the ndarray is and how to create and inspect an array in Python.
  • Key functions for creating new empty arrays and arrays with default values.
  • How to combine existing arrays to create new arrays.

Kick-start your project with my new book Linear Algebra for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

A Gentle Introduction to N-Dimensional Arrays in Python with NumPy

A Gentle Introduction to N-Dimensional Arrays in Python with NumPy
Photo by patrickkavanagh, some rights reserved.

Tutorial Overview

This tutorial is divided into 3 parts; they are:

  1. NumPy N-dimensional Array
  2. Functions to Create Arrays
  3. Combining Arrays

Need help with Linear Algebra for Machine Learning?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

NumPy N-dimensional Array

NumPy is a Python library that can be used for scientific and numerical applications and is the tool to use for linear algebra operations.

The main data structure in NumPy is the ndarray, which is a shorthand name for N-dimensional array. When working with NumPy, data in an ndarray is simply referred to as an array.

It is a fixed-sized array in memory that contains data of the same type, such as integers or floating point values.

The data type supported by an array can be accessed via the “dtype” attribute on the array. The dimensions of an array can be accessed via the “shape” attribute that returns a tuple describing the length of each dimension. There are a host of other attributes. Learn more here:

A simple way to create an array from data or simple Python data structures like a list is to use the array() function.

The example below creates a Python list of 3 floating point values, then creates an ndarray from the list and access the arrays’ shape and data type.

Running the example prints the contents of the ndarray, the shape, which is a one-dimensional array with 3 elements, and the data type, which is a 64-bit floating point.

Functions to Create Arrays

There are more convenient functions for creating fixed-sized arrays that you may encounter or be required to use.

Let’s look at just a few. You can see the full list here:

Empty

The empty() function will create a new array of the specified shape.

The argument to the function is an array or tuple that specifies the length of each dimension of the array to create. The values or content of the created array will be random and will need to be assigned before use.

The example below creates an empty 3×3 two-dimensional array.

Running the example prints the content of the empty array. Your specific array contents will vary.

Zeros

The zeros() function will create a new array of the specified size with the contents filled with zero values.

The argument to the function is an array or tuple that specifies the length of each dimension of the array to create.

The example below creates a 3×5 zero two-dimensional array.

Running the example prints the contents of the created zero array.

Ones

The ones() function will create a new array of the specified size with the contents filled with one values.

The argument to the function is an array or tuple that specifies the length of each dimension of the array to create.

The example below creates a 5-element one-dimensional array.

Running the example prints the contents of the created ones array.

Combining Arrays

NumPy provides many functions to create new arrays from existing arrays.

Let’s look at two of the most popular functions you may need or encounter.

Vertical Stack

Given two or more existing arrays, you can stack them vertically using the vstack() function.

For example, given two one-dimensional arrays, you can create a new two-dimensional array with two rows by vertically stacking them.

This is demonstrated in the example below.

Running the example first prints the two separately defined one-dimensional arrays. The arrays are vertically stacked resulting in a new 2×3 array, the contents and shape of which are printed.

Horizontal Stack

Given two or more existing arrays, you can stack them horizontally using the hstack() function.

For example, given two one-dimensional arrays, you can create a new one-dimensional array or one row with the columns of the first and second arrays concatenated.

This is demonstrated in the example below.

Running the example first prints the two separately defined one-dimensional arrays. The arrays are then horizontally stacked resulting in a new one-dimensional array with 6 elements, the contents and shape of which are printed.

Extensions

This section lists some ideas for extending the tutorial that you may wish to explore.

  • Experiment with the different ways of creating arrays to your own sizes or with new data.
  • Locate and develop an example for 3 additional NumPy functions for creating arrays.
  • Locate and develop an example for 3 additional NumPy functions for combining arrays.

If you explore any of these extensions, I’d love to know.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.

Books

References

API

Summary

In this tutorial, you discovered the N-dimensional array in NumPy for representing numerical and manipulating data in Python.

Specifically, you learned:

  • What the ndarray is and how to create and inspect an array in Python.
  • Key functions for creating new empty arrays and arrays with default values.
  • How to combine existing arrays to create new arrays.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Get a Handle on Linear Algebra for Machine Learning!

Linear Algebra for Machine Learning

Develop a working understand of linear algebra

...by writing lines of code in python

Discover how in my new Ebook:
Linear Algebra for Machine Learning

It provides self-study tutorials on topics like:
Vector Norms, Matrix Multiplication, Tensors, Eigendecomposition, SVD, PCA and much more...

Finally Understand the Mathematics of Data

Skip the Academics. Just Results.

See What's Inside

21 Responses to A Gentle Introduction to NumPy Arrays in Python

  1. Avatar
    Klaas Brau February 2, 2018 at 6:12 am #

    Thanks Jason. Tensormultiplication will be next? 😉
    I acutally always wondered why a one dimensional array is not (3,1) as shape instead of (3,)

    • Avatar
      Jason Brownlee February 2, 2018 at 8:24 am #

      Good question. It really has to do with broadcasting. I have a post on the topic soon.

  2. Avatar
    Vidyush Bakshi March 6, 2018 at 5:09 am #

    nice expalaination .. waiting for more!!!

  3. Avatar
    Kitan September 26, 2018 at 2:04 am #

    Hi Jason,
    Thanks for this post, really explanatory.
    I’m new to numpy and I am slightly confused about why a one dimensional array is (3,) and not (3,1). Could you point me in the right direction, say an article or something

    • Avatar
      Jason Brownlee September 26, 2018 at 6:17 am #

      Because 3,1 would be a two-dimensional array as it specifies two dimensions.

      • Avatar
        Jonathan MacPherson October 13, 2018 at 1:07 pm #

        Logic would suggest a one dimensional array would simply be (3), not (3,). So the lack of a second value for the np.shape would indicate the lack of a second dimension ? I wonder why np.shape would not return just (3). The comma seems to be my point of confusion.

        Thanks so much for your time !

        • Avatar
          Jason Brownlee October 14, 2018 at 6:01 am #

          Only 1D is specified.

          If if it was 2D it would be (3,1)

          The comma for 1D might be an artefact of printing the tuple or something.

  4. Avatar
    razan February 6, 2019 at 1:41 am #

    how to concatenate two ndarrays of shapes (5,7.10) and (5,7, 12) so I get one of shape (5, 7.22) ?

    • Avatar
      Jason Brownlee February 6, 2019 at 7:47 am #

      I’m not sure that makes sense.

    • Avatar
      Alan Jones March 12, 2019 at 7:54 am #

      Hi Razan,

      This does what you’re after.

      a = numpy.random.random(size=(5,7,12))
      b = numpy.random.random(size=(5,7,10))
      c = numpy.concatenate([a,b], axis=2)

      Cheers,

      Alan.

  5. Avatar
    Vas March 20, 2019 at 10:15 pm #

    How to insert a 1D array to a 3D array?

    • Avatar
      Jason Brownlee March 21, 2019 at 8:13 am #

      It really depends where in the 3d array.

      Often adding extra dimensions to the 1d array and using a dstack/hstack/vstack will get the result you need.

  6. Avatar
    Rishi May 24, 2019 at 10:24 am #

    It is an awesome tutorial to look over for python newbies working with arrays.

    Just wondering if you could give some insights into how to achieve the following task:

    Lets say:

    old_array = (size=(10,10))

    want to create a new array based on the “old_array” by using elements in the “old_array” and apply a formula (example – cos(old_array[1,1])).

    Thanks

    • Avatar
      Jason Brownlee May 24, 2019 at 2:28 pm #

      You can perform the subtraction between an array and single value directly. It will be performed element wise on the array.

      Is that what you mean?

  7. Avatar
    Jeff Lawrence July 30, 2020 at 12:39 am #

    Thanks for a very helpful introduction … now off to figure out how to populate from a chunk of a sound file.
    numerophobically yours
    Jeff

  8. Avatar
    steve October 22, 2021 at 12:08 pm #

    when you have a one dimensional numpy array (10, ). Does this mean 10 rows and no columns? How do we think of a one dimensional numpy array in terms of rows and columns?

    • Avatar
      Adrian Tam October 27, 2021 at 1:25 am #

      In numpy, (10,) means you have a one dimensional array. If you say rows and columns, you’re already assuming a two-dimensional array.

      But usually when you apply a one-dimensional array to a two-dimensional array in numpy, you can operate like a vector and matrix and the row/column orientation of the vector is sometimes magically corrected by numpy to make your operation successful.

Leave a Reply