Simple Ways to Create Array with NumPy

NumPy, the powerful numerical Python library, is essential for data manipulation, scientific computing, and data analysis. NumPy arrays provide a high-performance, memory-efficient way to store and process large datasets, making them a fundamental tool in the toolkit of data scientists, engineers, and researchers. They offer a unified data structure for mathematical operations, making it easier to work with multidimensional data, perform complex calculations, and implement algorithms efficiently. Whether you’re working on machine learning, scientific simulations, or data preprocessing, understanding how to create and use NumPy arrays is a crucial step in harnessing the full potential of Python for data-centric tasks. In this article, we’ll explore different ways to create array with NumPy easily with some examples.

ways to create array
Photo by Vlado Paunovic on Pexels.com

Table of Contents

Convert existing data into NumPy Array

Let’s get started with the simplest and most used method, which is converting existing data into one. If we have a Python list, we can convert it to NumPy by simply using the ‘np.array()’ function.

# covnert Python list to NumPy array
import numpy as np

python_list = [1, 2, 3, 4, 5]
numpy_array = np.array([python_list])

In the example above, I’ve only shown how to convert a 1D python list to NumPy, but you could use the same method for any dimensional data.

What if we don’t have a Python List or we want to create a large matrix from scratch? NumPy also provides some built-in functions for array generation. These functions include

1D array creation functions
  • np.arrange()
  • np.linspace()
# create an array with evenly spaced values within a given interval
>>> np.arange(0, 5, 0.5)
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])


# create an array with evenly spaced numbers over a specified interval
>>> np.linspace(0, 5, 5)
array([0.  , 1.25, 2.5 , 3.75, 5.  ])
2D array creation functions
  • np.eye()
  • np.diag()
# create an identity matrix 
>>> np.eye(3)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])


# create 2D array with given diagonal elements
>>> np.diag([1, 2, 3])
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])
General ndarray creation functions
  • np.zeros()
  • np.ones()
  • np.random()
  • np.indices()

These functions accept a desired array shape and data type as parameters. For example, np.zeros(2) returns a 1D array with 2 zeros. The default data type is float64, but you could also change it by changing the dtype argument. We could also replace it with (a, b) which returns an a-by-b array. You also try np.zeros((1, 2, 3)) and see what it returns (supposed to be a 3D array).

# create an array of given shape and type, filled with zeros
>>> np.zeros((2, 2))
array([[0., 0.],
       [0., 0.]])

# create an array of given shape and type, filled with ones
>>> np.ones((2, 3))
array([[1., 1.],
       [1., 1.],
       [1., 1.]])

# create an array of given shape and type, without initializing entries
>>> np.empty([2, 2])
array([[ -9.74499359e+001,   6.69583040e-309],
       [  2.13182611e-314,   3.06959433e-309]])  

The arrays created above are all 1D, but you could also create different dimensional data by providing the data shape. For example, creating a 2D zero array with “np.zeros((2, 2))”.

Other Posts to Read