How To Build A Numpy Array

Before we start: This Python tutorial is a part of our series of Python Package tutorials. You can find other Numpy related topics too!

There are many ways of creating a Numpy array. Let’s start with the simplest one: an array of zero dimensions (0d), which contains a single element. 

arr = np.array(4)

Here we use the np.array function to initialize our array with a single argument (4). The result is an array that contains just one number: 4. That’s simple enough, but not very useful. 

We can create a regular 1-dimensional (1d) array by giving the np.array function a list as an argument. 

arr = np.array([1,2,3,4])

Or

arr = np.array((1, 2, 3, 4))

Both of which give us the following array:

[1 2 3 4]

Note that the second example uses a tuple as an argument, which also works. 

We can create a numpy array of any dimension we want simply by giving it an argument of that dimension (2D, 3D, etc). For example, to create a 2-dimensional array (2D) with:

arr = np.array([[1,2],[3,4]])

Or

arr = np.array(((1,2),(3,4)))

Both of which give us the following 2D array:

[[1 2]

[3 4]]

 

Another functionality of np.array function allows us to create any kind of numpy array with any dimension without specifically providing that dimensioned array as an argument. For example, we can create a 5-dimensional Numpy Array from just a regular 1d array.

arr = np.array([1, 2, 3, 4], ndmin=2)
print(arr.ndim) 
print(arr.shape)

Here the print statement will print 2 as the dimension, and our array will be a 2-dimensional array with only the given list. The print statement printing the shape will print (1,4) as the rest of the array is empty.

The output looks like this:

2

(1, 4)

There are some intrinsic functions numpy provides for easy array creation, as well. Let’s take a look at the “zeros” and “ones” functions:

arr = np.zeros((2,2))
arr = np.ones((2,2))

The above code snippet will create two different arrays:

  • The first array will contain only zeros in a 2×2 array
    [[0 0]

 [0 0]]

  • The second array will contain only ones in a 2×2 array
    [[1 1]

[1 1]]

As you can see from the code the argument given as a tuple will define the size. 

For use in linear algebra, numpy provides the following function:

arr = np.eye(3)

The above code snippet will create the following array:

[[1 0 0]

 [0 1 0]

 [0 0 1]]

Finally, let’s look at the arange() function:

arr = np.arange(10)

The above code snippet will print generate a 1D array of elements from 0 to 9: 

[0 1 2 3 4 5 6 7 8 9]

Compiling from source can get quite complex, what with environment setup, scripts and patches, not to mention resolving any dependency conflicts or errors that may occur. Instead, consider using the ActivateState Platform to automatically build and package it for you.

The following tutorials will provide you with step-by-step instructions on how to work with Numpy, including:

Get a version of Python that’s pre-compiled for Data Science

While the open source distribution of Python may be satisfactory for an individual, it doesn’t always meet the support, security, or platform requirements of large organizations.

This is why organizations choose ActivePython for their data science, big data processing and statistical analysis needs.

Pre-bundled with the most important packages Data Scientists need, ActivePython is pre-compiled so you and your team don’t have to waste time configuring the open source distribution. You can focus on what’s important–spending more time building algorithms and predictive models against your big data sources, and less time on system configuration.

Some Popular Python Packages for Data Science/Big Data/Machine Learning You Get Pre-compiled – with ActivePython

  • pandas (data analysis)
  • NumPy (multi-dimensional Numpy array)
  • SciPy (algorithms to use with numpy)
  • HDF5 (store & manipulate data)
  • Matplotlib (data visualization)
  • Jupyter (research collaboration)
  • PyTables (managing HDF5 datasets)
  • HDFS (C/C++ wrapper for Hadoop)
  • pymongo (MongoDB driver)
  • SQLAlchemy (Python SQL Toolkit)

python for datascience

Use ActivePython and accelerate your Python projects.

  • The #1 Python solution used by innovative enterprise teams
  • Comes pre-bundled with top Python packages
  • Spend less time resolving dependencies and more time on quality coding

Take a look at ActivePython

Remi M