ADVERTISEMENT
ADVERTISEMENT

NumPy - Creating Arrays

In this tutorial learn how to create different types of arrays using Numpy, the creation of an array refers to the process of generating a new array object with specified dimensions, data type, and values. It involves allocating memory and organizing the elements in a structured manner to form a multidimensional container that can store homogeneous data.

Creating an array in NumPy involves specifying the size or shape of the array, the data type of its elements, and optionally providing the values for initialization. The array can be one-dimensional (1D), two-dimensional (2D), or multi-dimensional.

The creation of an array in NumPy provides a foundation for performing various mathematical and statistical operations efficiently. It allows for vectorized computations and facilitates operations on arrays as a whole, rather than on individual elements.

NumPy provides several functions and methods to create arrays, such as np.array(), np.zeros(), np.ones(), np.empty(), np.full(), np.eye(), np.arange(), np.linspace(), and more. These functions enable users to create arrays with different shapes, initialize them with specific values, and work with various numerical data types.

Different Ways to Create Arrays in NumPy

  • 1D Array Creation
  • 2D Array Creation
  • 3D Array Creation
  • Array Creation from a List
  • Array Creation from a Tuple
  • Array Creation using numpy.zeros()
  • Array Creation using numpy.ones()
  • Array Creation using numpy.empty()
  • Array Creation using numpy.arange()
  • Array Creation using numpy.linspace()
  • Array Creation using numpy.eye() or numpy.identity()
  • Array Creation using numpy.random.random()

Check different ways to create arrays in NumPy, along with their definitions and examples:

1D Array Creation

Definition: A 1D array, also known as a one-dimensional array or a vector, is a collection of elements arranged in a single row. In the given example, we create a 1D array using the np.array() function and pass a list of values [1, 2, 3, 4, 5] as an argument. The resulting array arr_1d is displayed as [1 2 3 4 5], where the elements are separated by spaces.

Example

import numpy as np

arr_1d = np.array([1, 2, 3, 4, 5])
print(arr_1d)


#output
# [1,2,3,4,5]

2D Array Creation

Definition: A 2D array, also known as a two-dimensional array or a matrix, is a collection of elements arranged in rows and columns. In the provided example, we create a 2D array using the np.array() function and pass a list of lists [[1, 2, 3], [4, 5, 6]] as an argument. 

Example:

import numpy as np

arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d)

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

3D Array Creation

Definition: A 3D array, also known as a three-dimensional array, is a collection of elements arranged in multiple layers, rows, and columns. In the provided example, we create a 3D array using the np.array() function and pass a nested list [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]] as an argument. The resulting array arr_3d is displayed as:

Example:

import numpy as np

arr_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr_3d)

#output
# [[[ 1  2  3]
#   [ 4  5  6]]
#
#  [[ 7  8  9]
#   [10 11 12]]]

Array Creation from a List:

Definition: Create an array by passing a Python list or list-like object to the numpy.array() function.

Example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)

#Output: [1 2 3 4 5]

Array Creation from a Tuple:

Definition: Create an array by passing a Python tuple or tuple-like object to the numpy.array() function.

Example:

import numpy as np

arr = np.array((1, 2, 3, 4, 5))
print(arr)

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

Array Creation using numpy.zeros():

Definition: Create an array filled with zeros of a specified shape and data type.

Example:

import numpy as np

arr = np.zeros((3, 4))  # Creates a 3x4 array filled with zeros
print(arr)

#output
#[[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]

Array Creation using numpy.ones():

Definition: Create an array filled with ones of a specified shape and data type.

Example:

import numpy as np

arr = np.ones((2, 2),dtype=np.int32)  # Creates a 2x2 array filled with ones
print(arr)

#output
# [[1 1]
#  [1 1]]

Array Creation using numpy.empty():

Definition: Create an array without initializing its elements. The content is unpredictable and depends on the state of the memory.

Example:

import numpy as np

arr = np.empty((2, 3),dtype=np.int32)  # Creates a 2x3 array with uninitialized elements
print(arr)

#output
#[[         0          0          0]
# [1071644672          0 1072693248]]

Array Creation using numpy.arange():

Definition: Create an array with evenly spaced values within a specified range.

Example:

import numpy as np

arr = np.arange(1, 10, 2)  # Creates an array with values [1, 3, 5, 7, 9]
print(arr)

#output
# [1 3 5 7 9]

Array Creation using numpy.linspace():

Definition: Create an array with evenly spaced values over a specified range.

Parameters:

  • start: The starting value of the sequence.
  • stop: The end value of the sequence.
  • num: (Optional)  The number is of evenly spaced values to generate. Default is 50.

Example:

mport numpy as np

arr = np.linspace(1, 10, 6)  # Creates an array with values [1.0, 2.8, 4.6, 6.4, 8.2, 10.0]
print(arr)

# Output: 6 Values Generated  System decide the number for step
# [ 1.   2.8  4.6  6.4  8.2 10. ]

Array Creation using numpy.eye() or numpy.identity():

Definition: Create an identity matrix or an array with ones on the diagonal and zeros elsewhere.

Example:

import numpy as np

arr = np.eye(3,dtype=int)  # Creates a 3x4 identity matrix
print(arr)

# [[1 0 0]
#  [0 1 0]
 # [0 0 1]]

Array Creation using numpy.random.random():

Definition: The np.random.random function is a part of the NumPy library in Python and is used to generate random numbers from a uniform distribution over the interval [0, 1). It returns an array of random numbers with the specified shape.

Example:

import numpy as np

arr = np.random.random((2, 2))  # Creates a 2x2 array with random values between 0 and 1
print(arr)

#output
#[[0.70932303 0.39873474]
# [0.42961632 0.22997078]]

 


ADVERTISEMENT

ADVERTISEMENT