**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 Numpy arrays that can contain any number of elements. Let’s start with the simplest one: an array of zero dimensions (0d), which contains a single element of integer data type (dtype).**

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 array object: 4. That’s simple enough, but not very useful.

We can create a regular one-dimensional array (1D) 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 new array:

[1 2 3 4]

**Note that the second example uses a tuple data structure as an argument, which is also a syntax that works.**

**We can create a numpy array with any number of dimensions (multidimensional arrays, denoted numpy.ndarray) we want simply by giving it an argument of that dimension (2D array, 3D array…nD array).**

For example, to create a two-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, effectively reshaping it.

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 array data. The print statement printing the shape will print (1,4) as the rest of the array is empty (ie., np.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” (bool) functions:

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

The above code snippet will create two different resulting 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 and number of array elements.

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

arr = np.eye(3)

The above code snippet will create the following returned 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 Python libraries 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 provides organizations with a false sense of security since 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 Python3 Packages for Data Science/Big Data/Machine Learning You Get Pre-compiled – with ActivePython

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