Open In App

Basics of NumPy Arrays

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

NumPy stands for Numerical Python and is used for handling large, multi-dimensional arrays and matrices. Unlike Python's built-in lists NumPy arrays provide efficient storage and faster processing for numerical and scientific computations. It offers functions for linear algebra and random number generation making it important for data science and machine learning.

Types of Array

Various types of arrays are as follows:

1. One Dimensional Array:

A one-dimensional array is a type of linear array.

One Dimensional Array

Example:

Python
import numpy as np

list = [1, 2, 3, 4]

sample_array = np.array(list)

print("List in python : ", list)

print("Numpy Array in python :",
      sample_array)

Output:

n1
One Dimensional Array:

Check data type for list and array:

Python
print(type(list_1))

print(type(sample_array))

Output:

n2
Check data type

2. Multi-Dimensional Array:

A Multi-Dimensional Array is an array that can store data in more than one dimension such as rows and columns. In simple terms it is a array of arrays.

For example a 2D array is like a table with rows and columns where each element is accessed by two indices: one for the row and one for the column. Higher dimensions like 3D arrays involve adding additional layers.

Two Dimensional Array

Example:

Python
import numpy as np

list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]

sample_array = np.array([list_1, 
                         list_2,
                         list_3])
print("Numpy multi dimensional array in python\n",
      sample_array)

Output:

n3
Multi-Dimensional Array

Note: use [ ] operators inside numpy.array() for multi-dimensional.

Parameters of a Numpy Array

1. Axis: Axis of an array describes the order of the indexing into the array.

Axis 0 = one dimensional
Axis 1 = Two dimensional
Axis 2 = Three dimensional 

2. Shape: Number of elements along with each axis and is returned as a tuple.

Example:

Python
import numpy as np
sample_array = np.array([[0, 4, 2],
                       [3, 4, 5],
                       [23, 4, 5],
                       [2, 34, 5],
                       [5, 6, 7]])

print("shape of the array :",
      sample_array.shape)

Output:

shape of the array : (5, 3)

3. Rank: Rank of an array is simply the number of axes or dimensions it has.

One-dimensional array has rank 1.

Rank 1

Two-dimensional array has rank 2.

Rank 2

4. Data type objects (dtype): Data type objects (dtype) is an example of numpy.dtype class. It describes how the bytes in the fixed-size block of memory corresponding to an array item should be interpreted.

Example:

Python
import numpy as np

sample_array_1 = np.array([[0, 4, 2]])

sample_array_2 = np.array([0.2, 0.4, 2.4])

print("Data type of the array 1 :",
      sample_array_1.dtype)

print("Data type of array 2 :",
      sample_array_2.dtype)

Output: 

n5
dtype

Different Ways of Creating Numpy Array

1. numpy.array(): Numpy array object in Numpy is called ndarray. We can create ndarray using this function.

Syntax: numpy.array(parameter)

Example: 

Python
import numpy as np

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

print("Array :",arr)

Output:

Array : [3 4 5 5]

2. numpy.fromiter(): The fromiter() function create a new one-dimensional array from an iterable object.

Syntax: numpy.fromiter(iterable, dtype, count=-1)

Example:

Python
import numpy as np

var = "Geekforgeeks"

arr = np.fromiter(var, dtype = 'U2')

print("fromiter() array :",
      arr)

Output:

fromiter() array : ['G' 'e' 'e' 'k' 'f' 'o' 'r' 'g' 'e' 'e' 'k' 's']
 

3. numpy.arange(): This is an inbuilt NumPy function that returns evenly spaced values within a given interval.

Syntax: numpy.arange( start , stop, step , dtype=None )

Example:

Python
import numpy as np

np.arange(1, 20 , 2, 
          dtype = np.float32)

Output:

array([ 1.,  3.,  5.,  7.,  9., 11., 13., 15., 17., 19.], dtype=float32) 

4. numpy.linspace(): This function returns evenly spaced numbers over a specified between two limits. 

Syntax: numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

Example:

Python
import numpy as np

np.linspace(3.5, 10, 3, 
            dtype = np.int32)

Output:

array([ 3, 6, 10], dtype=int32)

5. numpy.empty(): This function create a new array of given shape and type without initializing value.

Syntax: numpy.empty(shape, dtype=float, order='C')

Example:

Python
import numpy as np

np.empty([4, 3],
         dtype = np.int32,
         order = 'f')

Output:

n6
numpy.empty()

6. numpy.ones(): This function is used to get a new array of given shape and type filled with ones (1).

Syntax: numpy.ones(shape, dtype=None, order='C')

Example:

Python
import numpy as np

np.ones([4, 3],
        dtype = np.int32,
        order = 'f')

Output:

n7
numpy.ones()

7. numpy.zeros(): This function is used to get a new array of given shape and type filled with zeros (0). 

Syntax: numpy.ones(shape, dtype=None)

Example:

Python
import numpy as np
np.zeros([4, 3], 
         dtype = np.int32,
         order = 'f')

Output:

n8
numpy.zeros()

As we move forward NumPy will remain an important tool for efficient data manipulation and mastering its core concepts will helps us to solve more complex computational challenges in the future.


Next Article
Article Tags :
Practice Tags :

Similar Reads