NumPy (short for numerical Python) is a useful library for mathematics and data science, specifically for working with arrays of data.

In this post I want to detail, with examples, some common ways to create NumPy arrays. These range from creating arrays with Python lists to using built-in NumPy commands for standard arrays (like ones and zeros).

This post does not cover matrix operations like multiplication, splicing, etc. Those are reserved for other posts. I want to solely focus on methods for creating NumPy arrays.

Since I want this post to be comprehensive, if you have a specific way of creating NumPy arrays feel free to reach out using this contact form. You will receive credit and a backlink for contributing.

To begin, install NumPy using ‘pip install numpy’.

pip install numpy

Then, in your Notebook or Python file, import NumPy as the variable np.

import numpy as np

If you’re using Conda, use this installation guide from the NumPy documentation.

Creating Arrays in NumPy

There are so many ways to create arrays in NumPy, your eyes will cross. Let’s go over them one at a time. This won’t cover all the ways, but these are at least the most useful.

Creating 1-D Arrays

To create a 1-dimensional (1-D) NumPy array, use the np.array() function.

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

# Output:
[1 2 3]

Note that the argument passed into np.array is a simple Python list. We could just as easily write the following:

a_list = [i for i in range(1,4)]
a = np.array(a_list)

# Output:
[1 2 3]

Of course, NumPy has an easier way to do the same thing. Using the np.arange() function you can create an array of numbers from a start value up to, but not including, an end value.

a = np.arange(start=1, stop=4, step=1)
# Or, more succinctly:
a = np.arange(1, 4)

# Output:
[1 2 3]

If you have a range of values in mind, but need an array that cuts the range in an even n number of times, then you can use the np.linspace() function.

a = np.linspace(start=0, stop=50, num=6)
# Or, more succintly:
a = np.linspace(0, 50, 6)

# Output:
[0. 10. 20. 30. 40. 50.]

Note how the output is expressed as a floating point number. You can have NumPy output the datatype you prefer using the dtype parameter.

a = np.linspace(0, 50, 6, dtype=int) # prints a list of ints
a = np.linspace(0, 50, 6, dtype=str) # prints a list of strings

Prepackaged NumPy Arrays

There are some NumPy functions that create arrays with specific characteristics. For example:

  • np.ones()
  • np.zeros()
  • np.random.rand()

The first two are pretty straightforward. Pass in an integer value and you’ll receive a 1-dimensional array of ones or zeros.

print(np.ones(5))
# Output: [1. 1. 1. 1. 1.]

print(np.zeros(5))
# Output: [0. 0. 0. 0. 0.]

If you’d like to fill a 1-dimenstional array of size n with n randomly generated numbers between 0 and 1, then use the np.random.rand() function. This function produces uniformly distributed random samples from 0 to n (not including n).

print(np.random.rand(5))
# Example Output: [0.59836903 0.2973207  0.26984867 0.30717907 0.63486949]

If you’d like to produce random samples between 0 and a different number (say, 100) then you can perform an element-wise multiplication on the resulting array.

In NumPy, an element-wise multiplication is done by simply multiplying the array by a number.

a = np.random.rand(5)
a = 100 * a
print(a)
# Example Output: [55.28802359 44.4521426  48.20100507 29.64656067 26.23718413]

Creating 2-D Arrays

There are a few ways to define a 2-dimensional array in NumPy using Python. The first is by explicitly defining the 2D array as a list of lists and then passing that into the np.array() function.

a_list = [[1,2,3],[4,5,6]]
a = np.array(a_list)

print(a)
# Output:
# [[1 2 3]
#  [4 5 6]]

You can also “reshape” a 1D array into a 2D array using the np.reshape() function. The first argument is the 1D array you want to convert. The second argument is a tuple representing the number of rows and columns in the 2D array, respectively.

a_1D = np.array([1, 2, 3, 4, 5, 6])
a_2D = np.reshape(a_list, (3, 2))
print(a_2D)

# Output:
#  [[1 2]
#   [3 4]
#   [5 6]]

Article Corrections and Contributions

Did you know you can contribute to Quantastic Research articles? For contributions, fill out the Article Contribution Form. For corrections, see the Article Correction Form. Use the standard Contact Form for all other inquiries.

Author

quantasticresearch.blog@gmail.com

Hi, I'm Dom and I'm a graduate of Electrical Engineering & Computer Science, as well as a long-time user of the Python programming language. With Quantastic Research, I'm aiming to evolve my understanding of data science and machine learning techniques by sharing my experience through blog articles. Anything you find on this website is purely informational and should not be construed as financial or professional advice.

Probabilities of Up and Down Days feature image

Probabilities of Up and Down Days in the S&P500

[Last Updated: 11/24/2024] In this post we’ll be calculating the probabilities and statistics of up days and down days. First, I’ll use...

Read out all
In

Automated Stock Alerts Using the Notion API and Python

I recently wrote an article on using Windows Task Manager to periodically run Python scripts. I currently have a couple scripts automated...

Read out all
In

Automating Python Scripts using Windows Task Scheduler

If you landed here, you’re probably interested in having a script run automatically at specified times on your PC. Specifically, a Python...

Read out all
In

Working with TOML Files in Python

TOML files (Tom’s Obvious Minimal Language) are the brain-child of Mr. Tom Preston-Werner. They were developed as an alternative configuration file format...

Read out all