Different Ways to Create Numpy Arrays in Python
Last Updated :
03 Apr, 2024
Creating NumPy arrays is a fundamental aspect of working with numerical data in Python. NumPy provides various methods to create arrays efficiently, catering to different needs and scenarios. In this article, we will see how we can create NumPy arrays using different ways and methods.
Ways to Create Numpy Arrays
Below are some of the ways by which we can create NumPy Arrays in Python:
Create Numpy Arrays Using Lists or Tuples
The simplest way to create a NumPy array is by passing a Python list or tuple to the numpy.array() function. This method creates a one-dimensional array.
Python3
import numpy as np
my_list = [1, 2, 3, 4, 5]
numpy_array = np.array(my_list)
print("Simple NumPy Array:",numpy_array)
Initialize a Python NumPy Array Using Special Functions
NumPy provides several built-in functions to generate arrays with specific properties.
- np.zeros(): Creates an array filled with zeros.
- np.ones(): Creates an array filled with ones.
- np.full(): Creates an array filled with a specified value.
- np.arange(): Creates an array with values that are evenly spaced within a given range.
- np.linspace(): Creates an array with values that are evenly spaced over a specified interval.
Python3
import numpy as np
zeros_array = np.zeros((2, 3))
ones_array = np.ones((3, 3))
constant_array = np.full((2, 2), 7)
range_array = np.arange(0, 10, 2) # start, stop, step
linspace_array = np.linspace(0, 1, 5) # start, stop, num
print("Zero Array:","\n",zeros_array)
print("Ones Array:","\n",ones_array)
print("Constant Array:","\n",constant_array)
print("Range Array:","\n",range_array)
print("Linspace Array:","\n",linspace_array)
OutputZero Array
[[0. 0. 0.]
[0. 0. 0.]]
Zero Array
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
Constant Array
[[7 7]
[7 7]]
Range Array
[0 2 4 6 8]
Linspace Array
[0. 0.25 0.5 0.75 1. ]
Create Python Numpy Arrays Using Random Number Generation
NumPy provides functions to create arrays filled with random numbers.
- np.random.rand(): Creates an array of specified shape and fills it with random values sampled from a uniform distribution over [0, 1).
- np.random.randn(): Creates an array of specified shape and fills it with random values sampled from a standard normal distribution.
- np.random.randint(): Creates an array of specified shape and fills it with random integers within a given range.
Python3
import numpy as np
random_array = np.random.rand(2, 3)
normal_array = np.random.randn(2, 2)
randint_array = np.random.randint(1, 10, size=(2, 3))
print(random_array)
print(normal_array)
print(randint_array)
Output[[0.87948864 0.55022063 0.29237533]
[0.99475413 0.76666244 0.55240304]]
[[ 1.77971899 0.67837749]
[ 0.33101208 -1.04029635]]
[[6 6 3]
[8 5 8]]
Create Python Numpy Arrays Using Matrix Creation Routines
NumPy provides functions to create specific types of matrices.
- np.eye(): Creates an identity matrix of specified size.
- np.diag(): Constructs a diagonal array.
- np.zeros_like(): Creates an array of zeros with the same shape and type as a given array.
- np.ones_like(): Creates an array of ones with the same shape and type as a given array.
Python3
import numpy as np
identity_matrix = np.eye(3)
diagonal_array = np.diag([1, 2, 3])
zeros_like_array = np.zeros_like(diagonal_array)
ones_like_array = np.ones_like(diagonal_array)
print(identity_matrix)
print(diagonal_array)
print(zeros_like_array)
print(ones_like_array)
Output[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
[[1 0 0]
[0 2 0]
[0 0 3]]
[[0 0 0]
[0 0 0]
[0 0 0]]
[[1 1 1]
[1 1 1]
[1 1 1]]
Similar Reads
Store Different Datatypes In One Numpy Array Storing diverse data types in a single NumPy array presents an effective approach to handling varied datasets efficiently. Although NumPy arrays are commonly homogeneous, situations may arise where managing multiple data types within a single array becomes necessary. In this article, we will underst
3 min read
Ways to Convert a Python Dictionary to a NumPy Array The task of converting a dictionary to a NumPy array involves transforming the dictionaryâs key-value pairs into a format suitable for NumPy. In Python, there are different ways to achieve this conversion, depending on the structure and organization of the resulting array.For example, consider a dic
3 min read
Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
How to Create Array of zeros using Numpy in Python numpy.zeros() function is the primary method for creating an array of zeros in NumPy. It requires the shape of the array as an argument, which can be a single integer for a one-dimensional array or a tuple for multi-dimensional arrays. This method is significant because it provides a fast and memory
4 min read
Python | Ways to add row/columns in numpy array Adding rows or columns to a NumPy array means appending new data along a specific axis. For example, if you have a 2D array like [[1, 2], [3, 4]] and you add a new row [5, 6], the array becomes [[1, 2], [3, 4], [5, 6]]. Similarly, adding a column [7, 8, 9] to a 3x2 array transforms it into a 3x3 arr
5 min read