How to Create a Numpy Array: 10 Methods

Numpy is a data structure that allows you to create an array using the Numpy library. The array creation allows you to do complex mathematical calculations very easily using the package. In this guide, you will learn how to create a numpy array using different methods or examples.

To create a NumPy array in Python, you first need to import the NumPy library if you haven’t already. But before that, you have to install the package. You can install NumPy using a package manager like pip if it’s not already installed.

pip install numpy # For python version 2.xx+
pip3 install numpy # For python 3.xx

Methods to Create a Numpy Array

Once NumPy is installed, you can create NumPy arrays using various methods. Here are some common ways to create NumPy arrays:

Method 1: Create a numpy array from a list or tuple

You can create a NumPy array from a Python list or tuple using the numpy.array() function. You will pass the list or tuple as an argument to the np.array() function.

import numpy as np
# Creating a NumPy array from a Python list
my_list = [10, 20, 30, 40, 50]
arr = np.array(my_list)
print(arr)

Output

[10 20 30 40 50]

Method 2: Using numpy.zeros() and numpy.ones ()

You can create arrays filled with zeros or ones using the numpy.zeros() and numpy.ones() functionsrespectively.

import numpy as np

# Creating a 1D array of zeros with 10 elements
zeros_array = np.zeros(10)
print(zeros_array)

# Creating a 2D array (matrix) of ones with 4 rows and 5 columns
ones_matrix = np.ones((4, 5))
print(ones_matrix)

Output

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]

Method 3: Create a numpy array Using numpy.arange()

The numpy.arange() function creates an array with a range of values similar to Python’s range().

import numpy as np
# Creating an array with values from 0 to 9
arr = np.arange(10)
print(arr)

Output

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

Method 4: Using numpy.linspace()

The numpy.linspace() creates an array of evenly spaced values over a defined range.

import numpy as np

# Creating an array of 5 equally spaced values between 0 and 1
arr = np.linspace(0, 1, 5)
print(arr)

Output

[0.   0.25 0.5  0.75 1.  ]

Method 5: Using Random Values with numpy.random ()

NumPy provides various functions in the numpy.random module to create arrays with random values. Most of the developers use it for testing purposes only.

import numpy as np

# Creating a 1D array with random values between 0 and 1
random_array = np.random.rand(5)
print(random_array)

# Creating a 2D array (matrix) with random integers between 0 and 9
random_matrix = np.random.randint(0, 10, size=(3, 3))
print(random_matrix)

Output

[0.33403416 0.45056918 0.62109675 0.40805637 0.74391679]
[[3 4 1]
 [9 2 7]
 [8 0 9]]

Method 6: Using numpy.eye() and numpy.identity()

You can also create a numpy array using the numpy.eye() or numpy.idenity() function. These functions create identity matrices.

import numpy as np

# Creating a 5x5 identity matrix
identity_matrix = np.eye(5)
print(identity_matrix)

Output

[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]

Method 7: Using numpy.full() and numpy.empty()

You can create arrays filled with a specific value using numpy.full(). You can also create an uninitialized array using numpy.empty().

import numpy as np

# Creating a 2x3 array filled with a specific value (e.g., 7)
filled_array = np.full((2, 3), 7)
print(filled_array)

# Creating an uninitialized 1D array of length 4
empty_array = np.empty(4)
print(empty_array)

Output

[[7 7 7]
 [7 7 7]]
[4.67273546e-310 0.00000000e+000 2.21149159e-316 1.76125651e-312]

Method 8: Using numpy.copy()

You can create a new NumPy array that is a copy of an existing array using the numpy.copy() function.

import numpy as np

# Creating a copy of an existing array
original_array = np.array([10, 20, 30])
copied_array = np.copy(original_array)
print(copied_array)

Output

[10 20 30]

Method 9: Using numpy.tile()

You can create an array by repeating a given array a certain number of times using numpy.tile().

import numpy as np

# Creating a 1D array by repeating [1, 2, 3] three times
tiled_array = np.tile([1, 2, 3], 3)
print(tiled_array)

Output

[1 2 3 1 2 3 1 2 3]

Method 10: Using numpy.meshgrid()

The numpy.meshgrid() is used to create coordinate matrices for 2D grids, which can be useful in various mathematical and plotting operations.

import numpy as np

# Creating coordinate matrices for a 2D grid
x = np.array([10, 20, 30])
y = np.array([40, 50, 60])
X, Y = np.meshgrid(x, y)
print(X)
print(Y)

Output

[[10 20 30]
 [10 20 30]
 [10 20 30]]
[[40 40 40]
 [50 50 50]
 [60 60 60]]

Conclusion

Numpy is a very useful python package for doing complex mathematical calculations. There are various methods to create a numpy array. The above are some of them .Use any method according to your needs.

Source:

Numpy Offical Webpage

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link